1
1
using System ;
2
2
using System . Diagnostics ;
3
3
using System . Runtime . InteropServices ;
4
- using System . Text . RegularExpressions ;
5
4
6
5
namespace ElectronNET . CLI
7
6
{
8
7
public class ProcessHelper
9
8
{
10
- private readonly static Regex ErrorRegex = new Regex ( @"\berror\b" , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
11
-
12
9
public static int CmdExecute ( string command , string workingDirectoryPath , bool output = true , bool waitForExit = true )
13
10
{
14
11
using ( Process cmd = new Process ( ) )
@@ -17,12 +14,13 @@ public static int CmdExecute(string command, string workingDirectoryPath, bool o
17
14
18
15
if ( isWindows )
19
16
{
20
- cmd . StartInfo . FileName = "cmd.exe" ;
17
+ cmd . StartInfo = new ProcessStartInfo ( "cmd.exe" , "/c " + command ) ;
21
18
}
22
19
else
23
20
{
24
21
// 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 } \" ") ;
26
24
}
27
25
28
26
cmd . StartInfo . RedirectStandardInput = true ;
@@ -32,65 +30,23 @@ public static int CmdExecute(string command, string workingDirectoryPath, bool o
32
30
cmd . StartInfo . UseShellExecute = false ;
33
31
cmd . StartInfo . WorkingDirectory = workingDirectoryPath ;
34
32
35
- int returnCode = 0 ;
36
-
37
33
if ( output )
38
34
{
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 ) ;
78
37
}
79
38
39
+ Console . WriteLine ( command ) ;
80
40
cmd . Start ( ) ;
81
41
cmd . BeginOutputReadLine ( ) ;
82
42
cmd . BeginErrorReadLine ( ) ;
83
43
84
- cmd . StandardInput . WriteLine ( command ) ;
85
- cmd . StandardInput . Flush ( ) ;
86
- cmd . StandardInput . Close ( ) ;
87
-
88
44
if ( waitForExit )
89
45
{
90
46
cmd . WaitForExit ( ) ;
91
47
}
92
48
93
- return returnCode ;
49
+ return cmd . ExitCode ;
94
50
}
95
51
}
96
52
}
0 commit comments