Skip to content

Commit df8e269

Browse files
committed
Core: Introduce cross-platform npm restore and check mismatch on publish
1 parent 19e785f commit df8e269

1 file changed

Lines changed: 61 additions & 19 deletions

File tree

src/ElectronNET/build/ElectronNET.LateImport.targets

Lines changed: 61 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

@@ -367,7 +412,7 @@
367412
<_ElectronPublishAppAfterTarget Condition="'$(UsingMicrosoftNETSdkWeb)' != 'true'">Publish</_ElectronPublishAppAfterTarget>
368413
</PropertyGroup>
369414

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

372417
<PropertyGroup>
373418
<PublishDir>$(_OriginalPublishDir)</PublishDir>
@@ -376,21 +421,18 @@
376421
</PropertyGroup>
377422

378423

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>
424+
<Error Condition="'$(_IsPlatformMismatch)' == 'true'"
425+
Code="ELECTRON100"
426+
Text="The target RuntimeIdentifier '$(RuntimeIdentifier)' (platform: $(ElectronPlatform)) does not match the current operating system ($(_CurrentOSPlatform)).
389427
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>
428+
Electron applications must be built on the target operating system:
429+
- Windows targets (win-x64, win-x86, win-arm64) must be built on Windows
430+
- Linux targets (linux-x64, linux-arm, linux-arm64) must be built on Linux (or Windows with WSL)
431+
- macOS targets (osx-x64, osx-arm64) must be built on macOS
432+
433+
EXCEPTION: Linux targets can be built on Windows using WSL (Windows Subsystem for Linux).
434+
435+
For more information, see: https://github.com/ElectronNET/Electron.NET/wiki/Migration-Checks#8-cross-platform-build-validation" />
394436

395437
<RemoveEnvironmentVariables Variables="BUILD_BUILDNUMBER;BUILD_NUMBER;TRAVIS_BUILD_NUMBER;APPVEYOR_BUILD_NUMBER;CIRCLE_BUILD_NUM;CI_PIPELINE_IID" />
396438

0 commit comments

Comments
 (0)