diff --git a/src/ElectronNET.API/ElectronNetRuntime.cs b/src/ElectronNET.API/ElectronNetRuntime.cs index 3f6fb43..78d976e 100644 --- a/src/ElectronNET.API/ElectronNetRuntime.cs +++ b/src/ElectronNET.API/ElectronNetRuntime.cs @@ -24,6 +24,8 @@ StartupManager.Initialize(); } + public static string ElectronExtraArguments { get; set; } + public static int? ElectronSocketPort { get; internal set; } public static int? AspNetWebPort { get; internal set; } diff --git a/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerDotNetFirst.cs b/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerDotNetFirst.cs index 8e1633d..7059167 100644 --- a/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerDotNetFirst.cs +++ b/src/ElectronNET.API/Runtime/Controllers/RuntimeControllerDotNetFirst.cs @@ -40,7 +40,7 @@ { var isUnPacked = ElectronNetRuntime.StartupMethod.IsUnpackaged(); var electronBinaryName = ElectronNetRuntime.ElectronExecutable; - var args = Environment.CommandLine; + var args = string.Format("{0} {1}", ElectronNetRuntime.ElectronExtraArguments, Environment.CommandLine).Trim(); this.port = ElectronNetRuntime.ElectronSocketPort; if (!this.port.HasValue) @@ -49,10 +49,15 @@ ElectronNetRuntime.ElectronSocketPort = this.port; } + Console.Error.WriteLine("[StartCore]: isUnPacked: {0}", isUnPacked); + Console.Error.WriteLine("[StartCore]: electronBinaryName: {0}", electronBinaryName); + Console.Error.WriteLine("[StartCore]: args: {0}", args); + this.electronProcess = new ElectronProcessActive(isUnPacked, electronBinaryName, args, this.port.Value); this.electronProcess.Ready += this.ElectronProcess_Ready; this.electronProcess.Stopped += this.ElectronProcess_Stopped; + Console.Error.WriteLine("[StartCore]: Before Start"); return this.electronProcess.Start(); } @@ -82,11 +87,11 @@ private void HandleStopped() { - if (this.socketBridge.State != LifetimeState.Stopped) + if (this.socketBridge != null && this.socketBridge.State != LifetimeState.Stopped) { this.socketBridge.Stop(); } - else if (this.electronProcess.State != LifetimeState.Stopped) + else if (this.electronProcess != null && this.electronProcess.State != LifetimeState.Stopped) { this.electronProcess.Stop(); } diff --git a/src/ElectronNET.API/Runtime/Services/ElectronProcess/ElectronProcessActive.cs b/src/ElectronNET.API/Runtime/Services/ElectronProcess/ElectronProcessActive.cs index 297c17c..0cf21c5 100644 --- a/src/ElectronNET.API/Runtime/Services/ElectronProcess/ElectronProcessActive.cs +++ b/src/ElectronNET.API/Runtime/Services/ElectronProcess/ElectronProcessActive.cs @@ -5,6 +5,7 @@ using System; using System.ComponentModel; using System.IO; + using System.Runtime.InteropServices; using System.Threading.Tasks; /// @@ -42,6 +43,11 @@ 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; } @@ -68,22 +74,40 @@ private async Task StartInternal(string startCmd, string args, string directoriy) { - await Task.Delay(10).ConfigureAwait(false); - - this.process = new ProcessRunner("ElectronRunner"); - this.process.ProcessExited += this.Process_Exited; - this.process.Run(startCmd, args, directoriy); - - await Task.Delay(500).ConfigureAwait(false); - - if (!this.process.IsRunning) + try { - Task.Run(() => this.TransitionState(LifetimeState.Stopped)); + await Task.Delay(10).ConfigureAwait(false); - throw new Exception("Failed to launch the Electron process."); + 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; } - - this.TransitionState(LifetimeState.Ready); } private void Process_Exited(object sender, EventArgs e) diff --git a/src/ElectronNET.API/Runtime/StartupManager.cs b/src/ElectronNET.API/Runtime/StartupManager.cs index 607d58b..125b6de 100644 --- a/src/ElectronNET.API/Runtime/StartupManager.cs +++ b/src/ElectronNET.API/Runtime/StartupManager.cs @@ -132,13 +132,19 @@ if (electronAssembly == null) { + Console.WriteLine("GatherBuildInfo: Early exit"); return buildInfo; } if (electronAssembly.GetName().Name == "testhost" || electronAssembly.GetName().Name == "ReSharperTestRunner") { + Console.WriteLine("GatherBuildInfo: Detected testhost"); electronAssembly = AppDomain.CurrentDomain.GetData("ElectronTestAssembly") as Assembly ?? electronAssembly; } + else + { + Console.WriteLine("GatherBuildInfo: No testhost detected: " + electronAssembly.GetName().Name); + } var attributes = electronAssembly.GetCustomAttributes().ToList();