Skip to content

Commit a788d71

Browse files
Implemented Electrons CommandLine-API, Implemented arguments support, Implemented different manifest file support
1 parent f5e2abf commit a788d71

File tree

13 files changed

+222
-96
lines changed

13 files changed

+222
-96
lines changed

ElectronNET.API/App.cs

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,10 @@ public event Action<bool> AccessibilitySupportChanged
351351

352352
private event Action<bool> _accessibilitySupportChanged;
353353

354-
internal App() { }
354+
internal App()
355+
{
356+
CommandLine = new CommandLine();
357+
}
355358

356359
internal static App Instance
357360
{
@@ -1224,7 +1227,7 @@ public void SetAppUserModelId(string id)
12241227
taskCompletionSource.SetResult((bool)success);
12251228
});
12261229

1227-
BridgeConnector.Socket.Emit("appSetBadgeCount");
1230+
BridgeConnector.Socket.Emit("appSetBadgeCount", count);
12281231

12291232
return await taskCompletionSource.Task
12301233
.ConfigureAwait(false);
@@ -1255,6 +1258,11 @@ public void SetAppUserModelId(string id)
12551258
}
12561259
}
12571260

1261+
/// <summary>
1262+
/// Manipulate the command line arguments for your app that Chromium reads.
1263+
/// </summary>
1264+
public CommandLine CommandLine { get; internal set; }
1265+
12581266
/// <summary>
12591267
/// Whether the current desktop environment is Unity launcher.
12601268
/// </summary>
@@ -1380,39 +1388,6 @@ public void SetAboutPanelOptions(AboutPanelOptions options)
13801388
BridgeConnector.Socket.Emit("appSetAboutPanelOptions", JObject.FromObject(options, _jsonSerializer));
13811389
}
13821390

1383-
/// <summary>
1384-
/// Append a switch (with optional value) to Chromium's command line. Note: This
1385-
/// will not affect process.argv, and is mainly used by developers to control some
1386-
/// low-level Chromium behaviors.
1387-
/// </summary>
1388-
/// <param name="theSwtich">A command-line switch.</param>
1389-
public void CommandLineAppendSwitch(string theSwtich)
1390-
{
1391-
BridgeConnector.Socket.Emit("appCommandLineAppendSwitch", theSwtich);
1392-
}
1393-
1394-
/// <summary>
1395-
/// Append a switch (with optional value) to Chromium's command line. Note: This
1396-
/// will not affect process.argv, and is mainly used by developers to control some
1397-
/// low-level Chromium behaviors.
1398-
/// </summary>
1399-
/// <param name="theSwtich">A command-line switch.</param>
1400-
/// <param name="value">A value for the given switch.</param>
1401-
public void CommandLineAppendSwitch(string theSwtich, string value)
1402-
{
1403-
BridgeConnector.Socket.Emit("appCommandLineAppendSwitch", theSwtich, value);
1404-
}
1405-
1406-
/// <summary>
1407-
/// Append an argument to Chromium's command line. The argument will be quoted
1408-
/// correctly.Note: This will not affect process.argv.
1409-
/// </summary>
1410-
/// <param name="value">The argument to append to the command line.</param>
1411-
public void CommandLineAppendArgument(string value)
1412-
{
1413-
BridgeConnector.Socket.Emit("appCommandLineAppendArgument", value);
1414-
}
1415-
14161391
/// <summary>
14171392
/// When critical is passed, the dock icon will bounce until either the application
14181393
/// becomes active or the request is canceled.When informational is passed, the

ElectronNET.API/CommandLine.cs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
4+
namespace ElectronNET.API
5+
{
6+
/// <summary>
7+
/// Manipulate the command line arguments for your app that Chromium reads.
8+
/// </summary>
9+
public sealed class CommandLine
10+
{
11+
internal CommandLine() { }
12+
13+
internal static CommandLine Instance
14+
{
15+
get
16+
{
17+
if (_commandLine == null)
18+
{
19+
lock (_syncRoot)
20+
{
21+
if (_commandLine == null)
22+
{
23+
_commandLine = new CommandLine();
24+
}
25+
}
26+
}
27+
28+
return _commandLine;
29+
}
30+
}
31+
32+
private static CommandLine _commandLine;
33+
34+
private static object _syncRoot = new object();
35+
36+
/// <summary>
37+
/// Append a switch (with optional value) to Chromium's command line.
38+
/// </summary>
39+
/// <param name="the_switch">A command-line switch, without the leading --</param>
40+
/// <param name="value">(optional) - A value for the given switch</param>
41+
/// <remarks>
42+
/// Note: This will not affect process.argv. The intended usage of this function is to control Chromium's behavior.
43+
/// </remarks>
44+
public void AppendSwitch(string the_switch, string value = "")
45+
{
46+
BridgeConnector.Socket.Emit("appCommandLineAppendSwitch", the_switch, value);
47+
}
48+
49+
/// <summary>
50+
/// Append an argument to Chromium's command line. The argument will be quoted correctly. Switches will precede arguments regardless of appending order.
51+
///
52+
/// If you're appending an argument like <code>--switch=value</code>, consider using <code>appendSwitch('switch', 'value')</code> instead.
53+
/// </summary>
54+
/// <param name="value">The argument to append to the command line</param>
55+
/// <remarks>
56+
/// Note: This will not affect process.argv. The intended usage of this function is to control Chromium's behavior.
57+
/// </remarks>
58+
public void AppendArgument(string value)
59+
{
60+
BridgeConnector.Socket.Emit("appCommandLineAppendArgument", value);
61+
}
62+
63+
/// <summary>
64+
/// Whether the command-line switch is present.
65+
/// </summary>
66+
/// <param name="switchName">A command-line switch</param>
67+
/// <param name="cancellationToken"></param>
68+
/// <returns>Whether the command-line switch is present.</returns>
69+
public async Task<bool> HasSwitchAsync(string switchName, CancellationToken cancellationToken = default(CancellationToken))
70+
{
71+
cancellationToken.ThrowIfCancellationRequested();
72+
73+
var taskCompletionSource = new TaskCompletionSource<bool>();
74+
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
75+
{
76+
BridgeConnector.Socket.On("appCommandLineHasSwitchCompleted", (result) =>
77+
{
78+
BridgeConnector.Socket.Off("appCommandLineHasSwitchCompleted");
79+
taskCompletionSource.SetResult((bool)result);
80+
});
81+
82+
BridgeConnector.Socket.Emit("appCommandLineHasSwitch", switchName);
83+
84+
return await taskCompletionSource.Task.ConfigureAwait(false);
85+
}
86+
}
87+
88+
/// <summary>
89+
/// The command-line switch value.
90+
/// </summary>
91+
/// <param name="switchName">A command-line switch</param>
92+
/// <param name="cancellationToken"></param>
93+
/// <returns>The command-line switch value.</returns>
94+
/// <remarks>
95+
/// Note: When the switch is not present or has no value, it returns empty string.
96+
/// </remarks>
97+
public async Task<string> GetSwitchValueAsync(string switchName, CancellationToken cancellationToken = default(CancellationToken))
98+
{
99+
cancellationToken.ThrowIfCancellationRequested();
100+
101+
var taskCompletionSource = new TaskCompletionSource<string>();
102+
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
103+
{
104+
BridgeConnector.Socket.On("appCommandLineGetSwitchValueCompleted", (result) =>
105+
{
106+
BridgeConnector.Socket.Off("appCommandLineGetSwitchValueCompleted");
107+
taskCompletionSource.SetResult((string)result);
108+
});
109+
110+
BridgeConnector.Socket.Emit("appCommandLineGetSwitchValue", switchName);
111+
112+
return await taskCompletionSource.Task.ConfigureAwait(false);
113+
}
114+
}
115+
}
116+
}

ElectronNET.CLI/Commands/Actions/DeployEmbeddedElectronFiles.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static void Do(string tempPath)
1818
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "ipc.js", "api.");
1919
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "app.js", "api.");
2020
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "browserWindows.js", "api.");
21+
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "commandLine.js", "api.");
2122
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "dialog.js", "api.");
2223
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "menu.js", "api.");
2324
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "notification.js", "api.");

ElectronNET.CLI/Commands/StartElectronCommand.cs

Lines changed: 26 additions & 6 deletions
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;
@@ -21,19 +22,27 @@ public StartElectronCommand(string[] args)
2122
_args = args;
2223
}
2324

25+
private string _aspCoreProjectPath = "project-path";
26+
private string _arguments = "args";
27+
private string _manifest = "manifest";
28+
2429
public Task<bool> ExecuteAsync()
2530
{
2631
return Task.Run(() =>
2732
{
2833
Console.WriteLine("Start Electron Desktop Application...");
2934

35+
SimpleCommandLineParser parser = new SimpleCommandLineParser();
36+
parser.Parse(_args);
37+
3038
string aspCoreProjectPath = "";
3139

32-
if (_args.Length > 0)
40+
if (parser.Arguments.ContainsKey(_aspCoreProjectPath))
3341
{
34-
if (Directory.Exists(_args[0]))
42+
string projectPath = parser.Arguments[_aspCoreProjectPath].First();
43+
if (Directory.Exists(projectPath))
3544
{
36-
aspCoreProjectPath = _args[0];
45+
aspCoreProjectPath = projectPath;
3746
}
3847
}
3948
else
@@ -83,19 +92,30 @@ public Task<bool> ExecuteAsync()
8392
ProcessHelper.CmdExecute(@"npx tsc -p ../../ElectronHostHook", tempPath);
8493
}
8594

86-
string path = Path.Combine(tempPath, "node_modules", ".bin");
95+
string arguments = "";
96+
97+
if (parser.Arguments.ContainsKey(_arguments))
98+
{
99+
arguments = string.Join(' ', parser.Arguments[_arguments]);
100+
}
87101

102+
if (parser.Arguments.ContainsKey(_manifest))
103+
{
104+
arguments += " --manifest=" + parser.Arguments[_manifest].First();
105+
}
88106

107+
string path = Path.Combine(tempPath, "node_modules", ".bin");
89108
bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
109+
90110
if (isWindows)
91111
{
92112
Console.WriteLine("Invoke electron.cmd - in dir: " + path);
93-
ProcessHelper.CmdExecute(@"electron.cmd ""..\..\main.js""", path);
113+
ProcessHelper.CmdExecute(@"electron.cmd ""..\..\main.js"" " + arguments, path);
94114
}
95115
else
96116
{
97117
Console.WriteLine("Invoke electron - in dir: " + path);
98-
ProcessHelper.CmdExecute(@"./electron ""../../main.js""", path);
118+
ProcessHelper.CmdExecute(@"./electron ""../../main.js"" " + arguments, path);
99119
}
100120

101121
return true;

ElectronNET.CLI/ElectronNET.CLI.csproj

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<OutputType>Exe</OutputType>
55
<TargetFramework>netcoreapp3.0</TargetFramework>
66
<AssemblyName>electronize</AssemblyName>
7-
7+
88
<PackageType>DotnetCliTool</PackageType>
99
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1010
<PackageOutputPath>..\artifacts</PackageOutputPath>
@@ -14,8 +14,10 @@
1414
<Authors>Gregor Biswanger, Robert Muehsig</Authors>
1515
<Product>Electron.NET</Product>
1616
<Company />
17-
<Description>Building cross platform electron based desktop apps with .NET Core and ASP.NET Core.
18-
This package contains the dotnet tooling to electronize your application.</Description>
17+
<Description>
18+
Building cross platform electron based desktop apps with .NET Core and ASP.NET Core.
19+
This package contains the dotnet tooling to electronize your application.
20+
</Description>
1921
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2022
<PackageProjectUrl>https://github.com/ElectronNET/Electron.NET/</PackageProjectUrl>
2123
<RepositoryUrl>https://github.com/ElectronNET/Electron.NET/</RepositoryUrl>
@@ -31,63 +33,34 @@ This package contains the dotnet tooling to electronize your application.</Descr
3133
<None Remove="ElectronHost\package-lock.json" />
3234
<None Remove="ElectronHost\package.json" />
3335
</ItemGroup>
34-
36+
3537
<ItemGroup>
3638
<None Include="PackageIcon.png" Pack="true" PackagePath="\"/>
3739
</ItemGroup>
3840

3941
<ItemGroup>
4042
<EmbeddedResource Include="..\ElectronNET.Host\electron.manifest.json" Link="ElectronHost\electron.manifest.json" />
41-
</ItemGroup>
42-
43-
<ItemGroup>
4443
<EmbeddedResource Include="..\ElectronNET.Host\package.json" Link="ElectronHost\package.json" />
45-
</ItemGroup>
46-
47-
<ItemGroup>
4844
<EmbeddedResource Include="..\ElectronNET.Host\main.js" Link="ElectronHost\main.js" />
49-
</ItemGroup>
50-
51-
<ItemGroup>
5245
<EmbeddedResource Include="..\ElectronNET.Host\build-helper.js" Link="ElectronHost\build-helper.js" />
53-
</ItemGroup>
54-
55-
<ItemGroup>
5646
<EmbeddedResource Include="..\ElectronNET.Host\api\ipc.js" Link="ElectronHost\api\ipc.js" />
57-
</ItemGroup>
58-
59-
<ItemGroup>
6047
<EmbeddedResource Include="..\ElectronNET.Host\ElectronHostHook\index.ts" Link="ElectronHost\ElectronHostHook\index.ts" />
61-
<EmbeddedResource Include="..\ElectronNET.Host\ElectronHostHook\connector.ts" Link="ElectronHost\ElectronHostHook\connector.ts" />
62-
<EmbeddedResource Include="..\ElectronNET.Host\ElectronHostHook\tsconfig.json" Link="ElectronHost\ElectronHostHook\tsconfig.json" />
63-
<EmbeddedResource Include="..\ElectronNET.Host\ElectronHostHook\package.json" Link="ElectronHost\ElectronHostHook\package.json" />
64-
<EmbeddedResource Include="..\ElectronNET.Host\ElectronHostHook\.gitignore" Link="ElectronHost\ElectronHostHook\.gitignore" />
65-
</ItemGroup>
66-
67-
<ItemGroup>
48+
<EmbeddedResource Include="..\ElectronNET.Host\ElectronHostHook\connector.ts" Link="ElectronHost\ElectronHostHook\connector.ts" />
49+
<EmbeddedResource Include="..\ElectronNET.Host\ElectronHostHook\tsconfig.json" Link="ElectronHost\ElectronHostHook\tsconfig.json" />
50+
<EmbeddedResource Include="..\ElectronNET.Host\ElectronHostHook\package.json" Link="ElectronHost\ElectronHostHook\package.json" />
51+
<EmbeddedResource Include="..\ElectronNET.Host\ElectronHostHook\.gitignore" Link="ElectronHost\ElectronHostHook\.gitignore" />
6852
<EmbeddedResource Include="..\ElectronNET.Host\splashscreen\index.html" Link="ElectronHost\splashscreen\index.html" />
69-
</ItemGroup>
70-
71-
<ItemGroup>
7253
<EmbeddedResource Include="..\ElectronNET.Host\api\app.js" Link="ElectronHost\api\app.js" />
7354
<EmbeddedResource Include="..\ElectronNET.Host\api\browserWindows.js" Link="ElectronHost\api\browserWindows.js" />
74-
</ItemGroup>
75-
76-
<ItemGroup>
55+
<EmbeddedResource Include="..\ElectronNET.Host\api\commandLine.js" Link="ElectronHost\api\commandLine.js" />
7756
<EmbeddedResource Include="..\ElectronNET.Host\api\dialog.js" Link="ElectronHost\api\dialog.js" />
7857
<EmbeddedResource Include="..\ElectronNET.Host\api\menu.js" Link="ElectronHost\api\menu.js" />
7958
<EmbeddedResource Include="..\ElectronNET.Host\api\notification.js" Link="ElectronHost\api\notification.js" />
8059
<EmbeddedResource Include="..\ElectronNET.Host\api\tray.js" Link="ElectronHost\api\tray.js" />
81-
</ItemGroup>
82-
83-
<ItemGroup>
8460
<EmbeddedResource Include="..\ElectronNET.Host\api\globalShortcut.js" Link="ElectronHost\api\globalShortcut.js" />
8561
<EmbeddedResource Include="..\ElectronNET.Host\api\screen.js" Link="ElectronHost\api\screen.js" />
8662
<EmbeddedResource Include="..\ElectronNET.Host\api\shell.js" Link="ElectronHost\api\shell.js" />
8763
<EmbeddedResource Include="..\ElectronNET.Host\api\webContents.js" Link="ElectronHost\api\webContents.js" />
88-
</ItemGroup>
89-
90-
<ItemGroup>
9164
<EmbeddedResource Include="..\ElectronNET.Host\api\clipboard.js" Link="ElectronHost\api\clipboard.js" />
9265
<EmbeddedResource Include="..\ElectronNET.Host\api\autoUpdater.js" Link="ElectronHost\api\autoUpdater.js" />
9366
</ItemGroup>

ElectronNET.Host/.vscode/launch.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
"name": "Launch Electron App",
1111
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron",
1212
"program": "${workspaceFolder}/main.js",
13-
"sourceMaps": true
13+
"sourceMaps": true,
14+
"args": [
15+
"--test=true",
16+
"--blub=wuhuu"
17+
]
1418
}
1519
]
1620
}

ElectronNET.Host/api/app.js

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)