2025-10-13 13:24:28 +02:00
|
|
|
|
namespace ElectronNET.Runtime.Services.ElectronProcess
|
|
|
|
|
|
{
|
2025-11-09 12:05:07 +01:00
|
|
|
|
using ElectronNET.Common;
|
|
|
|
|
|
using ElectronNET.Runtime.Data;
|
2025-10-13 13:24:28 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
|
using System.IO;
|
2025-11-15 09:31:19 +01:00
|
|
|
|
using System.Runtime.InteropServices;
|
2025-10-13 13:24:28 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Launches and manages the Electron app process.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Localizable(false)]
|
|
|
|
|
|
internal class ElectronProcessActive : ElectronProcessBase
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly bool isUnpackaged;
|
|
|
|
|
|
private readonly string electronBinaryName;
|
|
|
|
|
|
private readonly string extraArguments;
|
|
|
|
|
|
private readonly int socketPort;
|
|
|
|
|
|
private ProcessRunner process;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="ElectronProcessActive"/> class.</summary>
|
|
|
|
|
|
/// <param name="isUnpackaged">The is debug.</param>
|
|
|
|
|
|
/// <param name="electronBinaryName">Name of the electron.</param>
|
|
|
|
|
|
/// <param name="extraArguments">The extraArguments.</param>
|
|
|
|
|
|
/// <param name="socketPort">The socket port.</param>
|
|
|
|
|
|
public ElectronProcessActive(bool isUnpackaged, string electronBinaryName, string extraArguments, int socketPort)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.isUnpackaged = isUnpackaged;
|
|
|
|
|
|
this.electronBinaryName = electronBinaryName;
|
|
|
|
|
|
this.extraArguments = extraArguments;
|
|
|
|
|
|
this.socketPort = socketPort;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override Task StartCore()
|
|
|
|
|
|
{
|
|
|
|
|
|
var dir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
|
|
|
|
|
|
string startCmd, args, workingDir;
|
|
|
|
|
|
|
|
|
|
|
|
if (this.isUnpackaged)
|
|
|
|
|
|
{
|
|
|
|
|
|
var electrondir = Path.Combine(dir.FullName, ".electron");
|
|
|
|
|
|
startCmd = Path.Combine(electrondir, "node_modules", "electron", "dist", "electron");
|
|
|
|
|
|
|
2025-11-15 09:31:19 +01:00
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
|
|
|
|
|
{
|
|
|
|
|
|
startCmd = Path.Combine(electrondir, "node_modules", "electron", "dist", "Electron.app", "Contents", "MacOS", "Electron");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-13 13:24:28 +02:00
|
|
|
|
args = $"main.js -unpackeddotnet --trace-warnings -electronforcedport={this.socketPort:D} " + this.extraArguments;
|
|
|
|
|
|
workingDir = electrondir;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
dir = dir.Parent?.Parent;
|
|
|
|
|
|
startCmd = Path.Combine(dir.FullName, this.electronBinaryName);
|
|
|
|
|
|
args = $"-dotnetpacked -electronforcedport={this.socketPort:D} " + this.extraArguments;
|
|
|
|
|
|
workingDir = dir.FullName;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// We don't await this in order to let the state transition to "Starting"
|
|
|
|
|
|
Task.Run(async () => await this.StartInternal(startCmd, args, workingDir).ConfigureAwait(false));
|
|
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override Task StopCore()
|
|
|
|
|
|
{
|
|
|
|
|
|
this.process.Cancel();
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task StartInternal(string startCmd, string args, string directoriy)
|
|
|
|
|
|
{
|
2025-11-15 09:31:19 +01:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-12-07 11:20:43 +01:00
|
|
|
|
await Task.Delay(10.ms()).ConfigureAwait(false);
|
2025-10-13 13:24:28 +02:00
|
|
|
|
|
2025-11-15 09:31:19 +01:00
|
|
|
|
Console.Error.WriteLine("[StartInternal]: startCmd: {0}", startCmd);
|
|
|
|
|
|
Console.Error.WriteLine("[StartInternal]: args: {0}", args);
|
2025-10-13 13:24:28 +02:00
|
|
|
|
|
2025-11-15 09:31:19 +01:00
|
|
|
|
this.process = new ProcessRunner("ElectronRunner");
|
|
|
|
|
|
this.process.ProcessExited += this.Process_Exited;
|
|
|
|
|
|
this.process.Run(startCmd, args, directoriy);
|
2025-10-13 13:24:28 +02:00
|
|
|
|
|
2025-12-07 11:20:43 +01:00
|
|
|
|
await Task.Delay(500.ms()).ConfigureAwait(false);
|
2025-10-13 13:24:28 +02:00
|
|
|
|
|
2025-11-15 09:31:19 +01:00
|
|
|
|
Console.Error.WriteLine("[StartInternal]: after run:");
|
|
|
|
|
|
|
|
|
|
|
|
if (!this.process.IsRunning)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.Error.WriteLine("[StartInternal]: Process is not running: " + this.process.StandardError);
|
|
|
|
|
|
Console.Error.WriteLine("[StartInternal]: Process is not running: " + this.process.StandardOutput);
|
2025-10-13 13:24:28 +02:00
|
|
|
|
|
2025-11-15 09:31:19 +01:00
|
|
|
|
Task.Run(() => this.TransitionState(LifetimeState.Stopped));
|
|
|
|
|
|
|
|
|
|
|
|
throw new Exception("Failed to launch the Electron process.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.TransitionState(LifetimeState.Ready);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.Error.WriteLine("[StartInternal]: Exception: " + this.process?.StandardError);
|
|
|
|
|
|
Console.Error.WriteLine("[StartInternal]: Exception: " + this.process?.StandardOutput);
|
|
|
|
|
|
Console.Error.WriteLine("[StartInternal]: Exception: " + ex);
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
2025-10-13 13:24:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void Process_Exited(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.TransitionState(LifetimeState.Stopped);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|