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

186 lines
7.3 KiB
C#
Raw Normal View History

2017-10-04 16:24:41 +02:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
2017-10-04 16:24:41 +02:00
using System.Threading.Tasks;
using ElectronNET.CLI.Commands.Actions;
2017-10-04 16:24:41 +02:00
namespace ElectronNET.CLI.Commands
{
public class StartElectronCommand : ICommand
{
public const string COMMAND_NAME = "start";
public const string COMMAND_DESCRIPTION = "Start your ASP.NET Core Application with Electron, without package it as a single exe. Faster for development.";
2017-10-04 16:24:41 +02:00
public const string COMMAND_ARGUMENTS = "<Path> from ASP.NET Core Project.";
public static IList<CommandOption> CommandOptions { get; set; } = new List<CommandOption>();
private string[] _args;
public StartElectronCommand(string[] args)
{
_args = args;
}
private string _aspCoreProjectPath = "project-path";
private string _arguments = "args";
private string _manifest = "manifest";
2020-04-21 19:38:16 +02:00
private string _clearCache = "clear-cache";
private string _paramPublishReadyToRun = "PublishReadyToRun";
private string _paramPublishSingleFile = "PublishSingleFile";
private string _paramDotNetConfig = "dotnet-configuration";
private string _paramTarget = "target";
2017-10-04 16:24:41 +02:00
public Task<bool> ExecuteAsync()
{
return Task.Run(() =>
{
2017-10-05 21:28:47 +02:00
Console.WriteLine("Start Electron Desktop Application...");
2017-10-04 16:24:41 +02:00
SimpleCommandLineParser parser = new SimpleCommandLineParser();
parser.Parse(_args);
2017-10-04 16:24:41 +02:00
string aspCoreProjectPath = "";
if (parser.Arguments.ContainsKey(_aspCoreProjectPath))
2017-10-04 16:24:41 +02:00
{
string projectPath = parser.Arguments[_aspCoreProjectPath].First();
if (Directory.Exists(projectPath))
2017-10-04 16:24:41 +02:00
{
aspCoreProjectPath = projectPath;
2017-10-04 16:24:41 +02:00
}
}
else
{
aspCoreProjectPath = Directory.GetCurrentDirectory();
}
string tempPath = Path.Combine(aspCoreProjectPath, "obj", "Host");
if (Directory.Exists(tempPath) == false)
{
Directory.CreateDirectory(tempPath);
}
string tempBinPath = Path.Combine(tempPath, "bin");
2020-04-30 22:31:12 +10:00
var resultCode = 0;
string publishReadyToRun = "/p:PublishReadyToRun=";
if (parser.Arguments.ContainsKey(_paramPublishReadyToRun))
{
publishReadyToRun += parser.Arguments[_paramPublishReadyToRun][0];
}
else
{
publishReadyToRun += "true";
}
string publishSingleFile = "/p:PublishSingleFile=";
if (parser.Arguments.ContainsKey(_paramPublishSingleFile))
{
publishSingleFile += parser.Arguments[_paramPublishSingleFile][0];
}
else
{
publishSingleFile += "true";
}
// If target is specified as a command line argument, use it.
// Format is the same as the build command.
// If target is not specified, autodetect it.
var platformInfo = GetTargetPlatformInformation.Do(string.Empty, string.Empty);
if (parser.Arguments.ContainsKey(_paramTarget))
{
var desiredPlatform = parser.Arguments[_paramTarget][0];
string specifiedFromCustom = string.Empty;
if (desiredPlatform == "custom" && parser.Arguments[_paramTarget].Length > 1)
{
specifiedFromCustom = parser.Arguments[_paramTarget][1];
}
platformInfo = GetTargetPlatformInformation.Do(desiredPlatform, specifiedFromCustom);
}
string configuration = "Debug";
if (parser.Arguments.ContainsKey(_paramDotNetConfig))
{
configuration = parser.Arguments[_paramDotNetConfig][0];
}
if (parser != null && !parser.Arguments.ContainsKey("watch"))
2020-04-30 22:31:12 +10:00
{
resultCode = ProcessHelper.CmdExecute($"dotnet publish -r {platformInfo.NetCorePublishRid} -c \"{configuration}\" --output \"{tempBinPath}\" {publishReadyToRun} {publishSingleFile} --no-self-contained", aspCoreProjectPath);
2020-04-30 22:31:12 +10:00
}
if (resultCode != 0)
{
2018-09-27 22:48:54 +02:00
Console.WriteLine("Error occurred during dotnet publish: " + resultCode);
return false;
}
2017-10-04 16:24:41 +02:00
DeployEmbeddedElectronFiles.Do(tempPath);
2017-10-07 01:32:19 +02:00
2019-01-15 22:21:25 +01:00
var nodeModulesDirPath = Path.Combine(tempPath, "node_modules");
2019-01-15 22:21:25 +01:00
Console.WriteLine("node_modules missing in: " + nodeModulesDirPath);
2019-01-15 22:21:25 +01:00
Console.WriteLine("Start npm install...");
ProcessHelper.CmdExecute("npm install", tempPath);
2017-10-04 16:24:41 +02: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-03 23:36:21 +02:00
Console.WriteLine("Start npm install for typescript & hosthooks...");
2019-01-09 23:04:56 +01:00
ProcessHelper.CmdExecute("npm install", hosthookDir);
// ToDo: Not sure if this runs under linux/macos
ProcessHelper.CmdExecute(@"npx tsc -p ../../ElectronHostHook", tempPath);
2019-01-09 23:04:56 +01:00
}
string arguments = "";
if (parser.Arguments.ContainsKey(_arguments))
{
arguments = string.Join(' ', parser.Arguments[_arguments]);
}
if (parser.Arguments.ContainsKey(_manifest))
{
arguments += " --manifest=" + parser.Arguments[_manifest].First();
}
2019-01-09 23:04:56 +01:00
2020-04-21 19:38:16 +02:00
if (parser.Arguments.ContainsKey(_clearCache))
{
arguments += " --clear-cache=true";
}
2020-04-30 22:31:12 +10:00
if (parser.Arguments.ContainsKey("watch"))
{
arguments += " --watch=true";
}
string path = Path.Combine(tempPath, "node_modules", ".bin");
bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
if (isWindows)
{
Console.WriteLine("Invoke electron.cmd - in dir: " + path);
ProcessHelper.CmdExecute(@"electron.cmd ""..\..\main.js"" " + arguments, path);
2020-04-30 22:31:12 +10:00
}
else
{
Console.WriteLine("Invoke electron - in dir: " + path);
ProcessHelper.CmdExecute(@"./electron ""../../main.js"" " + arguments, path);
}
2017-10-04 16:24:41 +02:00
return true;
});
}
}
}