Skip to content

Commit 30d49ff

Browse files
Support for different manifest files (#340)
1 parent db9d3b1 commit 30d49ff

File tree

7 files changed

+101
-1521
lines changed

7 files changed

+101
-1521
lines changed

ElectronNET.CLI/Commands/BuildCommand.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Runtime.InteropServices;
56
using System.Threading.Tasks;
67
using ElectronNET.CLI.Commands.Actions;
@@ -40,6 +41,7 @@ public BuildCommand(string[] args)
4041
private string _paramAbsoluteOutput = "absolute-path";
4142
private string _paramPackageJson = "package-json";
4243
private string _paramForceNodeInstall = "install-modules";
44+
private string _manifest = "manifest";
4345

4446
public Task<bool> ExecuteAsync()
4547
{
@@ -79,7 +81,13 @@ public Task<bool> ExecuteAsync()
7981
if (Directory.Exists(tempPath) == false)
8082
{
8183
Directory.CreateDirectory(tempPath);
84+
}
85+
else
86+
{
87+
Directory.Delete(tempPath, true);
88+
Directory.CreateDirectory(tempPath);
8289
}
90+
8391

8492
Console.WriteLine("Executing dotnet publish in this directory: " + tempPath);
8593

@@ -157,7 +165,15 @@ public Task<bool> ExecuteAsync()
157165

158166
// ToDo: Make the same thing easer with native c# - we can save a tmp file in production code :)
159167
Console.WriteLine("Create electron-builder configuration file...");
160-
ProcessHelper.CmdExecute($"node build-helper.js", tempPath);
168+
169+
string manifestFileName = "electron.manifest.json";
170+
171+
if(parser.Arguments.ContainsKey(_manifest))
172+
{
173+
manifestFileName = parser.Arguments[_manifest].First();
174+
}
175+
176+
ProcessHelper.CmdExecute($"node build-helper.js " + manifestFileName, tempPath);
161177

162178
Console.WriteLine($"Package Electron App for Platform {platformInfo.ElectronPackerPlatform}...");
163179
ProcessHelper.CmdExecute($"npx electron-builder . --config=./bin/electron-builder.json --{platformInfo.ElectronPackerPlatform} --{electronArch} -c.electronVersion=7.1.2 {electronParams}", tempPath);

ElectronNET.CLI/Commands/InitCommand.cs

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,30 @@ public class InitCommand : ICommand
1616
public const string COMMAND_ARGUMENTS = "<Path> from ASP.NET Core Project.";
1717
public static IList<CommandOption> CommandOptions { get; set; } = new List<CommandOption>();
1818

19-
private const string ConfigName = "electron.manifest.json";
20-
21-
private string[] _args;
19+
private static SimpleCommandLineParser _parser = new SimpleCommandLineParser();
20+
private static string ConfigName = "electron.manifest.json";
21+
private const string DefaultConfigFileName = "electron.manifest.json";
2222

2323
public InitCommand(string[] args)
2424
{
25-
_args = args;
25+
_parser.Parse(args);
2626
}
2727

28+
private static string _aspCoreProjectPath = "project-path";
29+
private static string _manifest = "manifest";
30+
2831
public Task<bool> ExecuteAsync()
2932
{
3033
return Task.Run(() =>
3134
{
3235
string aspCoreProjectPath = "";
3336

34-
if (_args.Length > 0)
37+
if (_parser.Arguments.ContainsKey(_aspCoreProjectPath))
3538
{
36-
if (Directory.Exists(_args[0]))
39+
string projectPath = _parser.Arguments[_aspCoreProjectPath].First();
40+
if (Directory.Exists(projectPath))
3741
{
38-
aspCoreProjectPath = _args[0];
42+
aspCoreProjectPath = projectPath;
3943
}
4044
}
4145
else
@@ -45,7 +49,15 @@ public Task<bool> ExecuteAsync()
4549

4650
var currentDirectory = aspCoreProjectPath;
4751

48-
Console.WriteLine("Adding our config file to your project...");
52+
if(_parser.Arguments.ContainsKey(_manifest))
53+
{
54+
ConfigName = "electron.manifest." + _parser.Arguments[_manifest].First() + ".json";
55+
Console.WriteLine($"Adding your custom {ConfigName} config file to your project...");
56+
}
57+
else
58+
{
59+
Console.WriteLine("Adding our config file to your project...");
60+
}
4961

5062
var targetFilePath = Path.Combine(currentDirectory, ConfigName);
5163

@@ -56,7 +68,7 @@ public Task<bool> ExecuteAsync()
5668
}
5769

5870
// Deploy config file
59-
EmbeddedFileHelper.DeployEmbeddedFile(currentDirectory, ConfigName);
71+
EmbeddedFileHelper.DeployEmbeddedFileToTargetFile(currentDirectory, DefaultConfigFileName, ConfigName);
6072

6173
// search .csproj
6274
Console.WriteLine($"Search your .csproj to add the needed {ConfigName}...");
@@ -99,7 +111,32 @@ private static void EditLaunchSettings(string currentDirectory)
99111

100112
string launchSettingText = File.ReadAllText(launchSettingFile);
101113

102-
if (launchSettingText.Contains("\"executablePath\": \"electronize\"") == false)
114+
if(_parser.Arguments.ContainsKey(_manifest))
115+
{
116+
string manifestName = _parser.Arguments[_manifest].First();
117+
118+
if(launchSettingText.Contains("start /manifest " + ConfigName) == false)
119+
{
120+
StringBuilder debugProfileBuilder = new StringBuilder();
121+
debugProfileBuilder.AppendLine("profiles\": {");
122+
debugProfileBuilder.AppendLine(" \"Electron.NET App - " + manifestName + "\": {");
123+
debugProfileBuilder.AppendLine(" \"commandName\": \"Executable\",");
124+
debugProfileBuilder.AppendLine(" \"executablePath\": \"electronize\",");
125+
debugProfileBuilder.AppendLine(" \"commandLineArgs\": \"start /manifest " + ConfigName + "\",");
126+
debugProfileBuilder.AppendLine(" \"workingDirectory\": \".\"");
127+
debugProfileBuilder.AppendLine(" },");
128+
129+
launchSettingText = launchSettingText.Replace("profiles\": {", debugProfileBuilder.ToString());
130+
File.WriteAllText(launchSettingFile, launchSettingText);
131+
132+
Console.WriteLine($"Debug profile added!");
133+
}
134+
else
135+
{
136+
Console.WriteLine($"Debug profile already existing");
137+
}
138+
}
139+
else if (launchSettingText.Contains("\"executablePath\": \"electronize\"") == false)
103140
{
104141
StringBuilder debugProfileBuilder = new StringBuilder();
105142
debugProfileBuilder.AppendLine("profiles\": {");

ElectronNET.CLI/EmbeddedFileHelper.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,19 @@ public static void DeployEmbeddedFile(string targetPath, string file, string nam
2929
streamFromEmbeddedFile.CopyTo(fileStream);
3030
}
3131
}
32+
33+
public static void DeployEmbeddedFileToTargetFile(string targetPath, string embeddedFile, string targetFile, string namespacePath = "")
34+
{
35+
using (var fileStream = File.Create(Path.Combine(targetPath, targetFile)))
36+
{
37+
var streamFromEmbeddedFile = GetTestResourceFileStream("ElectronHost." + namespacePath + embeddedFile);
38+
if (streamFromEmbeddedFile == null)
39+
{
40+
Console.WriteLine("Error: Couldn't find embedded file: " + embeddedFile);
41+
}
42+
43+
streamFromEmbeddedFile.CopyTo(fileStream);
44+
}
45+
}
3246
}
3347
}

ElectronNET.Host/.vscode/launch.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@
1515
"--test=true",
1616
"--blub=wuhuu"
1717
]
18+
},
19+
{
20+
"type": "node",
21+
"request": "launch",
22+
"name": "Launch build-helper",
23+
"program": "${workspaceFolder}/build-helper.js",
24+
"skipFiles": [
25+
"<node_internals>/**"
26+
],
27+
"args": [
28+
"electron.manifest.json"
29+
]
1830
}
1931
]
2032
}

ElectronNET.Host/build-helper.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
const manifestFileName = process.argv[2];
12
// @ts-ignore
2-
const manifestFile = require('./bin/electron.manifest');
3+
const manifestFile = require('./bin/' + manifestFileName);
34
const fs = require('fs');
45

56
const builderConfiguration = { ...manifestFile.build };
@@ -33,4 +34,11 @@ fs.writeFile('./bin/electron-builder.json', builderConfigurationString, (error)
3334
if(error) {
3435
console.log(error.message);
3536
}
37+
});
38+
39+
const manifestContent = JSON.stringify(manifestFile);
40+
fs.writeFile('./bin/electron.manifest.json', manifestContent, (error) => {
41+
if(error) {
42+
console.log(error.message);
43+
}
3644
});

0 commit comments

Comments
 (0)