1. Watch enabled successfully.

2. Reloading electron after build.
This commit is contained in:
Syed Adeel Hassan Rizvi
2020-05-01 17:29:56 +10:00
parent 2987e3143d
commit 00eb9869dc
7 changed files with 77 additions and 25 deletions

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageOutputPath>..\artifacts</PackageOutputPath>
<PackageId>ElectronNET.API</PackageId>

View File

@@ -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;

View File

@@ -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);
}

View File

@@ -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);
});

View File

@@ -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);
});

View File

@@ -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);

View File

@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
<AspNetCoreModuleName>AspNetCoreModule</AspNetCoreModuleName>
<RuntimeIdentifiers>win-x64</RuntimeIdentifiers>