11using System ;
22using System . Diagnostics ;
33using System . Runtime . InteropServices ;
4- using System . Text . RegularExpressions ;
54
65namespace ElectronNET . CLI
76{
87 public class ProcessHelper
98 {
10- private readonly static Regex ErrorRegex = new Regex ( @"\berror\b" , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
11-
129 public static int CmdExecute ( string command , string workingDirectoryPath , bool output = true , bool waitForExit = true )
1310 {
1411 using ( Process cmd = new Process ( ) )
@@ -17,12 +14,13 @@ public static int CmdExecute(string command, string workingDirectoryPath, bool o
1714
1815 if ( isWindows )
1916 {
20- cmd . StartInfo . FileName = "cmd.exe" ;
17+ cmd . StartInfo = new ProcessStartInfo ( "cmd.exe" , "/c " + command ) ;
2118 }
2219 else
2320 {
2421 // works for OSX and Linux (at least on Ubuntu)
25- cmd . StartInfo . FileName = "bash" ;
22+ var escapedArgs = command . Replace ( "\" " , "\\ \" " ) ;
23+ cmd . StartInfo = new ProcessStartInfo ( "bash" , $ "-c \" { escapedArgs } \" ") ;
2624 }
2725
2826 cmd . StartInfo . RedirectStandardInput = true ;
@@ -32,65 +30,23 @@ public static int CmdExecute(string command, string workingDirectoryPath, bool o
3230 cmd . StartInfo . UseShellExecute = false ;
3331 cmd . StartInfo . WorkingDirectory = workingDirectoryPath ;
3432
35- int returnCode = 0 ;
36-
3733 if ( output )
3834 {
39- cmd . OutputDataReceived += ( s , e ) =>
40- {
41- // (sometimes error messages are only visbile here)
42- // poor mans solution, we just seek for the term 'error'
43-
44- // we can't just use cmd.ExitCode, because
45- // we delegate it to cmd.exe, which runs fine
46- // but we can catch any error here and return
47- // 1 if something fails
48- if ( e != null && string . IsNullOrWhiteSpace ( e . Data ) == false )
49- {
50- if ( ErrorRegex . IsMatch ( e . Data ) )
51- {
52- returnCode = 1 ;
53- }
54-
55- Console . WriteLine ( e . Data ) ;
56- }
57-
58- } ;
59- cmd . ErrorDataReceived += ( s , e ) =>
60- {
61- // poor mans solution, we just seek for the term 'error'
62-
63- // we can't just use cmd.ExitCode, because
64- // we delegate it to cmd.exe, which runs fine
65- // but we can catch any error here and return
66- // 1 if something fails
67- if ( e != null && string . IsNullOrWhiteSpace ( e . Data ) == false )
68- {
69- if ( ErrorRegex . IsMatch ( e . Data ) )
70- {
71- returnCode = 1 ;
72- }
73-
74- Console . WriteLine ( e . Data ) ;
75- }
76-
77- } ;
35+ cmd . OutputDataReceived += ( s , e ) => Console . WriteLine ( e . Data ) ;
36+ cmd . ErrorDataReceived += ( s , e ) => Console . WriteLine ( e . Data ) ;
7837 }
7938
39+ Console . WriteLine ( command ) ;
8040 cmd . Start ( ) ;
8141 cmd . BeginOutputReadLine ( ) ;
8242 cmd . BeginErrorReadLine ( ) ;
8343
84- cmd . StandardInput . WriteLine ( command ) ;
85- cmd . StandardInput . Flush ( ) ;
86- cmd . StandardInput . Close ( ) ;
87-
8844 if ( waitForExit )
8945 {
9046 cmd . WaitForExit ( ) ;
9147 }
9248
93- return returnCode ;
49+ return cmd . ExitCode ;
9450 }
9551 }
9652 }
0 commit comments