Skip to content

Commit

Permalink
1. Watch enabled successfully.
Browse files Browse the repository at this point in the history
2. Reloading electron after build.
  • Loading branch information
Syed Adeel Hassan Rizvi committed May 1, 2020
1 parent 2987e31 commit 00eb986
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 25 deletions.
2 changes: 1 addition & 1 deletion ElectronNET.API/ElectronNET.API.csproj
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
20 changes: 16 additions & 4 deletions ElectronNET.API/WebHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Hosting;
using System;
using System.IO;

namespace ElectronNET.API
{
Expand All @@ -22,16 +23,27 @@ public static IWebHostBuilder UseElectron(this IWebHostBuilder builder, string[]
{
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;
Expand Down
15 changes: 1 addition & 14 deletions ElectronNET.CLI/Commands/StartElectronCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,7 @@ public Task<bool> ExecuteAsync()
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);
}
Expand Down
21 changes: 21 additions & 0 deletions ElectronNET.Host/api/browserWindows.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions ElectronNET.Host/api/browserWindows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
});
Expand Down
18 changes: 13 additions & 5 deletions ElectronNET.Host/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down Expand Up @@ -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')) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion ElectronNET.WebApp/ElectronNET.WebApp.csproj
Original file line number Diff line number Diff line change
@@ -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>
Expand Down

0 comments on commit 00eb986

Please sign in to comment.