WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 368ef41

Browse files
authored
Merge pull request #988 from softworkz/submit_crossdebug
Core: Introduce cross-platform npm restore and check mismatch on publish
2 parents 89cdf2f + 9d03787 commit 368ef41

File tree

2 files changed

+167
-25
lines changed

2 files changed

+167
-25
lines changed

src/ElectronNET.API/Runtime/Services/ElectronProcess/ElectronProcessActive.cs

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
namespace ElectronNET.Runtime.Services.ElectronProcess
22
{
3-
using ElectronNET.Common;
4-
using ElectronNET.Runtime.Data;
53
using System;
64
using System.ComponentModel;
75
using System.IO;
6+
using System.Linq;
87
using System.Runtime.InteropServices;
98
using System.Threading.Tasks;
9+
using ElectronNET.Common;
10+
using ElectronNET.Runtime.Data;
1011

1112
/// <summary>
1213
/// Launches and manages the Electron app process.
@@ -33,14 +34,42 @@ public ElectronProcessActive(bool isUnpackaged, string electronBinaryName, strin
3334
this.socketPort = socketPort;
3435
}
3536

36-
protected override Task StartCore()
37+
protected override async Task StartCore()
3738
{
3839
var dir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
3940
string startCmd, args, workingDir;
4041

4142
if (this.isUnpackaged)
4243
{
44+
this.CheckRuntimeIdentifier();
45+
4346
var electrondir = Path.Combine(dir.FullName, ".electron");
47+
48+
ProcessRunner chmodRunner = null;
49+
50+
try
51+
{
52+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
53+
{
54+
var distFolder = Path.Combine(electrondir, "node_modules", "electron", "dist");
55+
56+
chmodRunner = new ProcessRunner("ElectronRunner-Chmod");
57+
chmodRunner.Run("chmod", "-R +x " + distFolder, electrondir);
58+
await chmodRunner.WaitForExitAsync().ConfigureAwait(true);
59+
60+
if (chmodRunner.LastExitCode != 0)
61+
{
62+
throw new Exception("Failed to set executable permissions on Electron dist folder.");
63+
}
64+
}
65+
}
66+
catch (Exception ex)
67+
{
68+
Console.Error.WriteLine("[StartCore]: Exception: " + chmodRunner?.StandardError);
69+
Console.Error.WriteLine("[StartCore]: Exception: " + chmodRunner?.StandardOutput);
70+
Console.Error.WriteLine("[StartCore]: Exception: " + ex);
71+
}
72+
4473
startCmd = Path.Combine(electrondir, "node_modules", "electron", "dist", "electron");
4574

4675
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
@@ -53,17 +82,71 @@ protected override Task StartCore()
5382
}
5483
else
5584
{
56-
dir = dir.Parent?.Parent;
85+
dir = dir.Parent!.Parent!;
5786
startCmd = Path.Combine(dir.FullName, this.electronBinaryName);
5887
args = $"-dotnetpacked -electronforcedport={this.socketPort:D} " + this.extraArguments;
5988
workingDir = dir.FullName;
6089
}
6190

62-
6391
// We don't await this in order to let the state transition to "Starting"
6492
Task.Run(async () => await this.StartInternal(startCmd, args, workingDir).ConfigureAwait(false));
93+
}
6594

66-
return Task.CompletedTask;
95+
private void CheckRuntimeIdentifier()
96+
{
97+
var buildInfoRid = ElectronNetRuntime.BuildInfo.RuntimeIdentifier;
98+
if (string.IsNullOrEmpty(buildInfoRid))
99+
{
100+
return;
101+
}
102+
103+
var osPart = buildInfoRid.Split('-').First();
104+
105+
var mismatch = false;
106+
107+
switch (osPart)
108+
{
109+
case "win":
110+
111+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
112+
{
113+
mismatch = true;
114+
}
115+
116+
break;
117+
118+
case "linux":
119+
120+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
121+
{
122+
mismatch = true;
123+
}
124+
125+
break;
126+
127+
case "osx":
128+
129+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
130+
{
131+
mismatch = true;
132+
}
133+
134+
break;
135+
136+
case "freebsd":
137+
138+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD))
139+
{
140+
mismatch = true;
141+
}
142+
143+
break;
144+
}
145+
146+
if (mismatch)
147+
{
148+
throw new PlatformNotSupportedException($"This Electron.NET application was built for '{buildInfoRid}'. It cannot run on this platform.");
149+
}
67150
}
68151

69152
protected override Task StopCore()

src/ElectronNET/build/ElectronNET.LateImport.targets

Lines changed: 78 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,58 @@
297297
<RemoveDir Directories="$(ElectronHookTargetModuleDir)" Condition="Exists($(ElectronHookTargetModuleDir))" />
298298
</Target>
299299

300+
<Target Name="ElectronCheckVersionMismatch">
301+
302+
<PropertyGroup>
303+
<ElectronArch Condition="'$(RuntimeIdentifier)' == 'win-x64'">x64</ElectronArch>
304+
<ElectronArch Condition="'$(RuntimeIdentifier)' == 'win-x86'">ia32</ElectronArch>
305+
<ElectronArch Condition="'$(RuntimeIdentifier)' == 'win-arm64'">arm64</ElectronArch>
306+
<ElectronArch Condition="'$(RuntimeIdentifier)' == 'linux-x64'">x64</ElectronArch>
307+
<ElectronArch Condition="'$(RuntimeIdentifier)' == 'linux-arm'">armv7l</ElectronArch>
308+
<ElectronArch Condition="'$(RuntimeIdentifier)' == 'linux-arm64'">arm64</ElectronArch>
309+
<ElectronArch Condition="'$(RuntimeIdentifier)' == 'osx-x64'">x64</ElectronArch>
310+
<ElectronArch Condition="'$(RuntimeIdentifier)' == 'osx-arm64'">arm64</ElectronArch>
311+
312+
<ElectronPlatform Condition="'$(RuntimeIdentifier)' == 'win-x64' OR '$(RuntimeIdentifier)' == 'win-x86' OR '$(RuntimeIdentifier)' == 'win-arm64'">win</ElectronPlatform>
313+
<ElectronPlatform Condition="'$(RuntimeIdentifier)' == 'linux-x64' OR '$(RuntimeIdentifier)' == 'linux-arm' OR '$(RuntimeIdentifier)' == 'linux-arm64'">linux</ElectronPlatform>
314+
<ElectronPlatform Condition="'$(RuntimeIdentifier)' == 'osx-x64' OR '$(RuntimeIdentifier)' == 'osx-arm64'">mac</ElectronPlatform>
315+
316+
<!-- npm uses different OS names than Electron -->
317+
<NpmOs Condition="'$(ElectronPlatform)' == 'win'">win32</NpmOs>
318+
<NpmOs Condition="'$(ElectronPlatform)' == 'linux'">linux</NpmOs>
319+
<NpmOs Condition="'$(ElectronPlatform)' == 'mac'">darwin</NpmOs>
320+
321+
<!-- npm CPU is same as ElectronArch except for linux-arm -->
322+
<NpmCpu>$(ElectronArch)</NpmCpu>
323+
<NpmCpu Condition="'$(RuntimeIdentifier)' == 'linux-arm'">arm</NpmCpu>
324+
325+
<_CurrentOSPlatform Condition="$([MSBuild]::IsOSPlatform('Windows'))">win</_CurrentOSPlatform>
326+
<_CurrentOSPlatform Condition="$([MSBuild]::IsOSPlatform('Linux'))">linux</_CurrentOSPlatform>
327+
<_CurrentOSPlatform Condition="$([MSBuild]::IsOSPlatform('OSX'))">mac</_CurrentOSPlatform>
328+
</PropertyGroup>
329+
330+
<!-- Validate that the target platform matches the current OS -->
331+
<PropertyGroup>
332+
<IsLinuxWsl>false</IsLinuxWsl>
333+
<IsLinuxWsl Condition="'$(ElectronPlatform)' == 'linux' AND '$(_CurrentOSPlatform)' == 'win'">true</IsLinuxWsl>
334+
335+
<_IsCrossCompileAllowed>false</_IsCrossCompileAllowed>
336+
<!-- Allow Linux builds on Windows via WSL -->
337+
<_IsCrossCompileAllowed Condition="'$(_CurrentOSPlatform)' == 'win' AND '$(ElectronPlatform)' == 'linux' AND '$(IsLinuxWsl)' == 'true'">true</_IsCrossCompileAllowed>
338+
339+
<_IsPlatformMismatch>false</_IsPlatformMismatch>
340+
<_IsPlatformMismatch Condition="'$(_CurrentOSPlatform)' != '$(ElectronPlatform)' AND '$(_IsCrossCompileAllowed)' != 'true'">true</_IsPlatformMismatch>
341+
342+
</PropertyGroup>
343+
344+
</Target>
345+
300346

301347
<Target Name="ElectronConfigureApp"
302348
Inputs="@(ElectronPackageJsonFiles)"
303349
Outputs="@(ElectronPackageJsonFiles->'$(OutDir)%(TargetPath)')"
304350
AfterTargets="CopyFilesToOutputDirectory"
305-
DependsOnTargets="ElectronResetHostHook"
351+
DependsOnTargets="ElectronResetHostHook;ElectronCheckVersionMismatch"
306352
>
307353

308354
<Copy SourceFiles="@(ElectronPackageJsonFiles)" DestinationFiles="@(ElectronPackageJsonFiles->'$(OutDir)%(TargetPath)')" >
@@ -316,10 +362,9 @@
316362

317363
<PropertyGroup>
318364
<ElectronOutputPath>$([System.IO.Path]::GetFullPath('$(ElectronOutDir)'))</ElectronOutputPath>
319-
<LinuxPrefix>linux</LinuxPrefix>
320-
<IsLinuxWsl>false</IsLinuxWsl>
321-
<IsLinuxWsl Condition="'$(RuntimeIdentifier.StartsWith($(LinuxPrefix)))' == 'true'AND $([MSBuild]::IsOSPlatform('Windows'))">true</IsLinuxWsl>
322365
<_NpmCmd>npm install --no-bin-links</_NpmCmd>
366+
<!-- Add cross-platform parameters when there's a platform mismatch (for remote debugging preparation) -->
367+
<_NpmCmd Condition="'$(_IsPlatformMismatch)' == 'true'">$(_NpmCmd) --os=$(NpmOs) --cpu=$(NpmCpu) --arch=$(NpmCpu) --platform=$(NpmOs)</_NpmCmd>
323368
<_NpmCmd Condition="'$(IsLinuxWsl)' == 'true'">wsl bash -ic '$(_NpmCmd)'</_NpmCmd>
324369
</PropertyGroup>
325370

@@ -335,6 +380,23 @@
335380

336381
<Message Importance="High" Text="Electron setup failed!" Condition="'$(ExecExitCode)' != '0'" />
337382

383+
<!-- Fix up incorrect symlinks created by npm on Windows when targeting macOS -->
384+
<PropertyGroup>
385+
<_ElectronFrameworksDir>$(ElectronOutDir)node_modules\electron\dist\Electron.app\Contents\Frameworks</_ElectronFrameworksDir>
386+
</PropertyGroup>
387+
388+
<ItemGroup>
389+
<_ElectronFrameworkDirs Include="$(_ElectronFrameworksDir)\Electron Framework.framework" />
390+
<_ElectronFrameworkDirs Include="$(_ElectronFrameworksDir)\Mantle.framework" />
391+
<_ElectronFrameworkDirs Include="$(_ElectronFrameworksDir)\ReactiveObjC.framework" />
392+
<_ElectronFrameworkDirs Include="$(_ElectronFrameworksDir)\Squirrel.framework" />
393+
</ItemGroup>
394+
395+
<!-- Only execute on Windows host when targeting macOS -->
396+
<Message Importance="High" Text="Fixing macOS framework Resources symlinks" Condition="'$(ElectronPlatform)' == 'mac' AND '$(_CurrentOSPlatform)' == 'win'" />
397+
398+
<Exec Command="cmd /c del Resources &amp;&amp; mklink /D Resources &quot;Versions\Current\Resources&quot;" WorkingDirectory="%(_ElectronFrameworkDirs.Identity)" Condition="'$(ElectronPlatform)' == 'mac' AND '$(_CurrentOSPlatform)' == 'win'" />
399+
338400
</Target>
339401

340402
<Target Name="BeforePublishElectronApp" BeforeTargets="PrepareForPublish">
@@ -367,7 +429,7 @@
367429
<_ElectronPublishAppAfterTarget Condition="'$(UsingMicrosoftNETSdkWeb)' != 'true'">Publish</_ElectronPublishAppAfterTarget>
368430
</PropertyGroup>
369431

370-
<Target Name="ElectronPublishApp" AfterTargets="$(_ElectronPublishAppAfterTarget)">
432+
<Target Name="ElectronPublishApp" AfterTargets="$(_ElectronPublishAppAfterTarget)" DependsOnTargets="ElectronCheckVersionMismatch">
371433

372434
<PropertyGroup>
373435
<PublishDir>$(_OriginalPublishDir)</PublishDir>
@@ -376,21 +438,18 @@
376438
</PropertyGroup>
377439

378440

379-
<PropertyGroup>
380-
<!-- Default values -->
381-
<ElectronArch Condition="'$(RuntimeIdentifier)' == 'win-x64'">x64</ElectronArch>
382-
<ElectronArch Condition="'$(RuntimeIdentifier)' == 'win-x86'">ia32</ElectronArch>
383-
<ElectronArch Condition="'$(RuntimeIdentifier)' == 'win-arm64'">arm64</ElectronArch>
384-
<ElectronArch Condition="'$(RuntimeIdentifier)' == 'linux-x64'">x64</ElectronArch>
385-
<ElectronArch Condition="'$(RuntimeIdentifier)' == 'linux-arm'">armv7l</ElectronArch>
386-
<ElectronArch Condition="'$(RuntimeIdentifier)' == 'linux-arm64'">arm64</ElectronArch>
387-
<ElectronArch Condition="'$(RuntimeIdentifier)' == 'osx-x64'">x64</ElectronArch>
388-
<ElectronArch Condition="'$(RuntimeIdentifier)' == 'osx-arm64'">arm64</ElectronArch>
441+
<Error Condition="'$(_IsPlatformMismatch)' == 'true'"
442+
Code="ELECTRON100"
443+
Text="The target RuntimeIdentifier '$(RuntimeIdentifier)' (platform: $(ElectronPlatform)) does not match the current operating system ($(_CurrentOSPlatform)).
389444
390-
<ElectronPlatform Condition="'$(RuntimeIdentifier)' == 'win-x64' OR '$(RuntimeIdentifier)' == 'win-x86' OR '$(RuntimeIdentifier)' == 'win-arm64'">win</ElectronPlatform>
391-
<ElectronPlatform Condition="'$(RuntimeIdentifier)' == 'linux-x64' OR '$(RuntimeIdentifier)' == 'linux-arm' OR '$(RuntimeIdentifier)' == 'linux-arm64'">linux</ElectronPlatform>
392-
<ElectronPlatform Condition="'$(RuntimeIdentifier)' == 'osx-x64' OR '$(RuntimeIdentifier)' == 'osx-arm64'">mac</ElectronPlatform>
393-
</PropertyGroup>
445+
Electron applications must be built on the target operating system:
446+
- Windows targets (win-x64, win-x86, win-arm64) must be built on Windows
447+
- Linux targets (linux-x64, linux-arm, linux-arm64) must be built on Linux (or Windows with WSL)
448+
- macOS targets (osx-x64, osx-arm64) must be built on macOS
449+
450+
EXCEPTION: Linux targets can be built on Windows using WSL (Windows Subsystem for Linux).
451+
452+
For more information, see: https://github.com/ElectronNET/Electron.NET/wiki/Migration-Checks#8-cross-platform-build-validation" />
394453

395454
<RemoveEnvironmentVariables Variables="BUILD_BUILDNUMBER;BUILD_NUMBER;TRAVIS_BUILD_NUMBER;APPVEYOR_BUILD_NUMBER;CIRCLE_BUILD_NUM;CI_PIPELINE_IID" />
396455

0 commit comments

Comments
 (0)