|
3 | 3 | using System.IO;
|
4 | 4 | using System.Linq;
|
5 | 5 | using System.Threading.Tasks;
|
| 6 | +using System.Text.Json; |
6 | 7 | using ElectronNET.CLI.Commands.Actions;
|
7 | 8 |
|
8 | 9 | namespace ElectronNET.CLI.Commands
|
@@ -139,28 +140,138 @@ public Task<bool> ExecuteAsync()
|
139 | 140 | File.Copy(parser.Arguments[_paramPackageJson][0], Path.Combine(tempPath, "package.json"), true);
|
140 | 141 | }
|
141 | 142 |
|
142 |
| - var checkForNodeModulesDirPath = Path.Combine(tempPath, "node_modules"); |
| 143 | + // Read electron version from manifest file and update package.json BEFORE npm install |
| 144 | + string manifestFileName = "electron.manifest.json"; |
143 | 145 |
|
144 |
| - if (Directory.Exists(checkForNodeModulesDirPath) == false || parser.Contains(_paramForceNodeInstall) || parser.Contains(_paramPackageJson)) |
| 146 | + if (parser.Arguments.ContainsKey(_manifest)) |
| 147 | + { |
| 148 | + manifestFileName = parser.Arguments[_manifest].First(); |
| 149 | + } |
145 | 150 |
|
146 |
| - Console.WriteLine("Start npm install..."); |
147 |
| - ProcessHelper.CmdExecute("npm install --production", tempPath); |
| 151 | + // Read electron version from manifest file |
| 152 | + string electronVersion = "23.2.0"; // default fallback version |
| 153 | + string manifestPath = Path.Combine(Directory.GetCurrentDirectory(), manifestFileName); |
| 154 | + |
| 155 | + Console.WriteLine($"Reading electronVersion from manifest file: {manifestPath}"); |
| 156 | + |
| 157 | + if (File.Exists(manifestPath)) |
| 158 | + { |
| 159 | + try |
| 160 | + { |
| 161 | + string manifestContent = File.ReadAllText(manifestPath); |
| 162 | + using (JsonDocument document = JsonDocument.Parse(manifestContent)) |
| 163 | + { |
| 164 | + if (document.RootElement.TryGetProperty("electronVersion", out JsonElement electronVersionElement)) |
| 165 | + { |
| 166 | + string manifestElectronVersion = electronVersionElement.GetString(); |
| 167 | + if (!string.IsNullOrWhiteSpace(manifestElectronVersion)) |
| 168 | + { |
| 169 | + electronVersion = manifestElectronVersion; |
| 170 | + Console.WriteLine($"Using Electron version {electronVersion} from manifest file"); |
| 171 | + } |
| 172 | + else |
| 173 | + { |
| 174 | + Console.WriteLine($"electronVersion property found but empty in manifest file, using fallback version {electronVersion}"); |
| 175 | + } |
| 176 | + } |
| 177 | + else |
| 178 | + { |
| 179 | + Console.WriteLine($"electronVersion property not found in manifest file, using fallback version {electronVersion}"); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + catch (Exception ex) |
| 184 | + { |
| 185 | + Console.WriteLine($"Warning: Could not read electronVersion from manifest file: {ex.Message}"); |
| 186 | + Console.WriteLine($"Using fallback Electron version {electronVersion}"); |
| 187 | + } |
| 188 | + } |
| 189 | + else |
| 190 | + { |
| 191 | + Console.WriteLine($"Warning: Manifest file not found at {manifestPath}"); |
| 192 | + Console.WriteLine($"Using fallback Electron version {electronVersion}"); |
| 193 | + } |
148 | 194 |
|
149 |
| - Console.WriteLine("ElectronHostHook handling started..."); |
| 195 | + // Update package.json with electronVersion directly in C# before npm install |
| 196 | + string packageJsonPath = Path.Combine(tempPath, "package.json"); |
| 197 | + if (File.Exists(packageJsonPath)) |
| 198 | + { |
| 199 | + try |
| 200 | + { |
| 201 | + string packageJsonContent = File.ReadAllText(packageJsonPath); |
| 202 | + using (JsonDocument packageDocument = JsonDocument.Parse(packageJsonContent)) |
| 203 | + { |
| 204 | + var packageJsonObject = new Dictionary<string, object>(); |
| 205 | + |
| 206 | + // Copy existing properties |
| 207 | + foreach (var property in packageDocument.RootElement.EnumerateObject()) |
| 208 | + { |
| 209 | + if (property.Name == "devDependencies") |
| 210 | + { |
| 211 | + var devDeps = new Dictionary<string, object>(); |
| 212 | + foreach (var dep in property.Value.EnumerateObject()) |
| 213 | + { |
| 214 | + devDeps[dep.Name] = dep.Value.GetString(); |
| 215 | + } |
| 216 | + // Update electron version |
| 217 | + devDeps["electron"] = $"^{electronVersion}"; |
| 218 | + packageJsonObject["devDependencies"] = devDeps; |
| 219 | + } |
| 220 | + else if (property.Value.ValueKind == JsonValueKind.String) |
| 221 | + { |
| 222 | + packageJsonObject[property.Name] = property.Value.GetString(); |
| 223 | + } |
| 224 | + else if (property.Value.ValueKind == JsonValueKind.Object) |
| 225 | + { |
| 226 | + var subObject = new Dictionary<string, object>(); |
| 227 | + foreach (var subProp in property.Value.EnumerateObject()) |
| 228 | + { |
| 229 | + subObject[subProp.Name] = subProp.Value.GetString(); |
| 230 | + } |
| 231 | + packageJsonObject[property.Name] = subObject; |
| 232 | + } |
| 233 | + else |
| 234 | + { |
| 235 | + packageJsonObject[property.Name] = property.Value.ToString(); |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + // Ensure devDependencies exists and contains electron |
| 240 | + if (!packageJsonObject.ContainsKey("devDependencies")) |
| 241 | + { |
| 242 | + packageJsonObject["devDependencies"] = new Dictionary<string, object>(); |
| 243 | + } |
| 244 | + var devDependencies = (Dictionary<string, object>)packageJsonObject["devDependencies"]; |
| 245 | + devDependencies["electron"] = $"^{electronVersion}"; |
| 246 | + |
| 247 | + string updatedPackageJson = JsonSerializer.Serialize(packageJsonObject, new JsonSerializerOptions |
| 248 | + { |
| 249 | + WriteIndented = true |
| 250 | + }); |
| 251 | + |
| 252 | + File.WriteAllText(packageJsonPath, updatedPackageJson); |
| 253 | + Console.WriteLine($"Updated package.json with Electron version {electronVersion}"); |
| 254 | + } |
| 255 | + } |
| 256 | + catch (Exception ex) |
| 257 | + { |
| 258 | + Console.WriteLine($"Warning: Could not update package.json with electronVersion: {ex.Message}"); |
| 259 | + } |
| 260 | + } |
150 | 261 |
|
151 |
| - string electronhosthookDir = Path.Combine(Directory.GetCurrentDirectory(), "ElectronHostHook"); |
| 262 | + var checkForNodeModulesDirPath = Path.Combine(tempPath, "node_modules"); |
152 | 263 |
|
153 |
| - if (Directory.Exists(electronhosthookDir)) |
154 |
| - { |
155 |
| - string hosthookDir = Path.Combine(tempPath, "ElectronHostHook"); |
156 |
| - DirectoryCopy.Do(electronhosthookDir, hosthookDir, true, new List<string>() { "node_modules" }); |
| 264 | + if (Directory.Exists(checkForNodeModulesDirPath) == false || parser.Contains(_paramForceNodeInstall) || parser.Contains(_paramPackageJson)) |
| 265 | + Console.WriteLine("Start npm install..."); |
157 | 266 |
|
158 |
| - Console.WriteLine("Start npm install for hosthooks..."); |
159 |
| - ProcessHelper.CmdExecute("npm install", hosthookDir); |
| 267 | + ProcessHelper.CmdExecute("npm install --production", tempPath); |
160 | 268 |
|
161 |
| - // ToDo: Not sure if this runs under linux/macos |
162 |
| - ProcessHelper.CmdExecute(@"npx tsc -p . --sourceMap false", hosthookDir); |
163 |
| - } |
| 269 | + // Update build helper and configuration files after npm install |
| 270 | + Console.WriteLine("Create electron-builder configuration file..."); |
| 271 | + ProcessHelper.CmdExecute( |
| 272 | + string.IsNullOrWhiteSpace(version) |
| 273 | + ? $"node build-helper.js {manifestFileName}" |
| 274 | + : $"node build-helper.js {manifestFileName} {version}", tempPath); |
164 | 275 |
|
165 | 276 | Console.WriteLine("Build Electron Desktop Application...");
|
166 | 277 |
|
@@ -189,23 +300,8 @@ public Task<bool> ExecuteAsync()
|
189 | 300 | electronParams = parser.Arguments[_paramElectronParams][0];
|
190 | 301 | }
|
191 | 302 |
|
192 |
| - // ToDo: Make the same thing easer with native c# - we can save a tmp file in production code :) |
193 |
| - Console.WriteLine("Create electron-builder configuration file..."); |
194 |
| - |
195 |
| - string manifestFileName = "electron.manifest.json"; |
196 |
| - |
197 |
| - if (parser.Arguments.ContainsKey(_manifest)) |
198 |
| - { |
199 |
| - manifestFileName = parser.Arguments[_manifest].First(); |
200 |
| - } |
201 |
| - |
202 |
| - ProcessHelper.CmdExecute( |
203 |
| - string.IsNullOrWhiteSpace(version) |
204 |
| - ? $"node build-helper.js {manifestFileName}" |
205 |
| - : $"node build-helper.js {manifestFileName} {version}", tempPath); |
206 |
| - |
207 | 303 | Console.WriteLine($"Package Electron App for Platform {platformInfo.ElectronPackerPlatform}...");
|
208 |
| - ProcessHelper.CmdExecute($"npx electron-builder --config=./bin/electron-builder.json --{platformInfo.ElectronPackerPlatform} --{electronArch} -c.electronVersion=30.0.3 {electronParams}", tempPath); |
| 304 | + ProcessHelper.CmdExecute($"npx electron-builder --config=./bin/electron-builder.json --{platformInfo.ElectronPackerPlatform} --{electronArch} -c.electronVersion={electronVersion} {electronParams}", tempPath); |
209 | 305 |
|
210 | 306 | Console.WriteLine("... done");
|
211 | 307 |
|
|
0 commit comments