diff --git a/ElectronNET.CLI/Commands/BuildCommand.cs b/ElectronNET.CLI/Commands/BuildCommand.cs new file mode 100644 index 0000000..372d0c2 --- /dev/null +++ b/ElectronNET.CLI/Commands/BuildCommand.cs @@ -0,0 +1,124 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Text; +using System.Threading.Tasks; + +namespace ElectronNET.CLI.Commands +{ + public class BuildCommand : ICommand + { + public const string COMMAND_NAME = "build"; + public const string COMMAND_DESCRIPTION = "Build your Electron Application."; + public const string COMMAND_ARGUMENTS = " from ASP.NET Core Project."; + public static IList CommandOptions { get; set; } = new List(); + + public Task ExecuteAsync() + { + return Task.Run(() => + { + Console.WriteLine("Build Windows Application..."); + + string currentAssemblyPath = AppDomain.CurrentDomain.BaseDirectory; + string targetPath = Path.Combine(currentAssemblyPath, "Host"); + + Console.WriteLine("Target: " + targetPath); + + using (Process cmd = new Process()) + { + cmd.StartInfo.FileName = "cmd.exe"; + cmd.StartInfo.RedirectStandardInput = true; + cmd.StartInfo.RedirectStandardOutput = true; + cmd.StartInfo.CreateNoWindow = true; + cmd.StartInfo.UseShellExecute = false; + cmd.StartInfo.WorkingDirectory = Directory.GetCurrentDirectory(); + + cmd.Start(); + + cmd.StandardInput.WriteLine("dotnet publish -r win10-x64 --output " + Path.Combine(targetPath, "bin")); + cmd.StandardInput.Flush(); + cmd.StandardInput.Close(); + cmd.WaitForExit(); + Console.WriteLine(cmd.StandardOutput.ReadToEnd()); + } + + if (Directory.Exists(targetPath) == false) + { + Directory.CreateDirectory(targetPath); + } + + DeployEmbeddedFile(targetPath, "main.js"); + DeployEmbeddedFile(targetPath, "package.json"); + DeployEmbeddedFile(targetPath, "package-lock.json"); + + Console.WriteLine("Start npm install..."); + using (Process cmd = new Process()) + { + cmd.StartInfo.FileName = "cmd.exe"; + cmd.StartInfo.RedirectStandardInput = true; + cmd.StartInfo.RedirectStandardOutput = true; + cmd.StartInfo.CreateNoWindow = true; + cmd.StartInfo.UseShellExecute = false; + cmd.StartInfo.WorkingDirectory = targetPath; + + cmd.Start(); + + cmd.StandardInput.WriteLine("npm install"); + cmd.StandardInput.Flush(); + cmd.StandardInput.Close(); + cmd.WaitForExit(); + Console.WriteLine(cmd.StandardOutput.ReadToEnd()); + } + + using (Process cmd = new Process()) + { + cmd.StartInfo.FileName = "cmd.exe"; + cmd.StartInfo.RedirectStandardInput = true; + cmd.StartInfo.RedirectStandardOutput = true; + cmd.StartInfo.CreateNoWindow = true; + cmd.StartInfo.UseShellExecute = false; + cmd.StartInfo.WorkingDirectory = targetPath; + + cmd.Start(); + + cmd.StandardInput.WriteLine("npm install electron-packager --global"); + cmd.StandardInput.Flush(); + cmd.StandardInput.Close(); + cmd.WaitForExit(); + Console.WriteLine(cmd.StandardOutput.ReadToEnd()); + } + + using (Process cmd = new Process()) + { + cmd.StartInfo.FileName = "cmd.exe"; + cmd.StartInfo.RedirectStandardInput = true; + cmd.StartInfo.RedirectStandardOutput = true; + cmd.StartInfo.CreateNoWindow = true; + cmd.StartInfo.UseShellExecute = false; + cmd.StartInfo.WorkingDirectory = targetPath; + + cmd.Start(); + + string buildPath = Path.Combine(Directory.GetCurrentDirectory(), "bin", "desktop"); + cmd.StandardInput.WriteLine($"electron-packager . --platform=win32 --arch=x64 --out=\"{buildPath}\" --overwrite"); + cmd.StandardInput.Flush(); + cmd.StandardInput.Close(); + cmd.WaitForExit(); + Console.WriteLine(cmd.StandardOutput.ReadToEnd()); + } + + return true; + }); + } + + private static void DeployEmbeddedFile(string targetPath, string file) + { + using (var fileStream = File.Create(Path.Combine(targetPath, file))) + { + var streamFromEmbeddedFile = EmbeddedFileHelper.GetTestResourceFileStream("ElectronHost." + file); + streamFromEmbeddedFile.CopyTo(fileStream); + } + } + } +} diff --git a/ElectronNET.CLI/Commands/ICommand.cs b/ElectronNET.CLI/Commands/ICommand.cs index 1ea9489..e247a2a 100644 --- a/ElectronNET.CLI/Commands/ICommand.cs +++ b/ElectronNET.CLI/Commands/ICommand.cs @@ -1,5 +1,4 @@ -using System.Collections.Generic; -using System.Threading.Tasks; +using System.Threading.Tasks; namespace ElectronNET.CLI.Commands { @@ -8,11 +7,6 @@ namespace ElectronNET.CLI.Commands /// public interface ICommand { - /// - /// If enabled the tool will prompt for required fields if they are not already given. - /// - bool DisableInteractive { get; set; } - Task ExecuteAsync(); } } diff --git a/ElectronNET.CLI/Commands/InstallElectronCommand.cs b/ElectronNET.CLI/Commands/InstallElectronCommand.cs index ae6c320..25d86f3 100644 --- a/ElectronNET.CLI/Commands/InstallElectronCommand.cs +++ b/ElectronNET.CLI/Commands/InstallElectronCommand.cs @@ -12,8 +12,6 @@ namespace ElectronNET.CLI.Commands public const string COMMAND_DESCRIPTION = "install"; public static IList CommandOptions { get; set; } = new List(); - public bool DisableInteractive { get; set; } - private string[] _args; public InstallElectronCommand(string[] args) diff --git a/ElectronNET.CLI/Commands/StartElectronCommand.cs b/ElectronNET.CLI/Commands/StartElectronCommand.cs index 635e138..00998f9 100644 --- a/ElectronNET.CLI/Commands/StartElectronCommand.cs +++ b/ElectronNET.CLI/Commands/StartElectronCommand.cs @@ -13,8 +13,6 @@ namespace ElectronNET.CLI.Commands public const string COMMAND_ARGUMENTS = " from ASP.NET Core Project."; public static IList CommandOptions { get; set; } = new List(); - public bool DisableInteractive { get; set; } - private string[] _args; public StartElectronCommand(string[] args) diff --git a/ElectronNET.CLI/ElectronHost/main.js b/ElectronNET.CLI/ElectronHost/main.js index 7f8d453..9d28157 100644 --- a/ElectronNET.CLI/ElectronHost/main.js +++ b/ElectronNET.CLI/ElectronHost/main.js @@ -9,7 +9,7 @@ app.on('ready', () => { const process = require('child_process').spawn; // run server - var apipath = path.join(__dirname, '..\\ElectronNET.WebApp\\bin\\dist\\win\\ElectronNET.WebApp.exe'); + var apipath = path.join(__dirname, '\\bin\\ElectronNET.WebApp.exe'); apiProcess = process(apipath); apiProcess.stdout.on('data', (data) => { @@ -40,3 +40,19 @@ io.on('connection', (socket) => { }); }); +// Quit when all windows are closed. +app.on('window-all-closed', () => { + // On macOS it is common for applications and their menu bar + // to stay active until the user quits explicitly with Cmd + Q + if (process.platform !== 'darwin') { + app.quit(); + } +}); + +app.on('activate', () => { + // On macOS it's common to re-create a window in the app when the + // dock icon is clicked and there are no other windows open. + if (win === null) { + createWindow(); + } +}); \ No newline at end of file diff --git a/ElectronNET.CLI/Program.cs b/ElectronNET.CLI/Program.cs index 0005019..ed195d8 100644 --- a/ElectronNET.CLI/Program.cs +++ b/ElectronNET.CLI/Program.cs @@ -28,6 +28,9 @@ namespace ElectronNET.CLI case InstallElectronCommand.COMMAND_NAME: command = new InstallElectronCommand(args.Skip(1).ToArray()); break; + case BuildCommand.COMMAND_NAME: + command = new BuildCommand(); + break; case "--help": case "--h": case "help": @@ -79,7 +82,7 @@ namespace ElectronNET.CLI Console.WriteLine("\t"); Console.WriteLine("Commands to build the Electron Application:"); Console.WriteLine("\t"); - Console.WriteLine($"\t{InstallElectronCommand.COMMAND_NAME.PadRight(NAME_WIDTH)} {InstallElectronCommand.COMMAND_DESCRIPTION}"); + Console.WriteLine($"\t{BuildCommand.COMMAND_NAME.PadRight(NAME_WIDTH)} {BuildCommand.COMMAND_DESCRIPTION}"); Console.WriteLine("\t"); @@ -95,6 +98,9 @@ namespace ElectronNET.CLI case StartElectronCommand.COMMAND_NAME: PrintUsage(StartElectronCommand.COMMAND_NAME, StartElectronCommand.COMMAND_DESCRIPTION, StartElectronCommand.CommandOptions, StartElectronCommand.COMMAND_ARGUMENTS); break; + case BuildCommand.COMMAND_NAME: + PrintUsage(BuildCommand.COMMAND_NAME, BuildCommand.COMMAND_DESCRIPTION, BuildCommand.CommandOptions, BuildCommand.COMMAND_ARGUMENTS); + break; default: Console.Error.WriteLine($"Unknown command {command}"); PrintUsage(); diff --git a/ElectronNET.CLI/Properties/launchSettings.json b/ElectronNET.CLI/Properties/launchSettings.json index a99107e..bcce156 100644 --- a/ElectronNET.CLI/Properties/launchSettings.json +++ b/ElectronNET.CLI/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "ElectronNET.CLI": { "commandName": "Project", - "commandLineArgs": "start \"C:\\Users\\Gregor\\Documents\\Visual Studio 2017\\Projects\\ElectronNET\\ElectronNET.WebApp\"" + "commandLineArgs": "build \"C:\\Users\\Gregor\\Documents\\Visual Studio 2017\\Projects\\ElectronNET\\ElectronNET.WebApp\"" } } } \ No newline at end of file diff --git a/build.cmd b/build.cmd index ec30601..3bf1a2b 100644 --- a/build.cmd +++ b/build.cmd @@ -3,4 +3,4 @@ dotnet restore dotnet publish -r win10-x64 --output ../ElectronNET.Host/bin/ cd ..\ElectronNET.Host -electron-packager . --platform=win32 --arch=x64 --out=../ElectronNET.WebApp/bin/desktop/ --overwrite \ No newline at end of file +electron-packager . --platform=win32 --arch=x64 --out="C:\Users\Gregor\Documents\Visual Studio 2017\Projects\ElectronNET\ElectronNET.WebApp\bin\desktop" --overwrite \ No newline at end of file