Skip to content

Commit 00eb986

Browse files
author
Syed Adeel Hassan Rizvi
committed
1. Watch enabled successfully.
2. Reloading electron after build.
1 parent 2987e31 commit 00eb986

File tree

7 files changed

+77
-25
lines changed

7 files changed

+77
-25
lines changed

ElectronNET.API/ElectronNET.API.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.0</TargetFramework>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
66
<PackageOutputPath>..\artifacts</PackageOutputPath>
77
<PackageId>ElectronNET.API</PackageId>

ElectronNET.API/WebHostBuilderExtensions.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.AspNetCore.Hosting;
22
using System;
3+
using System.IO;
34

45
namespace ElectronNET.API
56
{
@@ -22,16 +23,27 @@ public static IWebHostBuilder UseElectron(this IWebHostBuilder builder, string[]
2223
{
2324
BridgeSettings.SocketPort = argument.ToUpper().Replace("/ELECTRONPORT=", "");
2425
Console.WriteLine("Use Electron Port: " + BridgeSettings.SocketPort);
25-
} else if(argument.ToUpper().Contains("ELECTRONWEBPORT"))
26+
}
27+
else if (argument.ToUpper().Contains("ELECTRONWEBPORT"))
2628
{
2729
BridgeSettings.WebPort = argument.ToUpper().Replace("/ELECTRONWEBPORT=", "");
2830
}
2931
}
3032

31-
if(HybridSupport.IsElectronActive)
33+
if (HybridSupport.IsElectronActive)
3234
{
33-
builder.UseContentRoot(AppDomain.CurrentDomain.BaseDirectory)
34-
.UseUrls("http://127.0.0.1:" + BridgeSettings.WebPort);
35+
// check for the content folder if its exists in base director otherwise no need to include
36+
// It was used before because we are publishing the project which copies everything to bin folder and contentroot wwwroot was folder there.
37+
// now we have implemented the live reload if app is run using /watch then we need to use the default project path.
38+
if (Directory.Exists($"{AppDomain.CurrentDomain.BaseDirectory}\\wwwroot"))
39+
{
40+
builder.UseContentRoot(AppDomain.CurrentDomain.BaseDirectory)
41+
.UseUrls("http://localhost:" + BridgeSettings.WebPort);
42+
}
43+
else
44+
{
45+
builder.UseUrls("http://localhost:" + BridgeSettings.WebPort);
46+
}
3547
}
3648

3749
return builder;

ElectronNET.CLI/Commands/StartElectronCommand.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,7 @@ public Task<bool> ExecuteAsync()
6262
string tempBinPath = Path.Combine(tempPath, "bin");
6363
var resultCode = 0;
6464

65-
if (parser != null && parser.Contains("watch"))
66-
{
67-
68-
// no need for this code i will remove this before PRS
69-
//if (!Directory.Exists($"{tempBinPath}")) Directory.CreateDirectory(tempBinPath);
70-
//if (!Directory.Exists($"{tempBinPath}\\wwwroot")) resultCode = ProcessHelper.CmdExecute($"mklink /D {tempBinPath}\\wwwroot wwwroot", aspCoreProjectPath);
71-
72-
//if (!File.Exists($"{tempBinPath}\\electron.manifest.json"))
73-
//{
74-
// resultCode = ProcessHelper.CmdExecute($"mklink /h {tempBinPath}\\electron.manifest.json electron.manifest.json", aspCoreProjectPath);
75-
//}
76-
77-
}
78-
else
65+
if (parser != null && !parser.Arguments.ContainsKey("watch"))
7966
{
8067
resultCode = ProcessHelper.CmdExecute($"dotnet publish -r {platformInfo.NetCorePublishRid} --output \"{tempBinPath}\" /p:PublishReadyToRun=true --no-self-contained", aspCoreProjectPath);
8168
}

ElectronNET.Host/api/browserWindows.js

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

ElectronNET.Host/api/browserWindows.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const path = require('path');
33
const windows: Electron.BrowserWindow[] = [];
44
let readyToShowWindowsIds: number[] = [];
55
let window, lastOptions, electronSocket;
6+
let mainWindowId;
67

78
export = (socket: SocketIO.Socket, app: Electron.App) => {
89
electronSocket = socket;
@@ -199,6 +200,23 @@ export = (socket: SocketIO.Socket, app: Electron.App) => {
199200
options = { ...options, webPreferences: { nodeIntegration: true } };
200201
}
201202

203+
// lets not recreate the same window if its already open
204+
let nWindow = null;
205+
206+
if (app.commandLine.hasSwitch('watch')) {
207+
(app as any).on('hot-reload', (id) => {
208+
mainWindowId = id;
209+
nWindow = getWindowById(mainWindowId);
210+
if (nWindow) {
211+
nWindow.reload();
212+
if (loadUrl) {
213+
nWindow.loadURL(loadUrl);
214+
}
215+
return;
216+
}
217+
})
218+
}
219+
202220
window = new BrowserWindow(options);
203221
window.on('ready-to-show', () => {
204222
if (readyToShowWindowsIds.includes(window.id)) {
@@ -245,6 +263,12 @@ export = (socket: SocketIO.Socket, app: Electron.App) => {
245263
console.log('auto clear-cache active for new window.');
246264
}
247265

266+
// set main window id
267+
if (mainWindowId == undefined) {
268+
mainWindowId = window.id;
269+
app.emit("mainWindow", mainWindowId);
270+
}
271+
248272
windows.push(window);
249273
electronSocket.emit('BrowserWindowCreated', window.id);
250274
});

ElectronNET.Host/main.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ let appApi, menu, dialogApi, notification, tray, webContents;
99
let globalShortcut, shellApi, screen, clipboard, autoUpdater;
1010
let commandLine, browserView;
1111
let splashScreen, hostHook;
12+
let mainWindowId;
1213

1314
let manifestJsonFileName = 'electron.manifest.json';
1415
let watchable = false;
@@ -26,7 +27,6 @@ let manifestJsonFilePath = path.join(currentBinPath, manifestJsonFileName);
2627
// if watch is enabled lets change the path
2728
if (watchable) {
2829
currentBinPath = path.join(__dirname, '../../'); // go to project directory
29-
currentBinPath = currentBinPath.substr(0, currentBinPath.length - 1);
3030
manifestJsonFilePath = path.join(currentBinPath, manifestJsonFileName);
3131
}
3232

@@ -61,6 +61,11 @@ app.on('ready', () => {
6161

6262
});
6363

64+
app.on("mainWindow", id => {
65+
mainWindowId = id;
66+
console.log(` Main Window ID = ${id}`);
67+
})
68+
6469
function isSplashScreenEnabled() {
6570
if (manifestJsonFile.hasOwnProperty('splashscreen')) {
6671
if (manifestJsonFile.splashscreen.hasOwnProperty('imageFile')) {
@@ -131,6 +136,11 @@ function startSocketApiBridge(port) {
131136
global['electronsocket'].setMaxListeners(0);
132137
console.log('ASP.NET Core Application connected...', 'global.electronsocket', global['electronsocket'].id, new Date());
133138

139+
// send signal to reload
140+
if (mainWindowId != undefined) {
141+
app.emit("hot-reload", mainWindowId);
142+
}
143+
134144
appApi = require('./api/app')(socket, app);
135145
browserWindows = require('./api/browserWindows')(socket, app);
136146
commandLine = require('./api/commandLine')(socket, app);
@@ -213,14 +223,12 @@ function startAspCoreBackendWithWatch(electronPort) {
213223
function startBackend(aspCoreBackendPort) {
214224
console.log('ASP.NET Core Watch Port: ' + aspCoreBackendPort);
215225
loadURL = `http://localhost:${aspCoreBackendPort}`;
216-
const parameters = ['watch','run', `--project ${currentBinPath}`, `/electronPort=${electronPort}`, `/electronWebPort=${aspCoreBackendPort}`];
226+
const parameters = ['watch', 'run', `/electronPort=${electronPort}`, `/electronWebPort=${aspCoreBackendPort}`];
217227

218228
console.log(currentBinPath);
219229
var options = {
220230
cwd: currentBinPath,
221-
env: {
222-
PATH: process.env.PATH
223-
}
231+
env: process.env,
224232
};
225233
apiProcess = cProcess('dotnet', parameters, options);
226234

ElectronNET.WebApp/ElectronNET.WebApp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22
<PropertyGroup>
3-
<TargetFramework>netcoreapp3.0</TargetFramework>
3+
<TargetFramework>netcoreapp3.1</TargetFramework>
44
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
55
<AspNetCoreModuleName>AspNetCoreModule</AspNetCoreModuleName>
66
<RuntimeIdentifiers>win-x64</RuntimeIdentifiers>

0 commit comments

Comments
 (0)