forked from ElectronNET/Electron.NET
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathElectronProcessActive.cs
More file actions
118 lines (98 loc) · 4.61 KB
/
ElectronProcessActive.cs
File metadata and controls
118 lines (98 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
namespace ElectronNET.Runtime.Services.ElectronProcess
{
using ElectronNET.Common;
using ElectronNET.Runtime.Data;
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
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");
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
startCmd = Path.Combine(electrondir, "node_modules", "electron", "dist", "Electron.app", "Contents", "MacOS", "Electron");
}
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)
{
try
{
await Task.Delay(10).ConfigureAwait(false);
Console.Error.WriteLine("[StartInternal]: startCmd: {0}", startCmd);
Console.Error.WriteLine("[StartInternal]: args: {0}", args);
this.process = new ProcessRunner("ElectronRunner");
this.process.ProcessExited += this.Process_Exited;
this.process.Run(startCmd, args, directoriy);
await Task.Delay(500).ConfigureAwait(false);
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);
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;
}
}
private void Process_Exited(object sender, EventArgs e)
{
this.TransitionState(LifetimeState.Stopped);
}
}
}