From d224e7b1ad2502b5482d3cd529934d4de2c82802 Mon Sep 17 00:00:00 2001 From: Gregor Biswanger Date: Thu, 5 Oct 2017 21:28:47 +0200 Subject: [PATCH] refactor implementation --- ElectronNET.CLI/Commands/BuildCommand.cs | 102 +++--------------- .../Commands/InstallElectronCommand.cs | 15 +-- .../Commands/StartElectronCommand.cs | 34 ++---- ElectronNET.CLI/EmbeddedFileHelper.cs | 12 ++- ElectronNET.CLI/ProcessHelper.cs | 33 ++++++ 5 files changed, 71 insertions(+), 125 deletions(-) create mode 100644 ElectronNET.CLI/ProcessHelper.cs diff --git a/ElectronNET.CLI/Commands/BuildCommand.cs b/ElectronNET.CLI/Commands/BuildCommand.cs index 372d0c2..51905a0 100644 --- a/ElectronNET.CLI/Commands/BuildCommand.cs +++ b/ElectronNET.CLI/Commands/BuildCommand.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; -using System.Text; using System.Threading.Tasks; namespace ElectronNET.CLI.Commands @@ -21,104 +19,32 @@ namespace ElectronNET.CLI.Commands Console.WriteLine("Build Windows Application..."); string currentAssemblyPath = AppDomain.CurrentDomain.BaseDirectory; - string targetPath = Path.Combine(currentAssemblyPath, "Host"); + string tempPath = Path.Combine(currentAssemblyPath, "Host"); + ProcessHelper.CmdExecute("dotnet publish -r win10-x64 --output " + Path.Combine(tempPath, "bin"), Directory.GetCurrentDirectory()); - Console.WriteLine("Target: " + targetPath); - - using (Process cmd = new Process()) + if (Directory.Exists(tempPath) == false) { - 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()); + Directory.CreateDirectory(tempPath); } - if (Directory.Exists(targetPath) == false) - { - Directory.CreateDirectory(targetPath); - } - - DeployEmbeddedFile(targetPath, "main.js"); - DeployEmbeddedFile(targetPath, "package.json"); - DeployEmbeddedFile(targetPath, "package-lock.json"); + EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "main.js"); + EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package.json"); + EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "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; + ProcessHelper.CmdExecute("npm install", tempPath); - cmd.Start(); + Console.WriteLine("Start npm install electron-packager..."); + ProcessHelper.CmdExecute("npm install electron-packager --global", tempPath); - 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()); - } + Console.WriteLine("Build Electron Desktop Application..."); + string buildPath = Path.Combine(Directory.GetCurrentDirectory(), "bin", "desktop"); + ProcessHelper.CmdExecute($"electron-packager . --platform=win32 --arch=x64 --electronVersion=1.7.8 --out=\"{buildPath}\" --overwrite", tempPath); 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/InstallElectronCommand.cs b/ElectronNET.CLI/Commands/InstallElectronCommand.cs index 25d86f3..8625dcf 100644 --- a/ElectronNET.CLI/Commands/InstallElectronCommand.cs +++ b/ElectronNET.CLI/Commands/InstallElectronCommand.cs @@ -35,9 +35,9 @@ namespace ElectronNET.CLI.Commands Directory.CreateDirectory(targetPath); } - DeployEmbeddedFile(targetPath, "main.js"); - DeployEmbeddedFile(targetPath, "package.json"); - DeployEmbeddedFile(targetPath, "package-lock.json"); + EmbeddedFileHelper.DeployEmbeddedFile(targetPath, "main.js"); + EmbeddedFileHelper.DeployEmbeddedFile(targetPath, "package.json"); + EmbeddedFileHelper.DeployEmbeddedFile(targetPath, "package-lock.json"); Console.WriteLine("Start npm install..."); using (Process cmd = new Process()) @@ -61,14 +61,5 @@ namespace ElectronNET.CLI.Commands 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/StartElectronCommand.cs b/ElectronNET.CLI/Commands/StartElectronCommand.cs index 00998f9..014e09c 100644 --- a/ElectronNET.CLI/Commands/StartElectronCommand.cs +++ b/ElectronNET.CLI/Commands/StartElectronCommand.cs @@ -24,10 +24,9 @@ namespace ElectronNET.CLI.Commands { return Task.Run(() => { - Console.WriteLine("Start Electron..."); + Console.WriteLine("Start Electron Desktop Application..."); string aspCoreProjectPath = ""; - string electronHostProjectPath = ""; if (_args.Length > 0) { @@ -41,30 +40,19 @@ namespace ElectronNET.CLI.Commands aspCoreProjectPath = Directory.GetCurrentDirectory(); } - 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; + string currentAssemblyPath = AppDomain.CurrentDomain.BaseDirectory; + string tempPath = Path.Combine(currentAssemblyPath, "Host"); + ProcessHelper.CmdExecute("dotnet publish -r win10-x64 --output " + Path.Combine(tempPath, "bin"), aspCoreProjectPath); - cmd.Start(); + EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "main.js"); + EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package.json"); + EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package-lock.json"); - cmd.StandardInput.WriteLine("cd " + aspCoreProjectPath); - cmd.StandardInput.Flush(); + Console.WriteLine("Start npm install..."); + ProcessHelper.CmdExecute("npm install", tempPath); + ProcessHelper.CmdExecute("npm install electron@1.7.8", tempPath); - cmd.StandardInput.WriteLine("dotnet restore"); - cmd.StandardInput.Flush(); - - cmd.StandardInput.WriteLine("dotnet publish -r win10-x64 --output bin/dist/win"); - cmd.StandardInput.Flush(); - - cmd.StandardInput.WriteLine(@"..\ElectronNET.Host\node_modules\.bin\electron.cmd ""..\ElectronNET.Host\main.js"""); - cmd.StandardInput.Flush(); - - cmd.StandardInput.Close(); - } + ProcessHelper.CmdExecute(@"electron.cmd ""..\..\main.js""", Path.Combine(tempPath, "node_modules", ".bin")); return true; }); diff --git a/ElectronNET.CLI/EmbeddedFileHelper.cs b/ElectronNET.CLI/EmbeddedFileHelper.cs index 1a4868d..c2d43a8 100644 --- a/ElectronNET.CLI/EmbeddedFileHelper.cs +++ b/ElectronNET.CLI/EmbeddedFileHelper.cs @@ -8,7 +8,7 @@ namespace ElectronNET.CLI { private const string ResourcePath = "ElectronNET.CLI.{0}"; - public static Stream GetTestResourceFileStream(string folderAndFileInProjectPath) + private static Stream GetTestResourceFileStream(string folderAndFileInProjectPath) { var asm = Assembly.GetExecutingAssembly(); var resource = string.Format(ResourcePath, folderAndFileInProjectPath); @@ -16,7 +16,7 @@ namespace ElectronNET.CLI return asm.GetManifestResourceStream(resource); } - public static string GetTestResourceFileContent(string folderAndFileInProjectPath) + private static string GetTestResourceFileContent(string folderAndFileInProjectPath) { var asm = Assembly.GetExecutingAssembly(); var resource = string.Format(ResourcePath, folderAndFileInProjectPath); @@ -32,5 +32,13 @@ namespace ElectronNET.CLI return String.Empty; } + public static void DeployEmbeddedFile(string targetPath, string file) + { + using (var fileStream = File.Create(Path.Combine(targetPath, file))) + { + var streamFromEmbeddedFile = GetTestResourceFileStream("ElectronHost." + file); + streamFromEmbeddedFile.CopyTo(fileStream); + } + } } } \ No newline at end of file diff --git a/ElectronNET.CLI/ProcessHelper.cs b/ElectronNET.CLI/ProcessHelper.cs new file mode 100644 index 0000000..d4c3c1d --- /dev/null +++ b/ElectronNET.CLI/ProcessHelper.cs @@ -0,0 +1,33 @@ +using System; +using System.Diagnostics; + +namespace ElectronNET.CLI +{ + public class ProcessHelper + { + public static void CmdExecute(string command, string workingDirectoryPath, bool output = true) + { + 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 = workingDirectoryPath; + + cmd.Start(); + + cmd.StandardInput.WriteLine(command); + cmd.StandardInput.Flush(); + cmd.StandardInput.Close(); + cmd.WaitForExit(); + + if (output) + { + Console.WriteLine(cmd.StandardOutput.ReadToEnd()); + } + } + } + } +}