Files
Electron.NET/ElectronNET.CLI/Commands/BuildCommand.cs

165 lines
8.5 KiB
C#
Raw Normal View History

2017-10-05 04:44:58 +02:00
using System;
using System.Collections.Generic;
using System.IO;
2017-10-08 23:22:20 +02:00
using System.Runtime.InteropServices;
2017-10-05 04:44:58 +02:00
using System.Threading.Tasks;
using ElectronNET.CLI.Commands.Actions;
2017-10-05 04:44:58 +02:00
namespace ElectronNET.CLI.Commands
{
public class BuildCommand : ICommand
{
public const string COMMAND_NAME = "build";
public const string COMMAND_DESCRIPTION = "Build your Electron Application.";
2018-02-19 22:07:07 +01:00
public static string COMMAND_ARGUMENTS = "Needed: '/target' with params 'win/osx/linux' to build for a typical app or use 'custom' and specify .NET Core build config & electron build config" + Environment.NewLine +
" for custom target, check .NET Core RID Catalog and Electron build target/" + Environment.NewLine +
" e.g. '/target win' or '/target custom \"win7-x86;win32\"'" + Environment.NewLine +
2019-01-15 22:21:25 +01:00
"Optional: '/dotnet-configuration' with the desired .NET Core build config e.g. release or debug. Default = Release" + Environment.NewLine +
2018-02-19 22:07:07 +01:00
"Optional: '/electron-arch' to specify the resulting electron processor architecture (e.g. ia86 for x86 builds). Be aware to use the '/target custom' param as well!" + Environment.NewLine +
"Optional: '/electron-params' specify any other valid parameter, which will be routed to the electron-packager." + Environment.NewLine +
"Optional: '/relative-path' to specify output a subdirectory for output." + Environment.NewLine +
"Optional: '/absolute-path to specify and absolute path for output." + Environment.NewLine +
"Optional: '/package-json' to specify a custom package.json file." + Environment.NewLine +
"Optional: '/install-modules' to force node module install. Implied by '/package-json'" + Environment.NewLine +
2018-02-19 22:07:07 +01:00
"Full example for a 32bit debug build with electron prune: build /target custom win7-x86;win32 /dotnet-configuration Debug /electron-arch ia32 /electron-params \"--prune=true \"";
2017-10-05 04:44:58 +02:00
public static IList<CommandOption> CommandOptions { get; set; } = new List<CommandOption>();
private string[] _args;
public BuildCommand(string[] args)
{
_args = args;
}
2018-02-12 21:17:40 +01:00
private string _paramTarget = "target";
private string _paramDotNetConfig = "dotnet-configuration";
private string _paramElectronArch = "electron-arch";
private string _paramElectronParams = "electron-params";
private string _paramOutputDirectory = "relative-path";
private string _paramAbsoluteOutput = "absolute-path";
private string _paramPackageJson = "package-json";
private string _paramForceNodeInstall = "install-modules";
2018-02-12 21:17:40 +01:00
2017-10-05 04:44:58 +02:00
public Task<bool> ExecuteAsync()
{
return Task.Run(() =>
{
2017-10-08 23:22:20 +02:00
Console.WriteLine("Build Electron Application...");
2017-10-05 04:44:58 +02:00
2018-02-11 22:24:12 +01:00
SimpleCommandLineParser parser = new SimpleCommandLineParser();
parser.Parse(_args);
2018-02-12 21:17:40 +01:00
var desiredPlatform = parser.Arguments[_paramTarget][0];
2018-02-11 22:24:12 +01:00
string specifiedFromCustom = string.Empty;
2018-02-12 21:17:40 +01:00
if (desiredPlatform == "custom" && parser.Arguments[_paramTarget].Length > 1)
2018-02-11 22:24:12 +01:00
{
2018-04-19 21:14:00 +02:00
specifiedFromCustom = parser.Arguments[_paramTarget][1];
2018-02-11 22:24:12 +01:00
}
2018-02-12 21:17:40 +01:00
string configuration = "Release";
if (parser.Arguments.ContainsKey(_paramDotNetConfig))
{
configuration = parser.Arguments[_paramDotNetConfig][0];
}
2018-02-11 22:24:12 +01:00
var platformInfo = GetTargetPlatformInformation.Do(desiredPlatform, specifiedFromCustom);
2018-01-24 22:39:46 +01:00
Console.WriteLine($"Build ASP.NET Core App for {platformInfo.NetCorePublishRid}...");
string tempPath = Path.Combine(Directory.GetCurrentDirectory(), "obj", "desktop", desiredPlatform);
if (Directory.Exists(tempPath) == false)
2017-10-08 23:22:20 +02:00
{
Directory.CreateDirectory(tempPath);
2017-10-08 23:22:20 +02:00
}
2017-10-05 04:44:58 +02:00
2017-10-19 21:31:32 +02:00
Console.WriteLine("Executing dotnet publish in this directory: " + tempPath);
string tempBinPath = Path.Combine(tempPath, "bin");
2018-02-12 21:17:40 +01:00
Console.WriteLine($"Build ASP.NET Core App for {platformInfo.NetCorePublishRid} under {configuration}-Configuration...");
2017-10-11 23:29:58 +02:00
2018-02-12 21:17:40 +01:00
var resultCode = ProcessHelper.CmdExecute($"dotnet publish -r {platformInfo.NetCorePublishRid} -c {configuration} --output \"{tempBinPath}\"", Directory.GetCurrentDirectory());
if (resultCode != 0)
{
2018-09-27 22:48:54 +02:00
Console.WriteLine("Error occurred during dotnet publish: " + resultCode);
return false;
}
2017-10-11 23:29:58 +02:00
DeployEmbeddedElectronFiles.Do(tempPath);
2019-01-15 22:21:25 +01:00
var nodeModulesDirPath = Path.Combine(tempPath, "node_modules");
2017-10-07 01:32:19 +02:00
if (parser.Arguments.ContainsKey(_paramPackageJson))
{
Console.WriteLine("Copying custom package.json.");
File.Copy(parser.Arguments[_paramPackageJson][0], Path.Combine(tempPath, "package.json"), true);
}
2017-11-15 22:58:33 +01:00
var checkForNodeModulesDirPath = Path.Combine(tempPath, "node_modules");
2017-10-05 04:44:58 +02:00
if (Directory.Exists(checkForNodeModulesDirPath) == false || parser.Contains(_paramForceNodeInstall) || parser.Contains(_paramPackageJson))
2019-05-16 01:06:56 +02:00
2019-01-15 22:21:25 +01:00
Console.WriteLine("Start npm install...");
ProcessHelper.CmdExecute("npm install --production", tempPath);
2019-01-15 22:21:25 +01:00
2019-01-09 23:04:56 +01:00
Console.WriteLine("ElectronHostHook handling started...");
string electronhosthookDir = Path.Combine(Directory.GetCurrentDirectory(), "ElectronHostHook");
if (Directory.Exists(electronhosthookDir))
{
string hosthookDir = Path.Combine(tempPath, "ElectronHostHook");
DirectoryCopy.Do(electronhosthookDir, hosthookDir, true, new List<string>() { "node_modules" });
2019-10-07 22:53:50 +02:00
Console.WriteLine("Start npm install for hosthooks...");
2019-10-14 22:44:30 +02:00
ProcessHelper.CmdExecute("npm install", hosthookDir);
2019-01-09 23:04:56 +01:00
// ToDo: Not sure if this runs under linux/macos
ProcessHelper.CmdExecute(@"tsc -p . --sourceMap false", hosthookDir);
2017-10-08 23:56:04 +02:00
}
2017-10-05 04:44:58 +02:00
2017-10-05 21:28:47 +02:00
Console.WriteLine("Build Electron Desktop Application...");
// Specifying an absolute path supercedes a relative path
2017-10-19 21:58:50 +02:00
string buildPath = Path.Combine(Directory.GetCurrentDirectory(), "bin", "desktop");
if (parser.Arguments.ContainsKey(_paramAbsoluteOutput))
{
buildPath = parser.Arguments[_paramAbsoluteOutput][0];
}
else if (parser.Arguments.ContainsKey(_paramOutputDirectory))
{
buildPath = Path.Combine(Directory.GetCurrentDirectory(),parser.Arguments[_paramOutputDirectory][0]);
}
2017-10-19 21:31:32 +02:00
2017-10-05 23:37:52 +02:00
Console.WriteLine("Executing electron magic in this directory: " + buildPath);
2018-02-12 21:17:40 +01:00
string electronArch = "x64";
if (parser.Arguments.ContainsKey(_paramElectronArch))
{
electronArch = parser.Arguments[_paramElectronArch][0];
}
string electronParams = "";
if (parser.Arguments.ContainsKey(_paramElectronParams))
{
2018-02-12 21:23:23 +01:00
electronParams = parser.Arguments[_paramElectronParams][0];
2018-02-12 21:17:40 +01:00
}
// ToDo: Make the same thing easer with native c# - we can save a tmp file in production code :)
Console.WriteLine("Create electron-builder configuration file...");
ProcessHelper.CmdExecute($"node build-helper.js", tempPath);
Console.WriteLine($"Package Electron App for Platform {platformInfo.ElectronPackerPlatform}...");
ProcessHelper.CmdExecute($"electron-builder . --config=./bin/electron-builder.json --{platformInfo.ElectronPackerPlatform} --{electronArch} -c.electronVersion=5.0.8 {electronParams}", tempPath);
2017-10-11 23:29:58 +02:00
2017-10-16 22:58:52 +02:00
Console.WriteLine("... done");
2017-10-05 04:44:58 +02:00
return true;
});
}
}
}