Skip to content

Commit a0e0cc3

Browse files
author
Syed Adeel Hassan Rizvi
committed
/watch arg added, main.js updated
1 parent 71955b9 commit a0e0cc3

File tree

6 files changed

+88
-12
lines changed

6 files changed

+88
-12
lines changed

ElectronNET.CLI/Commands/StartElectronCommand.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,25 @@ public Task<bool> ExecuteAsync()
6060
var platformInfo = GetTargetPlatformInformation.Do(string.Empty, string.Empty);
6161

6262
string tempBinPath = Path.Combine(tempPath, "bin");
63-
var resultCode = ProcessHelper.CmdExecute($"dotnet publish -r {platformInfo.NetCorePublishRid} --output \"{tempBinPath}\" /p:PublishReadyToRun=true --no-self-contained", aspCoreProjectPath);
63+
var resultCode = 0;
64+
65+
if (parser != null && parser.Contains("watch"))
66+
{
67+
68+
// if not exists then create mklink
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
79+
{
80+
resultCode = ProcessHelper.CmdExecute($"dotnet publish -r {platformInfo.NetCorePublishRid} --output \"{tempBinPath}\" /p:PublishReadyToRun=true --no-self-contained", aspCoreProjectPath);
81+
}
6482

6583
if (resultCode != 0)
6684
{
@@ -110,13 +128,19 @@ public Task<bool> ExecuteAsync()
110128
arguments += " --clear-cache=true";
111129
}
112130

131+
if (parser.Arguments.ContainsKey("watch"))
132+
{
133+
arguments += " --watch=true";
134+
}
135+
113136
string path = Path.Combine(tempPath, "node_modules", ".bin");
114137
bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
115138

116139
if (isWindows)
117140
{
118141
Console.WriteLine("Invoke electron.cmd - in dir: " + path);
119142
ProcessHelper.CmdExecute(@"electron.cmd ""..\..\main.js"" " + arguments, path);
143+
120144
}
121145
else
122146
{

ElectronNET.CLI/ElectronNET.CLI.csproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55

6-
<TargetFramework>netcoreapp3.0</TargetFramework>
6+
<TargetFramework>netcoreapp3.1</TargetFramework>
77
<AssemblyName>dotnet-electronize</AssemblyName>
88
<ToolCommandName>electronize</ToolCommandName>
99

@@ -29,6 +29,11 @@
2929
<PackageReleaseNotes>Changelog: https://github.com/ElectronNET/Electron.NET/blob/master/Changelog.md</PackageReleaseNotes>
3030
<PackageIcon>PackageIcon.png</PackageIcon>
3131
<PackAsTool>true</PackAsTool>
32+
<StartupObject></StartupObject>
33+
</PropertyGroup>
34+
35+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
36+
<PlatformTarget>AnyCPU</PlatformTarget>
3237
</PropertyGroup>
3338

3439
<ItemGroup>
@@ -37,7 +42,7 @@
3742
</ItemGroup>
3843

3944
<ItemGroup>
40-
<None Include="PackageIcon.png" Pack="true" PackagePath="\"/>
45+
<None Include="PackageIcon.png" Pack="true" PackagePath="\" />
4146
</ItemGroup>
4247

4348
<ItemGroup>

ElectronNET.CLI/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"ElectronNET.CLI": {
44
"commandName": "Project",
5-
"commandLineArgs": "build \"C:\\Users\\Gregor\\Documents\\Visual Studio 2017\\Projects\\ElectronNET\\ElectronNET.WebApp\""
5+
"commandLineArgs": "start /project-path \"C:\\Users\\Rizvi\\source\\repos\\Electron.NET\\ElectronNET.WebApp\" /watch"
66
}
77
}
88
}

ElectronNET.Host/main.js

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { app } = require('electron');
22
const { BrowserWindow } = require('electron');
33
const path = require('path');
4-
const process = require('child_process').spawn;
4+
const cProcess = require('child_process').spawn;
55
const portscanner = require('portscanner');
66
const imageSize = require('image-size');
77
let io, server, browserWindows, ipc, apiProcess, loadURL;
@@ -11,12 +11,25 @@ let commandLine, browserView;
1111
let splashScreen, hostHook;
1212

1313
let manifestJsonFileName = 'electron.manifest.json';
14-
if(app.commandLine.hasSwitch('manifest')) {
14+
let watchable = false;
15+
if (app.commandLine.hasSwitch('manifest')) {
1516
manifestJsonFileName = app.commandLine.getSwitchValue('manifest');
1617
};
1718

18-
const currentBinPath = path.join(__dirname.replace('app.asar', ''), 'bin');
19-
const manifestJsonFilePath = path.join(currentBinPath, manifestJsonFileName);
19+
if (app.commandLine.hasSwitch('watch')) {
20+
watchable = true;
21+
};
22+
23+
let currentBinPath = path.join(__dirname.replace('app.asar', ''), 'bin');
24+
let manifestJsonFilePath = path.join(currentBinPath, manifestJsonFileName);
25+
26+
// if watch is enabled lets change the path
27+
if (watchable) {
28+
currentBinPath = path.join(__dirname, '../../'); // go to project directory
29+
currentBinPath = currentBinPath.substr(0, currentBinPath.length - 1);
30+
manifestJsonFilePath = path.join(currentBinPath, manifestJsonFileName);
31+
}
32+
2033
const manifestJsonFile = require(manifestJsonFilePath);
2134
if (manifestJsonFile.singleInstance || manifestJsonFile.aspCoreBackendPort) {
2235
const mainInstance = app.requestSingleInstanceLock();
@@ -106,7 +119,11 @@ function startSocketApiBridge(port) {
106119
server.on('listening', function () {
107120
console.log('Electron Socket started on port %s at %s', server.address().port, server.address().address);
108121
// Now that socket connection is established, we can guarantee port will not be open for portscanner
109-
startAspCoreBackend(port);
122+
if (watchable) {
123+
startAspCoreBackendWithWatch(port);
124+
} else {
125+
startAspCoreBackend(port);
126+
}
110127
});
111128

112129
io.on('connection', (socket) => {
@@ -153,7 +170,7 @@ function isModuleAvailable(name) {
153170
}
154171

155172
function startAspCoreBackend(electronPort) {
156-
if(manifestJsonFile.aspCoreBackendPort) {
173+
if (manifestJsonFile.aspCoreBackendPort) {
157174
startBackend(manifestJsonFile.aspCoreBackendPort)
158175
} else {
159176
// hostname needs to be localhost, otherwise Windows Firewall will be triggered.
@@ -175,10 +192,40 @@ function startAspCoreBackend(electronPort) {
175192

176193
let binFilePath = path.join(currentBinPath, binaryFile);
177194
var options = { cwd: currentBinPath };
178-
apiProcess = process(binFilePath, parameters, options);
195+
apiProcess = cProcess(binFilePath, parameters, options);
179196

180197
apiProcess.stdout.on('data', (data) => {
181198
console.log(`stdout: ${data.toString()}`);
182199
});
183200
}
184201
}
202+
203+
function startAspCoreBackendWithWatch(electronPort) {
204+
if (manifestJsonFile.aspCoreBackendPort) {
205+
startBackend(manifestJsonFile.aspCoreBackendPort)
206+
} else {
207+
// hostname needs to be localhost, otherwise Windows Firewall will be triggered.
208+
portscanner.findAPortNotInUse(electronPort + 1, 65535, 'localhost', function (error, electronWebPort) {
209+
startBackend(electronWebPort);
210+
});
211+
}
212+
213+
function startBackend(aspCoreBackendPort) {
214+
console.log('ASP.NET Core Watch Port: ' + aspCoreBackendPort);
215+
loadURL = `http://localhost:${aspCoreBackendPort}`;
216+
const parameters = ['watch','run', `--project ${currentBinPath}`, `/electronPort=${electronPort}`, `/electronWebPort=${aspCoreBackendPort}`];
217+
218+
console.log(currentBinPath);
219+
var options = {
220+
cwd: currentBinPath,
221+
env: {
222+
PATH: process.env.PATH
223+
}
224+
};
225+
apiProcess = cProcess('dotnet', parameters, options);
226+
227+
apiProcess.stdout.on('data', (data) => {
228+
console.log(`stdout: ${data.toString()}`);
229+
});
230+
}
231+
}

ElectronNET.WebApp/ElectronNET.WebApp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
55
<AspNetCoreModuleName>AspNetCoreModule</AspNetCoreModuleName>
66
<RuntimeIdentifiers>win-x64</RuntimeIdentifiers>
7-
<TypeScriptToolsVersion>3.6</TypeScriptToolsVersion>
7+
<TypeScriptToolsVersion>3.8</TypeScriptToolsVersion>
88
</PropertyGroup>
99
<ItemGroup>
1010
<Compile Remove="Controllers\ManageWindowsController.cs" />

ElectronNET.WebApp/erd.png

87.1 KB
Loading

0 commit comments

Comments
 (0)