mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-09-22 15:04:47 +00:00
implement BuildCommand
This commit is contained in:
124
ElectronNET.CLI/Commands/BuildCommand.cs
Normal file
124
ElectronNET.CLI/Commands/BuildCommand.cs
Normal file
@@ -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 = "<Path> from ASP.NET Core Project.";
|
||||
public static IList<CommandOption> CommandOptions { get; set; } = new List<CommandOption>();
|
||||
|
||||
public Task<bool> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
/// </summary>
|
||||
public interface ICommand
|
||||
{
|
||||
/// <summary>
|
||||
/// If enabled the tool will prompt for required fields if they are not already given.
|
||||
/// </summary>
|
||||
bool DisableInteractive { get; set; }
|
||||
|
||||
Task<bool> ExecuteAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,8 +12,6 @@ namespace ElectronNET.CLI.Commands
|
||||
public const string COMMAND_DESCRIPTION = "install";
|
||||
public static IList<CommandOption> CommandOptions { get; set; } = new List<CommandOption>();
|
||||
|
||||
public bool DisableInteractive { get; set; }
|
||||
|
||||
private string[] _args;
|
||||
|
||||
public InstallElectronCommand(string[] args)
|
||||
|
||||
@@ -13,8 +13,6 @@ namespace ElectronNET.CLI.Commands
|
||||
public const string COMMAND_ARGUMENTS = "<Path> from ASP.NET Core Project.";
|
||||
public static IList<CommandOption> CommandOptions { get; set; } = new List<CommandOption>();
|
||||
|
||||
public bool DisableInteractive { get; set; }
|
||||
|
||||
private string[] _args;
|
||||
|
||||
public StartElectronCommand(string[] args)
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
});
|
||||
@@ -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();
|
||||
|
||||
@@ -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\""
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
electron-packager . --platform=win32 --arch=x64 --out="C:\Users\Gregor\Documents\Visual Studio 2017\Projects\ElectronNET\ElectronNET.WebApp\bin\desktop" --overwrite
|
||||
Reference in New Issue
Block a user