From dd5bfd3961ac353e735ae1aa6f7ff06b8791b2b0 Mon Sep 17 00:00:00 2001 From: Daniel Gidman Date: Mon, 25 Jan 2021 12:13:44 -0600 Subject: [PATCH] Pass dotnet publsh /p: arguments to the publish command use '/p:propertyName=value' or '/property:propertyName=value' to pass in dotnet build property overrides. fixes #523 https://github.com/ElectronNET/Electron.NET/issues/523 --- ElectronNET.CLI/Commands/BuildCommand.cs | 69 +++++++++++++++------- ElectronNET.CLI/SimpleCommandLineParser.cs | 18 +++++- 2 files changed, 66 insertions(+), 21 deletions(-) diff --git a/ElectronNET.CLI/Commands/BuildCommand.cs b/ElectronNET.CLI/Commands/BuildCommand.cs index 8117446..7b10a46 100644 --- a/ElectronNET.CLI/Commands/BuildCommand.cs +++ b/ElectronNET.CLI/Commands/BuildCommand.cs @@ -21,6 +21,7 @@ namespace ElectronNET.CLI.Commands "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 + + "Optional: '/p:[property]' or '/property:[property]' to pass in dotnet publish properties. Example: '/property:Version=1.0.0' to override the FileVersion" + Environment.NewLine + "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 \""; public static IList CommandOptions { get; set; } = new List(); @@ -95,28 +96,18 @@ namespace ElectronNET.CLI.Commands string tempBinPath = Path.Combine(tempPath, "bin"); Console.WriteLine($"Build ASP.NET Core App for {platformInfo.NetCorePublishRid} under {configuration}-Configuration..."); + + var dotNetPublishFlags = GetDotNetPublishFlags(parser); - string publishReadyToRun = "/p:PublishReadyToRun="; - if (parser.Arguments.ContainsKey(_paramPublishReadyToRun)) - { - publishReadyToRun += parser.Arguments[_paramPublishReadyToRun][0]; - } - else - { - publishReadyToRun += "true"; - } + var command = + $"dotnet publish -r {platformInfo.NetCorePublishRid} -c \"{configuration}\" --output \"{tempBinPath}\" {string.Join(' ', dotNetPublishFlags.Select(kvp => $"{kvp.Key}={kvp.Value}"))} --self-contained"; + + // output the command + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine(command); + Console.ResetColor(); - string publishSingleFile = "/p:PublishSingleFile="; - if (parser.Arguments.ContainsKey(_paramPublishSingleFile)) - { - publishSingleFile += parser.Arguments[_paramPublishSingleFile][0]; - } - else - { - publishSingleFile += "true"; - } - - var resultCode = ProcessHelper.CmdExecute($"dotnet publish -r {platformInfo.NetCorePublishRid} -c \"{configuration}\" --output \"{tempBinPath}\" {publishReadyToRun} {publishSingleFile} --self-contained", Directory.GetCurrentDirectory()); + var resultCode = ProcessHelper.CmdExecute(command, Directory.GetCurrentDirectory()); if (resultCode != 0) { @@ -204,5 +195,43 @@ namespace ElectronNET.CLI.Commands return true; }); } + + private Dictionary GetDotNetPublishFlags(SimpleCommandLineParser parser) + { + var dotNetPublishFlags = new Dictionary + { + {"/p:PublishReadyToRun", parser.TryGet(_paramPublishReadyToRun, out var rtr) ? rtr[0] : "true"}, + {"/p:PublishSingleFile", parser.TryGet(_paramPublishSingleFile, out var psf) ? psf[0] : "true"}, + }; + + foreach (var parm in parser.Arguments.Keys.Where(key => key.StartsWith("p:") || key.StartsWith("property:"))) + { + var split = parm.IndexOf('='); + if (split < 0) + { + continue; + } + + var key = $"/{parm.Substring(0, split)}"; + // normalize the key + if (key.StartsWith("/property:")) + { + key = key.Replace("/property:", "/p:"); + } + + var value = parm.Substring(split + 1); + + if (dotNetPublishFlags.ContainsKey(key)) + { + dotNetPublishFlags[key] = value; + } + else + { + dotNetPublishFlags.Add(key, value); + } + } + + return dotNetPublishFlags; + } } } \ No newline at end of file diff --git a/ElectronNET.CLI/SimpleCommandLineParser.cs b/ElectronNET.CLI/SimpleCommandLineParser.cs index e5cf86e..9da5d73 100644 --- a/ElectronNET.CLI/SimpleCommandLineParser.cs +++ b/ElectronNET.CLI/SimpleCommandLineParser.cs @@ -1,4 +1,6 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Linq; namespace ElectronNET.CLI { @@ -29,10 +31,24 @@ namespace ElectronNET.CLI } if (currentName != "") Arguments[currentName] = values.ToArray(); + + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine($"Arguments: \n\t{string.Join("\n\t",Arguments.Keys.Select(i => $"{i} = {string.Join(" ", Arguments[i])}"))}"); + Console.ResetColor(); } public bool Contains(string name) { return Arguments.ContainsKey(name); } + + internal bool TryGet(string key, out string[] value) + { + value = null; + if (!Contains(key)) { + return false; + } + value = Arguments[key]; + return true; + } } } \ No newline at end of file