mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-09-22 06:54:44 +00:00
fix #767 - Added Electron version management and package.json updates.
* BuildCommand.cs now reads the Electron version from the manifest file and updates package.json. * StartElectronCommand.cs now reads the electronicVersion from the manifest file and updates package.json. * Improved package.json updates and JSON response direction in build-helper.js. * Added the electronicVersion property to Electron.manifest.json.
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Text.Json;
|
||||
using ElectronNET.CLI.Commands.Actions;
|
||||
|
||||
namespace ElectronNET.CLI.Commands
|
||||
@@ -132,11 +133,70 @@ namespace ElectronNET.CLI.Commands
|
||||
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...");
|
||||
@@ -182,23 +242,8 @@ namespace ElectronNET.CLI.Commands
|
||||
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");
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading.Tasks;
|
||||
using System.Text.Json;
|
||||
using ElectronNET.CLI.Commands.Actions;
|
||||
|
||||
namespace ElectronNET.CLI.Commands
|
||||
@@ -118,6 +119,16 @@ namespace ElectronNET.CLI.Commands
|
||||
|
||||
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);
|
||||
|
||||
@@ -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 || '';
|
||||
}
|
||||
|
||||
fs.writeFile('./package.json', JSON.stringify(packageJson), (error) => {
|
||||
if(error) {
|
||||
console.log(error.message);
|
||||
}
|
||||
});
|
||||
|
||||
// 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}`;
|
||||
}
|
||||
|
||||
// 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');
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
},
|
||||
"environment": "Production",
|
||||
"singleInstance": false,
|
||||
"electronVersion": "23.2.0",
|
||||
"build": {
|
||||
"appId": "com.electronnetapidemos.app",
|
||||
"productName": "ElectronNET API Demos",
|
||||
|
||||
Reference in New Issue
Block a user