From 00eb9869dc16eae47a3d424b9b448da5e35e8751 Mon Sep 17 00:00:00 2001 From: Syed Adeel Hassan Rizvi Date: Fri, 1 May 2020 17:29:56 +1000 Subject: [PATCH] 1. Watch enabled successfully. 2. Reloading electron after build. --- ElectronNET.API/ElectronNET.API.csproj | 2 +- ElectronNET.API/WebHostBuilderExtensions.cs | 20 ++++++++++++---- .../Commands/StartElectronCommand.cs | 15 +----------- ElectronNET.Host/api/browserWindows.js | 21 ++++++++++++++++ ElectronNET.Host/api/browserWindows.ts | 24 +++++++++++++++++++ ElectronNET.Host/main.js | 18 ++++++++++---- ElectronNET.WebApp/ElectronNET.WebApp.csproj | 2 +- 7 files changed, 77 insertions(+), 25 deletions(-) diff --git a/ElectronNET.API/ElectronNET.API.csproj b/ElectronNET.API/ElectronNET.API.csproj index 5d54631..03bc805 100644 --- a/ElectronNET.API/ElectronNET.API.csproj +++ b/ElectronNET.API/ElectronNET.API.csproj @@ -1,7 +1,7 @@  - netcoreapp3.0 + netcoreapp3.1 true ..\artifacts ElectronNET.API diff --git a/ElectronNET.API/WebHostBuilderExtensions.cs b/ElectronNET.API/WebHostBuilderExtensions.cs index f458000..8680cf6 100644 --- a/ElectronNET.API/WebHostBuilderExtensions.cs +++ b/ElectronNET.API/WebHostBuilderExtensions.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Hosting; using System; +using System.IO; namespace ElectronNET.API { @@ -22,16 +23,27 @@ namespace ElectronNET.API { BridgeSettings.SocketPort = argument.ToUpper().Replace("/ELECTRONPORT=", ""); Console.WriteLine("Use Electron Port: " + BridgeSettings.SocketPort); - } else if(argument.ToUpper().Contains("ELECTRONWEBPORT")) + } + else if (argument.ToUpper().Contains("ELECTRONWEBPORT")) { BridgeSettings.WebPort = argument.ToUpper().Replace("/ELECTRONWEBPORT=", ""); } } - if(HybridSupport.IsElectronActive) + if (HybridSupport.IsElectronActive) { - builder.UseContentRoot(AppDomain.CurrentDomain.BaseDirectory) - .UseUrls("http://127.0.0.1:" + BridgeSettings.WebPort); + // check for the content folder if its exists in base director otherwise no need to include + // It was used before because we are publishing the project which copies everything to bin folder and contentroot wwwroot was folder there. + // now we have implemented the live reload if app is run using /watch then we need to use the default project path. + if (Directory.Exists($"{AppDomain.CurrentDomain.BaseDirectory}\\wwwroot")) + { + builder.UseContentRoot(AppDomain.CurrentDomain.BaseDirectory) + .UseUrls("http://localhost:" + BridgeSettings.WebPort); + } + else + { + builder.UseUrls("http://localhost:" + BridgeSettings.WebPort); + } } return builder; diff --git a/ElectronNET.CLI/Commands/StartElectronCommand.cs b/ElectronNET.CLI/Commands/StartElectronCommand.cs index fb9a76f..2340423 100644 --- a/ElectronNET.CLI/Commands/StartElectronCommand.cs +++ b/ElectronNET.CLI/Commands/StartElectronCommand.cs @@ -62,20 +62,7 @@ namespace ElectronNET.CLI.Commands string tempBinPath = Path.Combine(tempPath, "bin"); var resultCode = 0; - if (parser != null && parser.Contains("watch")) - { - - // no need for this code i will remove this before PRS - //if (!Directory.Exists($"{tempBinPath}")) Directory.CreateDirectory(tempBinPath); - //if (!Directory.Exists($"{tempBinPath}\\wwwroot")) resultCode = ProcessHelper.CmdExecute($"mklink /D {tempBinPath}\\wwwroot wwwroot", aspCoreProjectPath); - - //if (!File.Exists($"{tempBinPath}\\electron.manifest.json")) - //{ - // resultCode = ProcessHelper.CmdExecute($"mklink /h {tempBinPath}\\electron.manifest.json electron.manifest.json", aspCoreProjectPath); - //} - - } - else + if (parser != null && !parser.Arguments.ContainsKey("watch")) { resultCode = ProcessHelper.CmdExecute($"dotnet publish -r {platformInfo.NetCorePublishRid} --output \"{tempBinPath}\" /p:PublishReadyToRun=true --no-self-contained", aspCoreProjectPath); } diff --git a/ElectronNET.Host/api/browserWindows.js b/ElectronNET.Host/api/browserWindows.js index 7f3ebd3..ea42094 100644 --- a/ElectronNET.Host/api/browserWindows.js +++ b/ElectronNET.Host/api/browserWindows.js @@ -4,6 +4,7 @@ const path = require('path'); const windows = []; let readyToShowWindowsIds = []; let window, lastOptions, electronSocket; +let mainWindowId; module.exports = (socket, app) => { electronSocket = socket; socket.on('register-browserWindow-ready-to-show', (id) => { @@ -168,6 +169,21 @@ module.exports = (socket, app) => { else if (!options.webPreferences) { options = { ...options, webPreferences: { nodeIntegration: true } }; } + // lets not recreate the same window if its already open + let nWindow = null; + if (app.commandLine.hasSwitch('watch')) { + app.on('hot-reload', (id) => { + mainWindowId = id; + nWindow = getWindowById(mainWindowId); + if (nWindow) { + nWindow.reload(); + if (loadUrl) { + nWindow.loadURL(loadUrl); + } + return; + } + }); + } window = new electron_1.BrowserWindow(options); window.on('ready-to-show', () => { if (readyToShowWindowsIds.includes(window.id)) { @@ -209,6 +225,11 @@ module.exports = (socket, app) => { window.webContents.session.clearCache(); console.log('auto clear-cache active for new window.'); } + // set main window id + if (mainWindowId == undefined) { + mainWindowId = window.id; + app.emit("mainWindow", mainWindowId); + } windows.push(window); electronSocket.emit('BrowserWindowCreated', window.id); }); diff --git a/ElectronNET.Host/api/browserWindows.ts b/ElectronNET.Host/api/browserWindows.ts index 7832b7a..12fda60 100644 --- a/ElectronNET.Host/api/browserWindows.ts +++ b/ElectronNET.Host/api/browserWindows.ts @@ -3,6 +3,7 @@ const path = require('path'); const windows: Electron.BrowserWindow[] = []; let readyToShowWindowsIds: number[] = []; let window, lastOptions, electronSocket; +let mainWindowId; export = (socket: SocketIO.Socket, app: Electron.App) => { electronSocket = socket; @@ -199,6 +200,23 @@ export = (socket: SocketIO.Socket, app: Electron.App) => { options = { ...options, webPreferences: { nodeIntegration: true } }; } + // lets not recreate the same window if its already open + let nWindow = null; + + if (app.commandLine.hasSwitch('watch')) { + (app as any).on('hot-reload', (id) => { + mainWindowId = id; + nWindow = getWindowById(mainWindowId); + if (nWindow) { + nWindow.reload(); + if (loadUrl) { + nWindow.loadURL(loadUrl); + } + return; + } + }) + } + window = new BrowserWindow(options); window.on('ready-to-show', () => { if (readyToShowWindowsIds.includes(window.id)) { @@ -245,6 +263,12 @@ export = (socket: SocketIO.Socket, app: Electron.App) => { console.log('auto clear-cache active for new window.'); } + // set main window id + if (mainWindowId == undefined) { + mainWindowId = window.id; + app.emit("mainWindow", mainWindowId); + } + windows.push(window); electronSocket.emit('BrowserWindowCreated', window.id); }); diff --git a/ElectronNET.Host/main.js b/ElectronNET.Host/main.js index b4e993d..aa976e2 100644 --- a/ElectronNET.Host/main.js +++ b/ElectronNET.Host/main.js @@ -9,6 +9,7 @@ let appApi, menu, dialogApi, notification, tray, webContents; let globalShortcut, shellApi, screen, clipboard, autoUpdater; let commandLine, browserView; let splashScreen, hostHook; +let mainWindowId; let manifestJsonFileName = 'electron.manifest.json'; let watchable = false; @@ -26,7 +27,6 @@ let manifestJsonFilePath = path.join(currentBinPath, manifestJsonFileName); // if watch is enabled lets change the path if (watchable) { currentBinPath = path.join(__dirname, '../../'); // go to project directory - currentBinPath = currentBinPath.substr(0, currentBinPath.length - 1); manifestJsonFilePath = path.join(currentBinPath, manifestJsonFileName); } @@ -61,6 +61,11 @@ app.on('ready', () => { }); +app.on("mainWindow", id => { + mainWindowId = id; + console.log(` Main Window ID = ${id}`); +}) + function isSplashScreenEnabled() { if (manifestJsonFile.hasOwnProperty('splashscreen')) { if (manifestJsonFile.splashscreen.hasOwnProperty('imageFile')) { @@ -131,6 +136,11 @@ function startSocketApiBridge(port) { global['electronsocket'].setMaxListeners(0); console.log('ASP.NET Core Application connected...', 'global.electronsocket', global['electronsocket'].id, new Date()); + // send signal to reload + if (mainWindowId != undefined) { + app.emit("hot-reload", mainWindowId); + } + appApi = require('./api/app')(socket, app); browserWindows = require('./api/browserWindows')(socket, app); commandLine = require('./api/commandLine')(socket, app); @@ -213,14 +223,12 @@ function startAspCoreBackendWithWatch(electronPort) { function startBackend(aspCoreBackendPort) { console.log('ASP.NET Core Watch Port: ' + aspCoreBackendPort); loadURL = `http://localhost:${aspCoreBackendPort}`; - const parameters = ['watch','run', `--project ${currentBinPath}`, `/electronPort=${electronPort}`, `/electronWebPort=${aspCoreBackendPort}`]; + const parameters = ['watch', 'run', `/electronPort=${electronPort}`, `/electronWebPort=${aspCoreBackendPort}`]; console.log(currentBinPath); var options = { cwd: currentBinPath, - env: { - PATH: process.env.PATH - } + env: process.env, }; apiProcess = cProcess('dotnet', parameters, options); diff --git a/ElectronNET.WebApp/ElectronNET.WebApp.csproj b/ElectronNET.WebApp/ElectronNET.WebApp.csproj index f880723..729ae26 100644 --- a/ElectronNET.WebApp/ElectronNET.WebApp.csproj +++ b/ElectronNET.WebApp/ElectronNET.WebApp.csproj @@ -1,6 +1,6 @@  - netcoreapp3.0 + netcoreapp3.1 OutOfProcess AspNetCoreModule win-x64