@@ -81,6 +81,7 @@ public ProcessWrapper(Process process, IOutputProcessor outputProcessor,
8181
8282 public void Run ( )
8383 {
84+ Exception thrownException = null ;
8485 if ( Process . StartInfo . RedirectStandardError )
8586 {
8687 Process . ErrorDataReceived += ( s , e ) =>
@@ -103,73 +104,78 @@ public void Run()
103104 {
104105 if ( ! Process . StartInfo . Arguments . StartsWith ( "credential-" ) )
105106 Logger . Trace ( $ "Running '{ Process . StartInfo . FileName . ToNPath ( ) . FileName } { Process . StartInfo . Arguments } '") ;
107+
106108 Process . Start ( ) ;
107- }
108- catch ( Win32Exception ex )
109- {
110- StringBuilder sb = new StringBuilder ( ) ;
111- sb . AppendLine ( "Error code " + ex . NativeErrorCode ) ;
112- if ( ex . NativeErrorCode == 2 )
113- {
114- sb . AppendLine ( "The system cannot find the file specified." ) ;
115- }
116- foreach ( string env in Process . StartInfo . EnvironmentVariables . Keys )
117- {
118- sb . AppendFormat ( "{0}:{1}" , env , Process . StartInfo . EnvironmentVariables [ env ] ) ;
119- sb . AppendLine ( ) ;
120- }
121- onError ? . Invoke ( ex , String . Format ( "{0} {1}" , ex . Message , sb . ToString ( ) ) ) ;
122- onEnd ? . Invoke ( ) ;
123- return ;
124- }
125109
126- if ( Process . StartInfo . RedirectStandardInput )
127- Input = new StreamWriter ( Process . StandardInput . BaseStream , new UTF8Encoding ( false ) ) ;
128- if ( Process . StartInfo . RedirectStandardError )
129- Process . BeginErrorReadLine ( ) ;
110+ if ( Process . StartInfo . RedirectStandardInput )
111+ Input = new StreamWriter ( Process . StandardInput . BaseStream , new UTF8Encoding ( false ) ) ;
112+ if ( Process . StartInfo . RedirectStandardError )
113+ Process . BeginErrorReadLine ( ) ;
130114
131- onStart ? . Invoke ( ) ;
115+ onStart ? . Invoke ( ) ;
132116
133- if ( Process . StartInfo . RedirectStandardOutput )
134- {
135- var outputStream = Process . StandardOutput ;
136- var line = outputStream . ReadLine ( ) ;
137- while ( line != null )
117+ if ( Process . StartInfo . RedirectStandardOutput )
138118 {
139- outputProcessor . LineReceived ( line ) ;
119+ var outputStream = Process . StandardOutput ;
120+ var line = outputStream . ReadLine ( ) ;
121+ while ( line != null )
122+ {
123+ outputProcessor . LineReceived ( line ) ;
124+
125+ if ( token . IsCancellationRequested )
126+ {
127+ if ( ! Process . HasExited )
128+ Process . Kill ( ) ;
129+ Process . Close ( ) ;
130+ token . ThrowIfCancellationRequested ( ) ;
131+ }
132+
133+ line = outputStream . ReadLine ( ) ;
134+ }
135+ outputProcessor . LineReceived ( null ) ;
136+ }
140137
141- if ( token . IsCancellationRequested )
138+ if ( Process . StartInfo . CreateNoWindow )
139+ {
140+ while ( ! WaitForExit ( 500 ) )
142141 {
143- if ( ! Process . HasExited )
142+ if ( token . IsCancellationRequested )
143+ {
144144 Process . Kill ( ) ;
145-
146- Process . Close ( ) ;
147- onEnd ? . Invoke ( ) ;
145+ Process . Close ( ) ;
146+ }
148147 token . ThrowIfCancellationRequested ( ) ;
149148 }
150149
151- line = outputStream . ReadLine ( ) ;
150+ if ( Process . ExitCode != 0 && errors . Count > 0 )
151+ {
152+ thrownException = new ProcessException ( Process . ExitCode , string . Join ( Environment . NewLine , errors . ToArray ( ) ) ) ;
153+ }
152154 }
153- outputProcessor . LineReceived ( null ) ;
154155 }
155-
156- if ( Process . StartInfo . CreateNoWindow )
156+ catch ( Win32Exception ex )
157157 {
158- while ( ! WaitForExit ( 500 ) )
159- {
160- if ( token . IsCancellationRequested )
161- Process . Kill ( ) ;
162- Process . Close ( ) ;
163- onEnd ? . Invoke ( ) ;
164- token . ThrowIfCancellationRequested ( ) ;
165- }
158+ var errorCode = ex . NativeErrorCode ;
166159
167- if ( Process . ExitCode != 0 && errors . Count > 0 )
160+ StringBuilder sb = new StringBuilder ( ) ;
161+ if ( errorCode == 2 )
162+ sb . AppendLine ( "The system cannot find the file specified." ) ;
163+ foreach ( string env in Process . StartInfo . EnvironmentVariables . Keys )
168164 {
169- onError ? . Invoke ( null , string . Join ( Environment . NewLine , errors . ToArray ( ) ) ) ;
165+ sb . AppendFormat ( "{0}:{1}" , env , Process . StartInfo . EnvironmentVariables [ env ] ) ;
166+ sb . AppendLine ( ) ;
170167 }
168+ thrownException = new ProcessException ( errorCode , sb . ToString ( ) , ex ) ;
169+ }
170+ catch ( Exception ex )
171+ {
172+ thrownException = new ProcessException ( Process . HasExited ? Process . ExitCode : - 42 ,
173+ "Unknown error" ,
174+ ex ) ;
171175 }
172176
177+ if ( thrownException != null || errors . Count > 0 )
178+ onError ? . Invoke ( thrownException , string . Join ( Environment . NewLine , errors . ToArray ( ) ) ) ;
173179 onEnd ? . Invoke ( ) ;
174180 }
175181
@@ -275,11 +281,6 @@ protected virtual void ConfigureOutputProcessor()
275281 {
276282 }
277283
278- protected override void Run ( bool success )
279- {
280- throw new NotImplementedException ( ) ;
281- }
282-
283284 protected override T RunWithReturn ( bool success )
284285 {
285286 var result = base . RunWithReturn ( success ) ;
@@ -293,20 +294,24 @@ protected override T RunWithReturn(bool success)
293294 if ( outputProcessor != null )
294295 result = outputProcessor . Result ;
295296
296- if ( typeof ( T ) == typeof ( string ) && result == null && ! Process . StartInfo . CreateNoWindow )
297+ if ( typeof ( T ) == typeof ( string ) && result == null && ! Process . StartInfo . CreateNoWindow )
297298 result = ( T ) ( object ) "Process running" ;
298299
299- if ( Errors != null )
300- {
300+ if ( ! String . IsNullOrEmpty ( Errors ) )
301301 OnErrorData ? . Invoke ( Errors ) ;
302- thrownException = thrownException ?? new ProcessException ( this ) ;
303- throw thrownException ;
304- }
305302 }
306303 catch ( Exception ex )
307304 {
308- if ( ! RaiseFaultHandlers ( ex ) )
309- throw ;
305+ if ( thrownException == null )
306+ thrownException = new ProcessException ( ex . Message , ex ) ;
307+ else
308+ thrownException = new ProcessException ( thrownException . GetExceptionMessage ( ) , ex ) ;
309+ }
310+
311+ try
312+ {
313+ if ( thrownException != null && ! RaiseFaultHandlers ( thrownException ) )
314+ throw thrownException ;
310315 }
311316 finally
312317 {
@@ -427,17 +432,21 @@ protected override List<T> RunWithReturn(bool success)
427432 if ( result == null )
428433 result = new List < T > ( ) ;
429434
430- if ( Errors != null )
431- {
435+ if ( ! String . IsNullOrEmpty ( Errors ) )
432436 OnErrorData ? . Invoke ( Errors ) ;
433- thrownException = thrownException ?? new ProcessException ( this ) ;
434- throw thrownException ;
435- }
436437 }
437438 catch ( Exception ex )
438439 {
439- if ( ! RaiseFaultHandlers ( ex ) )
440- throw ;
440+ if ( thrownException == null )
441+ thrownException = new ProcessException ( ex . Message , ex ) ;
442+ else
443+ thrownException = new ProcessException ( thrownException . GetExceptionMessage ( ) , ex ) ;
444+ }
445+
446+ try
447+ {
448+ if ( thrownException != null && ! RaiseFaultHandlers ( thrownException ) )
449+ throw thrownException ;
441450 }
442451 finally
443452 {
0 commit comments