mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-08-08 17:19:41 +00:00
refactor implementation
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
33
ElectronNET.CLI/ProcessHelper.cs
Normal file
33
ElectronNET.CLI/ProcessHelper.cs
Normal file
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user