Skip to content

Commit d0c52a1

Browse files
Implement HostHook logic to CLI and API. Implement an example in the Web-App.
1 parent f709f65 commit d0c52a1

35 files changed

+656
-4600
lines changed

ElectronNET.API/Electron.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ public static class Electron
6060
/// </summary>
6161
public static Clipboard Clipboard { get { return Clipboard.Instance; } }
6262

63-
63+
/// <summary>
64+
/// Allows you to execute native JavaScript/TypeScript code from the host process.
65+
///
66+
/// It is only possible if the Electron.NET CLI has previously added an
67+
/// ElectronHostHook directory:
68+
/// <c>electronize add HostHook</c>
69+
/// </summary>
6470
public static HostHook HostHook { get { return HostHook.Instance; } }
6571
}
6672
}

ElectronNET.API/HostHook.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66

77
namespace ElectronNET.API
88
{
9+
/// <summary>
10+
/// Allows you to execute native JavaScript/TypeScript code from the host process.
11+
///
12+
/// It is only possible if the Electron.NET CLI has previously added an
13+
/// ElectronHostHook directory:
14+
/// <c>electronize add HostHook</c>
15+
/// </summary>
916
public sealed class HostHook
1017
{
1118
private static HostHook _electronHostHook;
@@ -33,6 +40,11 @@ internal static HostHook Instance
3340
}
3441
}
3542

43+
/// <summary>
44+
/// Execute native JavaScript/TypeScript code.
45+
/// </summary>
46+
/// <param name="socketEventName">Socket name registered on the host.</param>
47+
/// <param name="arguments">Optional parameters.</param>
3648
public void Call(string socketEventName, params dynamic[] arguments)
3749
{
3850
BridgeConnector.Socket.On(socketEventName + "Error" + oneCallguid, (result) =>
@@ -44,6 +56,13 @@ public void Call(string socketEventName, params dynamic[] arguments)
4456
BridgeConnector.Socket.Emit(socketEventName, arguments, oneCallguid);
4557
}
4658

59+
/// <summary>
60+
/// Execute native JavaScript/TypeScript code.
61+
/// </summary>
62+
/// <typeparam name="T">Results from the executed host code.</typeparam>
63+
/// <param name="socketEventName">Socket name registered on the host.</param>
64+
/// <param name="arguments">Optional parameters.</param>
65+
/// <returns></returns>
4766
public Task<T> CallAsync<T>(string socketEventName, params dynamic[] arguments)
4867
{
4968
var taskCompletionSource = new TaskCompletionSource<T>();

ElectronNET.CLI/Commands/Actions/DeployEmbeddedElectronFiles.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Text;
1+
using System.IO;
52

63
namespace ElectronNET.CLI.Commands.Actions
74
{
@@ -11,7 +8,6 @@ public static void Do(string tempPath)
118
{
129
EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "main.js");
1310
EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package.json");
14-
EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package-lock.json");
1511

1612
string hostApiFolder = Path.Combine(tempPath, "api");
1713
if (Directory.Exists(hostApiFolder) == false)

ElectronNET.CLI/Commands/AddCommand.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,17 @@ public Task<bool> ExecuteAsync()
6767
EmbeddedFileHelper.DeployEmbeddedFile(targetFilePath, "connector.ts", "ElectronHostHook.");
6868
EmbeddedFileHelper.DeployEmbeddedFile(targetFilePath, "package.json", "ElectronHostHook.");
6969
EmbeddedFileHelper.DeployEmbeddedFile(targetFilePath, "tsconfig.json", "ElectronHostHook.");
70+
EmbeddedFileHelper.DeployEmbeddedFile(targetFilePath, ".gitignore", "ElectronHostHook.");
7071

7172
// npm for typescript compiler etc.
7273
Console.WriteLine("Start npm install...");
7374
ProcessHelper.CmdExecute("npm install", targetFilePath);
7475

76+
// run typescript compiler
77+
string tscPath = Path.Combine(targetFilePath, "node_modules", ".bin");
78+
// ToDo: Not sure if this runs under linux/macos
79+
ProcessHelper.CmdExecute(@"tsc -p ../../", tscPath);
80+
7581
// search .csproj
7682
Console.WriteLine($"Search your .csproj to add configure CopyToPublishDirectory to 'Never'");
7783
var projectFile = Directory.EnumerateFiles(currentDirectory, "*.csproj", SearchOption.TopDirectoryOnly).FirstOrDefault();

ElectronNET.CLI/Commands/BuildCommand.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4-
using System.Linq;
54
using System.Runtime.InteropServices;
6-
using System.Text;
75
using System.Threading.Tasks;
86
using ElectronNET.CLI.Commands.Actions;
97

@@ -82,17 +80,11 @@ public Task<bool> ExecuteAsync()
8280
return false;
8381
}
8482

85-
86-
8783
DeployEmbeddedElectronFiles.Do(tempPath);
88-
8984
var nodeModulesDirPath = Path.Combine(tempPath, "node_modules");
9085

91-
92-
Console.WriteLine("node_modules missing in: " + checkForNodeModulesDirPath);
93-
9486
Console.WriteLine("Start npm install...");
95-
ProcessHelper.CmdExecute("npm install", tempPath);
87+
ProcessHelper.CmdExecute("npm install --production", tempPath);
9688

9789
Console.WriteLine("Start npm install electron-packager...");
9890

@@ -118,11 +110,11 @@ public Task<bool> ExecuteAsync()
118110
DirectoryCopy.Do(electronhosthookDir, hosthookDir, true, new List<string>() { "node_modules" });
119111

120112
Console.WriteLine("Start npm install for hosthooks...");
121-
ProcessHelper.CmdExecute("npm install", hosthookDir);
113+
ProcessHelper.CmdExecute("npm install --production", hosthookDir);
122114

123115
string tscPath = Path.Combine(tempPath, "node_modules", ".bin");
124116
// ToDo: Not sure if this runs under linux/macos
125-
ProcessHelper.CmdExecute(@"tsc -p ../../ElectronHostHook", tscPath);
117+
ProcessHelper.CmdExecute(@"tsc -p ../../ElectronHostHook --sourceMap false", tscPath);
126118
}
127119

128120
Console.WriteLine("Build Electron Desktop Application...");

ElectronNET.CLI/ElectronNET.CLI.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ This package contains the dotnet tooling to electronize your application.</Descr
4444
</ItemGroup>
4545

4646
<ItemGroup>
47-
<EmbeddedResource Include="..\ElectronNET.Host\package-lock.json" Link="ElectronHost\package-lock.json" />
4847
<EmbeddedResource Include="..\ElectronNET.Host\package.json" Link="ElectronHost\package.json" />
4948
</ItemGroup>
5049

@@ -61,6 +60,7 @@ This package contains the dotnet tooling to electronize your application.</Descr
6160
<EmbeddedResource Include="..\ElectronNET.Host\ElectronHostHook\connector.ts" Link="ElectronHost\ElectronHostHook\connector.ts" />
6261
<EmbeddedResource Include="..\ElectronNET.Host\ElectronHostHook\tsconfig.json" Link="ElectronHost\ElectronHostHook\tsconfig.json" />
6362
<EmbeddedResource Include="..\ElectronNET.Host\ElectronHostHook\package.json" Link="ElectronHost\ElectronHostHook\package.json" />
63+
<EmbeddedResource Include="..\ElectronNET.Host\ElectronHostHook\.gitignore" Link="ElectronHost\ElectronHostHook\.gitignore" />
6464
</ItemGroup>
6565

6666

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
2+
# Created by https://www.gitignore.io/api/node
3+
# Edit at https://www.gitignore.io/?templates=node
4+
5+
### Node ###
6+
# Logs
7+
logs
8+
*.log
9+
npm-debug.log*
10+
yarn-debug.log*
11+
yarn-error.log*
12+
lerna-debug.log*
13+
14+
# Diagnostic reports (https://nodejs.org/api/report.html)
15+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
16+
17+
# Runtime data
18+
pids
19+
*.pid
20+
*.seed
21+
*.pid.lock
22+
23+
# Directory for instrumented libs generated by jscoverage/JSCover
24+
lib-cov
25+
26+
# Coverage directory used by tools like istanbul
27+
coverage
28+
29+
# nyc test coverage
30+
.nyc_output
31+
32+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
33+
.grunt
34+
35+
# Bower dependency directory (https://bower.io/)
36+
bower_components
37+
38+
# node-waf configuration
39+
.lock-wscript
40+
41+
# Compiled binary addons (https://nodejs.org/api/addons.html)
42+
build/Release
43+
44+
# Dependency directories
45+
node_modules/
46+
jspm_packages/
47+
48+
# TypeScript v1 declaration files
49+
typings/
50+
51+
# Optional npm cache directory
52+
.npm
53+
54+
# Optional eslint cache
55+
.eslintcache
56+
57+
# Optional REPL history
58+
.node_repl_history
59+
60+
# Output of 'npm pack'
61+
*.tgz
62+
63+
# Yarn Integrity file
64+
.yarn-integrity
65+
66+
# dotenv environment variables file
67+
.env
68+
.env.test
69+
70+
# parcel-bundler cache (https://parceljs.org/)
71+
.cache
72+
73+
# next.js build output
74+
.next
75+
76+
# nuxt.js build output
77+
.nuxt
78+
79+
# vuepress build output
80+
.vuepress/dist
81+
82+
# Serverless directories
83+
.serverless/
84+
85+
# FuseBox cache
86+
.fusebox/
87+
88+
# DynamoDB Local files
89+
.dynamodb/
90+
91+
# End of https://www.gitignore.io/api/node

ElectronNET.Host/ElectronHostHook/connector.js

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

ElectronNET.Host/ElectronHostHook/connector.js.map

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

ElectronNET.Host/ElectronHostHook/connector.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
export class Connector {
2-
constructor(private socket: SocketIO.Socket, public app: Electron.App) { }
2+
constructor(private socket: SocketIO.Socket,
3+
// @ts-ignore
4+
public app: Electron.App) { }
35

46
on(key: string, javaScriptCode: Function): void {
57
this.socket.on(key, (...args: any[]) => {
68
const id: string = args.pop();
79

810
try {
911
javaScriptCode(...args, (data) => {
10-
if (isNaN(data)) {
11-
throw new Error('Result is NaN');
12-
} else {
12+
if (data) {
1313
this.socket.emit(`${key}Complete${id}`, data);
1414
}
1515
});
1616
} catch (error) {
17-
this.socket.emit(`${key}Error${id}`, 'Host Hook Exception', error);
17+
this.socket.emit(`${key}Error${id}`, `Host Hook Exception`, error);
1818
}
1919
});
2020
}

ElectronNET.Host/ElectronHostHook/index.js

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

ElectronNET.Host/ElectronHostHook/index.js.map

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

ElectronNET.Host/ElectronHostHook/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-ignore
12
import * as Electron from "electron";
23
import { Connector } from "./connector";
34

0 commit comments

Comments
 (0)