Skip to content

fix #767 - Added Electron version management and package.json updates. #885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 62 additions & 17 deletions src/ElectronNET.CLI/Commands/BuildCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Text.Json;
using ElectronNET.CLI.Commands.Actions;

namespace ElectronNET.CLI.Commands
Expand Down Expand Up @@ -132,11 +133,70 @@ public Task<bool> ExecuteAsync()
File.Copy(parser.Arguments[_paramPackageJson][0], Path.Combine(tempPath, "package.json"), true);
}

// Read electron version from manifest file and update package.json BEFORE npm install
string manifestFileName = "electron.manifest.json";

if (parser.Arguments.ContainsKey(_manifest))
{
manifestFileName = parser.Arguments[_manifest].First();
}

// Read electron version from manifest file
string electronVersion = "23.2.0"; // default fallback version
string manifestPath = Path.Combine(Directory.GetCurrentDirectory(), manifestFileName);

Console.WriteLine($"Reading electronVersion from manifest file: {manifestPath}");

if (File.Exists(manifestPath))
{
try
{
string manifestContent = File.ReadAllText(manifestPath);
using (JsonDocument document = JsonDocument.Parse(manifestContent))
{
if (document.RootElement.TryGetProperty("electronVersion", out JsonElement electronVersionElement))
{
string manifestElectronVersion = electronVersionElement.GetString();
if (!string.IsNullOrWhiteSpace(manifestElectronVersion))
{
electronVersion = manifestElectronVersion;
Console.WriteLine($"Using Electron version {electronVersion} from manifest file");
}
else
{
Console.WriteLine($"electronVersion property found but empty in manifest file, using fallback version {electronVersion}");
}
}
else
{
Console.WriteLine($"electronVersion property not found in manifest file, using fallback version {electronVersion}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Warning: Could not read electronVersion from manifest file: {ex.Message}");
Console.WriteLine($"Using fallback Electron version {electronVersion}");
}
}
else
{
Console.WriteLine($"Warning: Manifest file not found at {manifestPath}");
Console.WriteLine($"Using fallback Electron version {electronVersion}");
}

// Update package.json with electronVersion before npm install
Console.WriteLine("Create electron-builder configuration file...");
ProcessHelper.CmdExecute(
string.IsNullOrWhiteSpace(version)
? $"node build-helper.js {manifestFileName}"
: $"node build-helper.js {manifestFileName} {version}", tempPath);

var checkForNodeModulesDirPath = Path.Combine(tempPath, "node_modules");

if (Directory.Exists(checkForNodeModulesDirPath) == false || parser.Contains(_paramForceNodeInstall) || parser.Contains(_paramPackageJson))

Console.WriteLine("Start npm install...");

ProcessHelper.CmdExecute("npm install --production", tempPath);

Console.WriteLine("ElectronHostHook handling started...");
Expand Down Expand Up @@ -182,23 +242,8 @@ public Task<bool> ExecuteAsync()
electronParams = parser.Arguments[_paramElectronParams][0];
}

// ToDo: Make the same thing easer with native c# - we can save a tmp file in production code :)
Console.WriteLine("Create electron-builder configuration file...");

string manifestFileName = "electron.manifest.json";

if (parser.Arguments.ContainsKey(_manifest))
{
manifestFileName = parser.Arguments[_manifest].First();
}

ProcessHelper.CmdExecute(
string.IsNullOrWhiteSpace(version)
? $"node build-helper.js {manifestFileName}"
: $"node build-helper.js {manifestFileName} {version}", tempPath);

Console.WriteLine($"Package Electron App for Platform {platformInfo.ElectronPackerPlatform}...");
ProcessHelper.CmdExecute($"npx electron-builder --config=./bin/electron-builder.json --{platformInfo.ElectronPackerPlatform} --{electronArch} -c.electronVersion=23.2.0 {electronParams}", tempPath);
ProcessHelper.CmdExecute($"npx electron-builder --config=./bin/electron-builder.json --{platformInfo.ElectronPackerPlatform} --{electronArch} -c.electronVersion={electronVersion} {electronParams}", tempPath);

Console.WriteLine("... done");

Expand Down
11 changes: 11 additions & 0 deletions src/ElectronNET.CLI/Commands/StartElectronCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Text.Json;
using ElectronNET.CLI.Commands.Actions;

namespace ElectronNET.CLI.Commands
Expand Down Expand Up @@ -118,6 +119,16 @@ public Task<bool> ExecuteAsync()

DeployEmbeddedElectronFiles.Do(tempPath);

// Update package.json with electronVersion from manifest before npm install
string manifestFileName = "electron.manifest.json";
if (parser.Arguments.ContainsKey(_manifest))
{
manifestFileName = parser.Arguments[_manifest].First();
}

// Execute build-helper.js to update package.json before npm install
ProcessHelper.CmdExecute($"node build-helper.js {manifestFileName}", tempPath);

var nodeModulesDirPath = Path.Combine(tempPath, "node_modules");

Console.WriteLine("node_modules missing in: " + nodeModulesDirPath);
Expand Down
2 changes: 1 addition & 1 deletion src/ElectronNET.CLI/ElectronNET.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageOutputPath>..\..\artifacts</PackageOutputPath>
<PackageId>ElectronNET.CLI</PackageId>
<!-- Version 99 is just set for local development stuff to avoid a conflict with "real" packages on NuGet.org -->
<Version>99.0.0.0</Version>
<Version>23.6.2-messe</Version>
<Authors>Gregor Biswanger, Florian Rappl</Authors>
<Product>Electron.NET</Product>
<Company />
Expand Down
36 changes: 28 additions & 8 deletions src/ElectronNET.Host/build-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,40 @@ const builderConfiguration = { ...manifestFile.build };
if(process.argv.length > 3) {
builderConfiguration.buildVersion = process.argv[3];
}

// @ts-ignore
const packageJson = require('./package');

// Update package.json if buildVersion is provided
if(builderConfiguration.hasOwnProperty('buildVersion')) {
// @ts-ignore
const packageJson = require('./package');
packageJson.name = dasherize(manifestFile.name || 'electron-net');
packageJson.author = manifestFile.author || '';
packageJson.version = builderConfiguration.buildVersion;
packageJson.description = manifestFile.description || '';
}

// Update electron version if electronVersion is specified in manifest
if(manifestFile.hasOwnProperty('electronVersion') && manifestFile.electronVersion) {
console.log(`Using Electron version ${manifestFile.electronVersion} from manifest`);
packageJson.devDependencies = packageJson.devDependencies || {};
packageJson.devDependencies.electron = `^${manifestFile.electronVersion}`;
} else {
// If electronVersion is not specified, use a fallback version
const fallbackElectronVersion = "23.2.0";
console.log(`electronVersion not found in manifest, using fallback version ${fallbackElectronVersion}`);
packageJson.devDependencies = packageJson.devDependencies || {};
packageJson.devDependencies.electron = `^${fallbackElectronVersion}`;
}

fs.writeFile('./package.json', JSON.stringify(packageJson), (error) => {
if(error) {
console.log(error.message);
}
});

// Write updated package.json
fs.writeFile('./package.json', JSON.stringify(packageJson, null, 2), (error) => {
if(error) {
console.log(error.message);
}
});

// Update package-lock.json if it exists
if(builderConfiguration.hasOwnProperty('buildVersion')) {
try {
// @ts-ignore
const packageLockJson = require('./package-lock');
Expand Down
1 change: 1 addition & 0 deletions src/ElectronNET.WebApp/electron.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
},
"environment": "Production",
"singleInstance": false,
"electronVersion": "23.2.0",
"build": {
"appId": "com.electronnetapidemos.app",
"productName": "ElectronNET API Demos",
Expand Down