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

152 lines
6.4 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;
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 = "<Platform> to build (default is current OS, possible values are: win,osx,linux)";
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;
}
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
string desiredPlatform = "";
if (_args.Length > 0)
{
desiredPlatform = _args[0];
}
2017-10-08 23:22:20 +02:00
string netCorePublishRid = string.Empty;
string electronPackerPlatform = string.Empty;
2017-10-08 23:22:20 +02:00
switch (desiredPlatform)
2017-10-08 23:22:20 +02:00
{
case "win":
2017-11-04 09:25:43 -07:00
netCorePublishRid = "win-x64";
electronPackerPlatform = "win32";
break;
case "osx":
netCorePublishRid = "osx-x64";
electronPackerPlatform = "darwin";
break;
case "linux":
2017-11-04 09:25:43 -07:00
netCorePublishRid = "linux-x64";
electronPackerPlatform = "linux";
break;
default:
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
2017-10-19 21:31:32 +02:00
desiredPlatform = "win";
2017-11-04 09:25:43 -07:00
netCorePublishRid = "win-x64";
electronPackerPlatform = "win32";
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
2017-10-19 21:31:32 +02:00
desiredPlatform = "osx";
netCorePublishRid = "osx-x64";
electronPackerPlatform = "darwin";
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
2017-10-19 21:31:32 +02:00
desiredPlatform = "linux";
2017-11-04 09:25:43 -07:00
netCorePublishRid = "linux-x64";
electronPackerPlatform = "linux";
}
break;
2017-10-08 23:22:20 +02:00
}
2017-10-05 04:44:58 +02:00
2017-10-19 21:31:32 +02:00
string tempPath = Path.Combine(Directory.GetCurrentDirectory(), "obj", "desktop", desiredPlatform);
Console.WriteLine("Executing dotnet publish in this directory: " + tempPath);
string tempBinPath = Path.Combine(tempPath, "bin");
Console.WriteLine($"Build ASP.NET Core App for {netCorePublishRid}...");
ProcessHelper.CmdExecute($"dotnet publish -r {netCorePublishRid} --output \"{tempBinPath}\"", Directory.GetCurrentDirectory());
2017-10-11 23:29:58 +02:00
2017-10-05 21:28:47 +02:00
if (Directory.Exists(tempPath) == false)
2017-10-05 04:44:58 +02:00
{
2017-10-05 21:28:47 +02:00
Directory.CreateDirectory(tempPath);
2017-10-05 04:44:58 +02:00
}
EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "main.js");
EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package.json");
EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package-lock.json");
2017-10-05 04:44:58 +02:00
2017-10-07 01:32:19 +02:00
string hostApiFolder = Path.Combine(tempPath, "api");
if (Directory.Exists(hostApiFolder) == false)
{
Directory.CreateDirectory(hostApiFolder);
}
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "ipc.js", "api.");
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "app.js", "api.");
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "browserWindows.js", "api.");
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "dialog.js", "api.");
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "menu.js", "api.");
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "notification.js", "api.");
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "tray.js", "api.");
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "webContents.js", "api.");
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "globalShortcut.js", "api.");
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "shell.js", "api.");
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "screen.js", "api.");
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "clipboard.js", "api.");
2017-10-07 01:32:19 +02:00
2017-10-05 04:44:58 +02:00
Console.WriteLine("Start npm install...");
2017-10-05 21:28:47 +02:00
ProcessHelper.CmdExecute("npm install", tempPath);
2017-10-05 04:44:58 +02:00
2017-10-05 21:28:47 +02:00
Console.WriteLine("Start npm install electron-packager...");
2017-10-08 23:56:04 +02:00
2017-10-11 23:29:58 +02:00
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
2017-10-08 23:56:04 +02:00
{
2017-10-19 21:46:46 +02:00
// Works proper on Windows...
2017-10-08 23:56:04 +02:00
ProcessHelper.CmdExecute("npm install electron-packager --global", tempPath);
}
else
{
2017-10-09 00:13:04 +02:00
// ToDo: find another solution or document it proper
// GH Issue https://github.com/electron-userland/electron-prebuilt/issues/48
2017-10-15 23:15:42 +02:00
Console.WriteLine("Electron Packager - make sure you invoke 'sudo npm install electron-packager --global' at " + tempPath + " manually. Sry.");
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...");
2017-10-19 21:58:50 +02:00
string buildPath = Path.Combine(Directory.GetCurrentDirectory(), "bin", "desktop");
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);
2017-10-08 23:56:04 +02:00
// ToDo: Need a solution for --asar support
Console.WriteLine($"Package Electron App for Platform {electronPackerPlatform}...");
ProcessHelper.CmdExecute($"electron-packager . --platform={electronPackerPlatform} --arch=x64 --out=\"{buildPath}\" --overwrite", 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;
});
}
2017-10-05 21:28:47 +02:00
2017-10-05 04:44:58 +02:00
}
}