From 498ab0a6443d35560f164887a47fd05c6f6e6543 Mon Sep 17 00:00:00 2001 From: Hyeonmin Seon Date: Fri, 1 Aug 2025 17:37:16 +0900 Subject: [PATCH 1/2] 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. --- src/ElectronNET.CLI/Commands/BuildCommand.cs | 79 +++++++++++++++---- .../Commands/StartElectronCommand.cs | 11 +++ src/ElectronNET.Host/build-helper.js | 36 +++++++-- src/ElectronNET.WebApp/electron.manifest.json | 1 + 4 files changed, 102 insertions(+), 25 deletions(-) diff --git a/src/ElectronNET.CLI/Commands/BuildCommand.cs b/src/ElectronNET.CLI/Commands/BuildCommand.cs index 2f4f52c7..6f68e742 100644 --- a/src/ElectronNET.CLI/Commands/BuildCommand.cs +++ b/src/ElectronNET.CLI/Commands/BuildCommand.cs @@ -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 @@ -132,11 +133,70 @@ public Task 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..."); @@ -182,23 +242,8 @@ public Task 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"); diff --git a/src/ElectronNET.CLI/Commands/StartElectronCommand.cs b/src/ElectronNET.CLI/Commands/StartElectronCommand.cs index 03382ecd..b83e8e51 100644 --- a/src/ElectronNET.CLI/Commands/StartElectronCommand.cs +++ b/src/ElectronNET.CLI/Commands/StartElectronCommand.cs @@ -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 @@ -118,6 +119,16 @@ public Task 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); diff --git a/src/ElectronNET.Host/build-helper.js b/src/ElectronNET.Host/build-helper.js index d8ebc944..e04f2de5 100644 --- a/src/ElectronNET.Host/build-helper.js +++ b/src/ElectronNET.Host/build-helper.js @@ -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'); diff --git a/src/ElectronNET.WebApp/electron.manifest.json b/src/ElectronNET.WebApp/electron.manifest.json index 5fed92e8..185c0a31 100644 --- a/src/ElectronNET.WebApp/electron.manifest.json +++ b/src/ElectronNET.WebApp/electron.manifest.json @@ -5,6 +5,7 @@ }, "environment": "Production", "singleInstance": false, + "electronVersion": "23.2.0", "build": { "appId": "com.electronnetapidemos.app", "productName": "ElectronNET API Demos", From 855e8306c852231e8ec6afd172fc01adf2683aef Mon Sep 17 00:00:00 2001 From: Hyeonmin Seon Date: Fri, 1 Aug 2025 17:43:43 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=EB=B2=84=EC=A0=84=20=EC=97=85=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=8A=B8:=2023.6.2-messe=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 변경 사항: * `Version` 요소의 값이 `99.0.0.0`에서 `23.6.2-messe`로 변경되었습니다. * 패키지의 버전을 업데이트하여 새로운 기능이나 수정 사항을 반영하였습니다. --- src/ElectronNET.CLI/ElectronNET.CLI.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ElectronNET.CLI/ElectronNET.CLI.csproj b/src/ElectronNET.CLI/ElectronNET.CLI.csproj index 2023d894..7599d8a6 100644 --- a/src/ElectronNET.CLI/ElectronNET.CLI.csproj +++ b/src/ElectronNET.CLI/ElectronNET.CLI.csproj @@ -8,7 +8,7 @@ ..\..\artifacts ElectronNET.CLI - 99.0.0.0 + 23.6.2-messe Gregor Biswanger, Florian Rappl Electron.NET