Skip to content

Commit dd5bfd3

Browse files
committed
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 #523
1 parent 5d90786 commit dd5bfd3

File tree

2 files changed

+66
-21
lines changed

2 files changed

+66
-21
lines changed

ElectronNET.CLI/Commands/BuildCommand.cs

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class BuildCommand : ICommand
2121
"Optional: '/absolute-path to specify and absolute path for output." + Environment.NewLine +
2222
"Optional: '/package-json' to specify a custom package.json file." + Environment.NewLine +
2323
"Optional: '/install-modules' to force node module install. Implied by '/package-json'" + Environment.NewLine +
24+
"Optional: '/p:[property]' or '/property:[property]' to pass in dotnet publish properties. Example: '/property:Version=1.0.0' to override the FileVersion" + Environment.NewLine +
2425
"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 \"";
2526

2627
public static IList<CommandOption> CommandOptions { get; set; } = new List<CommandOption>();
@@ -95,28 +96,18 @@ public Task<bool> ExecuteAsync()
9596
string tempBinPath = Path.Combine(tempPath, "bin");
9697

9798
Console.WriteLine($"Build ASP.NET Core App for {platformInfo.NetCorePublishRid} under {configuration}-Configuration...");
99+
100+
var dotNetPublishFlags = GetDotNetPublishFlags(parser);
98101

99-
string publishReadyToRun = "/p:PublishReadyToRun=";
100-
if (parser.Arguments.ContainsKey(_paramPublishReadyToRun))
101-
{
102-
publishReadyToRun += parser.Arguments[_paramPublishReadyToRun][0];
103-
}
104-
else
105-
{
106-
publishReadyToRun += "true";
107-
}
102+
var command =
103+
$"dotnet publish -r {platformInfo.NetCorePublishRid} -c \"{configuration}\" --output \"{tempBinPath}\" {string.Join(' ', dotNetPublishFlags.Select(kvp => $"{kvp.Key}={kvp.Value}"))} --self-contained";
104+
105+
// output the command
106+
Console.ForegroundColor = ConsoleColor.Green;
107+
Console.WriteLine(command);
108+
Console.ResetColor();
108109

109-
string publishSingleFile = "/p:PublishSingleFile=";
110-
if (parser.Arguments.ContainsKey(_paramPublishSingleFile))
111-
{
112-
publishSingleFile += parser.Arguments[_paramPublishSingleFile][0];
113-
}
114-
else
115-
{
116-
publishSingleFile += "true";
117-
}
118-
119-
var resultCode = ProcessHelper.CmdExecute($"dotnet publish -r {platformInfo.NetCorePublishRid} -c \"{configuration}\" --output \"{tempBinPath}\" {publishReadyToRun} {publishSingleFile} --self-contained", Directory.GetCurrentDirectory());
110+
var resultCode = ProcessHelper.CmdExecute(command, Directory.GetCurrentDirectory());
120111

121112
if (resultCode != 0)
122113
{
@@ -204,5 +195,43 @@ public Task<bool> ExecuteAsync()
204195
return true;
205196
});
206197
}
198+
199+
private Dictionary<string, string> GetDotNetPublishFlags(SimpleCommandLineParser parser)
200+
{
201+
var dotNetPublishFlags = new Dictionary<string, string>
202+
{
203+
{"/p:PublishReadyToRun", parser.TryGet(_paramPublishReadyToRun, out var rtr) ? rtr[0] : "true"},
204+
{"/p:PublishSingleFile", parser.TryGet(_paramPublishSingleFile, out var psf) ? psf[0] : "true"},
205+
};
206+
207+
foreach (var parm in parser.Arguments.Keys.Where(key => key.StartsWith("p:") || key.StartsWith("property:")))
208+
{
209+
var split = parm.IndexOf('=');
210+
if (split < 0)
211+
{
212+
continue;
213+
}
214+
215+
var key = $"/{parm.Substring(0, split)}";
216+
// normalize the key
217+
if (key.StartsWith("/property:"))
218+
{
219+
key = key.Replace("/property:", "/p:");
220+
}
221+
222+
var value = parm.Substring(split + 1);
223+
224+
if (dotNetPublishFlags.ContainsKey(key))
225+
{
226+
dotNetPublishFlags[key] = value;
227+
}
228+
else
229+
{
230+
dotNetPublishFlags.Add(key, value);
231+
}
232+
}
233+
234+
return dotNetPublishFlags;
235+
}
207236
}
208237
}

ElectronNET.CLI/SimpleCommandLineParser.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24

35
namespace ElectronNET.CLI
46
{
@@ -29,10 +31,24 @@ public void Parse(string[] args)
2931
}
3032
if (currentName != "")
3133
Arguments[currentName] = values.ToArray();
34+
35+
Console.ForegroundColor = ConsoleColor.Green;
36+
Console.WriteLine($"Arguments: \n\t{string.Join("\n\t",Arguments.Keys.Select(i => $"{i} = {string.Join(" ", Arguments[i])}"))}");
37+
Console.ResetColor();
3238
}
3339
public bool Contains(string name)
3440
{
3541
return Arguments.ContainsKey(name);
3642
}
43+
44+
internal bool TryGet(string key, out string[] value)
45+
{
46+
value = null;
47+
if (!Contains(key)) {
48+
return false;
49+
}
50+
value = Arguments[key];
51+
return true;
52+
}
3753
}
3854
}

0 commit comments

Comments
 (0)