11namespace 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 ( )
0 commit comments