Skip to content

Commit a30239e

Browse files
committed
Update electron start
1 parent 17f2474 commit a30239e

File tree

4 files changed

+51
-14
lines changed

4 files changed

+51
-14
lines changed

src/ElectronNET.API/ElectronNetRuntime.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ static ElectronNetRuntime()
2424
StartupManager.Initialize();
2525
}
2626

27+
public static string ElectronExtraArguments { get; set; }
28+
2729
public static int? ElectronSocketPort { get; internal set; }
2830

2931
public static int? AspNetWebPort { get; internal set; }

src/ElectronNET.API/Runtime/Controllers/RuntimeControllerDotNetFirst.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected override Task StartCore()
4040
{
4141
var isUnPacked = ElectronNetRuntime.StartupMethod.IsUnpackaged();
4242
var electronBinaryName = ElectronNetRuntime.ElectronExecutable;
43-
var args = Environment.CommandLine;
43+
var args = string.Format("{0} {1}", ElectronNetRuntime.ElectronExtraArguments, Environment.CommandLine).Trim();
4444
this.port = ElectronNetRuntime.ElectronSocketPort;
4545

4646
if (!this.port.HasValue)
@@ -49,10 +49,15 @@ protected override Task StartCore()
4949
ElectronNetRuntime.ElectronSocketPort = this.port;
5050
}
5151

52+
Console.Error.WriteLine("[StartCore]: isUnPacked: {0}", isUnPacked);
53+
Console.Error.WriteLine("[StartCore]: electronBinaryName: {0}", electronBinaryName);
54+
Console.Error.WriteLine("[StartCore]: args: {0}", args);
55+
5256
this.electronProcess = new ElectronProcessActive(isUnPacked, electronBinaryName, args, this.port.Value);
5357
this.electronProcess.Ready += this.ElectronProcess_Ready;
5458
this.electronProcess.Stopped += this.ElectronProcess_Stopped;
5559

60+
Console.Error.WriteLine("[StartCore]: Before Start");
5661
return this.electronProcess.Start();
5762
}
5863

@@ -82,11 +87,11 @@ private void ElectronProcess_Stopped(object sender, EventArgs e)
8287

8388
private void HandleStopped()
8489
{
85-
if (this.socketBridge.State != LifetimeState.Stopped)
90+
if (this.socketBridge != null && this.socketBridge.State != LifetimeState.Stopped)
8691
{
8792
this.socketBridge.Stop();
8893
}
89-
else if (this.electronProcess.State != LifetimeState.Stopped)
94+
else if (this.electronProcess != null && this.electronProcess.State != LifetimeState.Stopped)
9095
{
9196
this.electronProcess.Stop();
9297
}

src/ElectronNET.API/Runtime/Services/ElectronProcess/ElectronProcessActive.cs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.ComponentModel;
77
using System.IO;
8+
using System.Runtime.InteropServices;
89
using System.Threading.Tasks;
910

1011
/// <summary>
@@ -42,6 +43,11 @@ protected override Task StartCore()
4243
var electrondir = Path.Combine(dir.FullName, ".electron");
4344
startCmd = Path.Combine(electrondir, "node_modules", "electron", "dist", "electron");
4445

46+
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
47+
{
48+
startCmd = Path.Combine(electrondir, "node_modules", "electron", "dist", "Electron.app", "Contents", "MacOS", "Electron");
49+
}
50+
4551
args = $"main.js -unpackeddotnet --trace-warnings -electronforcedport={this.socketPort:D} " + this.extraArguments;
4652
workingDir = electrondir;
4753
}
@@ -68,22 +74,40 @@ protected override Task StopCore()
6874

6975
private async Task StartInternal(string startCmd, string args, string directoriy)
7076
{
71-
await Task.Delay(10).ConfigureAwait(false);
77+
try
78+
{
79+
await Task.Delay(10).ConfigureAwait(false);
7280

73-
this.process = new ProcessRunner("ElectronRunner");
74-
this.process.ProcessExited += this.Process_Exited;
75-
this.process.Run(startCmd, args, directoriy);
81+
Console.Error.WriteLine("[StartInternal]: startCmd: {0}", startCmd);
82+
Console.Error.WriteLine("[StartInternal]: args: {0}", args);
7683

77-
await Task.Delay(500).ConfigureAwait(false);
84+
this.process = new ProcessRunner("ElectronRunner");
85+
this.process.ProcessExited += this.Process_Exited;
86+
this.process.Run(startCmd, args, directoriy);
7887

79-
if (!this.process.IsRunning)
80-
{
81-
Task.Run(() => this.TransitionState(LifetimeState.Stopped));
88+
await Task.Delay(500).ConfigureAwait(false);
8289

83-
throw new Exception("Failed to launch the Electron process.");
84-
}
90+
Console.Error.WriteLine("[StartInternal]: after run:");
91+
92+
if (!this.process.IsRunning)
93+
{
94+
Console.Error.WriteLine("[StartInternal]: Process is not running: " + this.process.StandardError);
95+
Console.Error.WriteLine("[StartInternal]: Process is not running: " + this.process.StandardOutput);
8596

86-
this.TransitionState(LifetimeState.Ready);
97+
Task.Run(() => this.TransitionState(LifetimeState.Stopped));
98+
99+
throw new Exception("Failed to launch the Electron process.");
100+
}
101+
102+
this.TransitionState(LifetimeState.Ready);
103+
}
104+
catch (Exception ex)
105+
{
106+
Console.Error.WriteLine("[StartInternal]: Exception: " + this.process?.StandardError);
107+
Console.Error.WriteLine("[StartInternal]: Exception: " + this.process?.StandardOutput);
108+
Console.Error.WriteLine("[StartInternal]: Exception: " + ex);
109+
throw;
110+
}
87111
}
88112

89113
private void Process_Exited(object sender, EventArgs e)

src/ElectronNET.API/Runtime/StartupManager.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,19 @@ private BuildInfo GatherBuildInfo()
132132

133133
if (electronAssembly == null)
134134
{
135+
Console.WriteLine("GatherBuildInfo: Early exit");
135136
return buildInfo;
136137
}
137138

138139
if (electronAssembly.GetName().Name == "testhost" || electronAssembly.GetName().Name == "ReSharperTestRunner")
139140
{
141+
Console.WriteLine("GatherBuildInfo: Detected testhost");
140142
electronAssembly = AppDomain.CurrentDomain.GetData("ElectronTestAssembly") as Assembly ?? electronAssembly;
141143
}
144+
else
145+
{
146+
Console.WriteLine("GatherBuildInfo: No testhost detected: " + electronAssembly.GetName().Name);
147+
}
142148

143149
var attributes = electronAssembly.GetCustomAttributes<AssemblyMetadataAttribute>().ToList();
144150

0 commit comments

Comments
 (0)