diff --git a/ElectronNET.CLI/Commands/CommandOption.cs b/ElectronNET.CLI/Commands/CommandOption.cs new file mode 100644 index 0000000..675f571 --- /dev/null +++ b/ElectronNET.CLI/Commands/CommandOption.cs @@ -0,0 +1,53 @@ +namespace ElectronNET.CLI.Commands +{ + /// + /// The definitionn of an option for a command. + /// + public class CommandOption + { + /// + /// An enum for the possible values for an option + /// + public enum CommandOptionValueType { NoValue, StringValue, BoolValue, IntValue, CommaDelimitedList, KeyValuePairs } + + /// + /// The name of the option. + /// + public string Name { get; set; } + + /// + /// The short form of the command line switch. This will start with just one dash e.g. -f for framework + /// + public string ShortSwitch { get; set; } + + /// + /// The full form of the command line switch. This will start with two dashes e.g. --framework + /// + public string Switch { get; set; } + + /// + /// The description of the option + /// + public string Description { get; set; } + + /// + /// The type of value that is expected with this command option + /// + public CommandOptionValueType ValueType { get; set; } + + /// + /// The JSON key used in configuration file.` + /// + public string ConfigFileKey + { + get + { + var key = this.Switch; + if (key.StartsWith("--")) + key = key.Substring(2); + + return key; + } + } + } +} \ No newline at end of file diff --git a/ElectronNET.CLI/Commands/ICommand.cs b/ElectronNET.CLI/Commands/ICommand.cs new file mode 100644 index 0000000..1ea9489 --- /dev/null +++ b/ElectronNET.CLI/Commands/ICommand.cs @@ -0,0 +1,18 @@ +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace ElectronNET.CLI.Commands +{ + /// + /// Interface for commands to implement. + /// + public interface ICommand + { + /// + /// If enabled the tool will prompt for required fields if they are not already given. + /// + bool DisableInteractive { get; set; } + + Task ExecuteAsync(); + } +} diff --git a/ElectronNET.CLI/Commands/StartElectronCommand.cs b/ElectronNET.CLI/Commands/StartElectronCommand.cs new file mode 100644 index 0000000..635e138 --- /dev/null +++ b/ElectronNET.CLI/Commands/StartElectronCommand.cs @@ -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 = " from ASP.NET Core Project."; + public static IList CommandOptions { get; set; } = new List(); + + public bool DisableInteractive { get; set; } + + private string[] _args; + + public StartElectronCommand(string[] args) + { + _args = args; + } + + public Task 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; + }); + } + } +} diff --git a/ElectronNET.CLI/ElectronNET.CLI.csproj b/ElectronNET.CLI/ElectronNET.CLI.csproj index bfc6933..7a7107b 100644 --- a/ElectronNET.CLI/ElectronNET.CLI.csproj +++ b/ElectronNET.CLI/ElectronNET.CLI.csproj @@ -12,8 +12,4 @@ 1.0.0 - - - - diff --git a/ElectronNET.CLI/Program.cs b/ElectronNET.CLI/Program.cs index 79776be..155d32d 100644 --- a/ElectronNET.CLI/Program.cs +++ b/ElectronNET.CLI/Program.cs @@ -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 "); + } + + 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 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(); + } + catch (AmbiguousMatchException) + { + // Catch exception and continue if multiple attributes are found. + } + return attribute?.InformationalVersion; + } } } } diff --git a/ElectronNET.CLI/Properties/launchSettings.json b/ElectronNET.CLI/Properties/launchSettings.json new file mode 100644 index 0000000..a99107e --- /dev/null +++ b/ElectronNET.CLI/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "ElectronNET.CLI": { + "commandName": "Project", + "commandLineArgs": "start \"C:\\Users\\Gregor\\Documents\\Visual Studio 2017\\Projects\\ElectronNET\\ElectronNET.WebApp\"" + } + } +} \ No newline at end of file