imeplement StartElectronCommand design

This commit is contained in:
Gregor Biswanger
2017-10-04 16:24:41 +02:00
parent 25834abc2b
commit 2b93201383
6 changed files with 295 additions and 26 deletions

View File

@@ -0,0 +1,53 @@
namespace ElectronNET.CLI.Commands
{
/// <summary>
/// The definitionn of an option for a command.
/// </summary>
public class CommandOption
{
/// <summary>
/// An enum for the possible values for an option
/// </summary>
public enum CommandOptionValueType { NoValue, StringValue, BoolValue, IntValue, CommaDelimitedList, KeyValuePairs }
/// <summary>
/// The name of the option.
/// </summary>
public string Name { get; set; }
/// <summary>
/// The short form of the command line switch. This will start with just one dash e.g. -f for framework
/// </summary>
public string ShortSwitch { get; set; }
/// <summary>
/// The full form of the command line switch. This will start with two dashes e.g. --framework
/// </summary>
public string Switch { get; set; }
/// <summary>
/// The description of the option
/// </summary>
public string Description { get; set; }
/// <summary>
/// The type of value that is expected with this command option
/// </summary>
public CommandOptionValueType ValueType { get; set; }
/// <summary>
/// The JSON key used in configuration file.`
/// </summary>
public string ConfigFileKey
{
get
{
var key = this.Switch;
if (key.StartsWith("--"))
key = key.Substring(2);
return key;
}
}
}
}

View File

@@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ElectronNET.CLI.Commands
{
/// <summary>
/// Interface for commands to implement.
/// </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();
}
}

View File

@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
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.";
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)
{
_args = args;
}
public Task<bool> ExecuteAsync()
{
return Task.Run(() =>
{
Console.WriteLine("Start Electron...");
string aspCoreProjectPath = "";
string electronHostProjectPath = "";
if (_args.Length > 0)
{
if (Directory.Exists(_args[0]))
{
aspCoreProjectPath = _args[0];
}
}
else
{
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;
cmd.Start();
cmd.StandardInput.WriteLine("cd " + aspCoreProjectPath);
cmd.StandardInput.Flush();
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();
}
return true;
});
}
}
}

View File

@@ -12,8 +12,4 @@
<Version>1.0.0</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.0.0" />
</ItemGroup>
</Project>

View File

@@ -1,38 +1,157 @@
using McMaster.Extensions.CommandLineUtils;
using ElectronNET.CLI.Commands;
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace ElectronNET.CLI
{
class Program
{
static int Main(string[] args)
static void Main(string[] args)
{
var app = new CommandLineApplication();
app.Name = "electronnet";
app.HelpOption("-?|-h|--help");
app.OnExecute(() =>
if (args.Length == 0)
{
Console.WriteLine("Hello World!");
return 0;
});
PrintUsageHeader();
PrintUsage();
Environment.Exit(-1);
}
app.Command("start", (command) => {
command.Description = "Start ASP.NET Core project with Electron.";
command.HelpOption("-?|-h|--help");
ICommand command = null;
command.Argument("path", "Path to the ASP.NET Core project");
switch (args[0])
{
case StartElectronCommand.COMMAND_NAME:
command = new StartElectronCommand(args.Skip(1).ToArray());
break;
case "--help":
case "--h":
case "help":
PrintUsageHeader();
command.OnExecute(() => {
Console.WriteLine(Directory.GetCurrentDirectory());
Console.WriteLine(command.Arguments[0].Value);
});
if (args.Length > 1)
PrintUsage(args[1]);
else
PrintUsage();
break;
default:
Console.Error.WriteLine($"Unknown command {args[0]}");
PrintUsage();
Environment.Exit(-1);
break;
}
});
if (command != null)
{
var success = command.ExecuteAsync().Result;
if (!success)
{
Environment.Exit(-1);
}
}
}
return app.Execute(args);
private static void PrintUsageHeader()
{
var sb = new StringBuilder("Electron.NET Tools");
var version = Version;
if (!string.IsNullOrEmpty(version))
{
sb.Append($" ({version})");
}
Console.WriteLine(sb.ToString());
Console.WriteLine("Project Home: https://github.com/GregorBiswanger/ElectronNET");
Console.WriteLine("\t");
}
private static void PrintUsage()
{
const int NAME_WIDTH = 23;
Console.WriteLine("\t");
Console.WriteLine("Commands to start the Electron Application:");
Console.WriteLine("\t");
Console.WriteLine($"\t{StartElectronCommand.COMMAND_NAME.PadRight(NAME_WIDTH)} {StartElectronCommand.COMMAND_DESCRIPTION}");
Console.WriteLine("\t");
Console.WriteLine("\t");
Console.WriteLine("To get help on individual commands execute:");
Console.WriteLine("\tdotnet electronize help <command>");
}
private static void PrintUsage(string command)
{
switch (command)
{
case StartElectronCommand.COMMAND_NAME:
PrintUsage(StartElectronCommand.COMMAND_NAME, StartElectronCommand.COMMAND_DESCRIPTION, StartElectronCommand.CommandOptions, StartElectronCommand.COMMAND_ARGUMENTS);
break;
default:
Console.Error.WriteLine($"Unknown command {command}");
PrintUsage();
break;
}
}
private static void PrintUsage(string command, string description, IList<CommandOption> options, string arguments)
{
const int INDENT = 3;
Console.WriteLine($"{command}: ");
Console.WriteLine($"{new string(' ', INDENT)}{description}");
Console.WriteLine("\t");
if (!string.IsNullOrEmpty(arguments))
{
Console.WriteLine($"{new string(' ', INDENT)}dotnet electronize {command} [arguments] [options]");
Console.WriteLine($"{new string(' ', INDENT)}Arguments:");
Console.WriteLine($"{new string(' ', INDENT * 2)}{arguments}");
}
else
{
Console.WriteLine($"{new string(' ', INDENT)}dotnet electronize {command} [options]");
}
const int SWITCH_COLUMN_WIDTH = 40;
Console.WriteLine($"{new string(' ', INDENT)}Options:");
foreach (var option in options)
{
StringBuilder stringBuilder = new StringBuilder();
if (option.ShortSwitch != null)
{
stringBuilder.Append($"{option.ShortSwitch.PadRight(6)} | ");
}
stringBuilder.Append($"{option.Switch}");
if (stringBuilder.Length < SWITCH_COLUMN_WIDTH)
{
stringBuilder.Append(new string(' ', SWITCH_COLUMN_WIDTH - stringBuilder.Length));
}
stringBuilder.Append(option.Description);
Console.WriteLine($"{new string(' ', INDENT * 2)}{stringBuilder.ToString()}");
}
}
private static string Version
{
get
{
AssemblyInformationalVersionAttribute attribute = null;
try
{
attribute = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>();
}
catch (AmbiguousMatchException)
{
// Catch exception and continue if multiple attributes are found.
}
return attribute?.InformationalVersion;
}
}
}
}

View File

@@ -0,0 +1,8 @@
{
"profiles": {
"ElectronNET.CLI": {
"commandName": "Project",
"commandLineArgs": "start \"C:\\Users\\Gregor\\Documents\\Visual Studio 2017\\Projects\\ElectronNET\\ElectronNET.WebApp\""
}
}
}