@@ -52,7 +52,13 @@ void AppendLines (string prefix, List<string> lines, StringBuilder sb)
52
52
}
53
53
}
54
54
55
- public static int RunProcess ( string command , string arguments , TaskLoggingHelper log , DataReceivedEventHandler ? onOutput = null , DataReceivedEventHandler ? onError = null )
55
+ public static int RunProcess ( string command , string arguments , TaskLoggingHelper log , DataReceivedEventHandler ? onOutput = null , DataReceivedEventHandler ? onError = null , CancellationToken ? cancellationToken = null , Action ? cancelTask = null , bool logWarningOnFailure = true )
56
+ {
57
+ return RunProcess ( "Running process" , command , arguments , log , onOutput , onError , cancellationToken , cancelTask , logWarningOnFailure ) ;
58
+ }
59
+
60
+ public static int RunProcess ( string logLabel , string command , string arguments , TaskLoggingHelper log , DataReceivedEventHandler ? onOutput = null , DataReceivedEventHandler ? onError = null ,
61
+ CancellationToken ? cancellationToken = null , Action ? cancelTask = null , bool logWarningOnFailure = true )
56
62
{
57
63
var stdout_completed = new ManualResetEvent ( false ) ;
58
64
var stderr_completed = new ManualResetEvent ( false ) ;
@@ -69,7 +75,7 @@ public static int RunProcess (string command, string arguments, TaskLoggingHelpe
69
75
var stdoutLines = new List < string > ( ) ;
70
76
var stderrLines = new List < string > ( ) ;
71
77
72
- log . LogDebugMessage ( $ "Running process : { psi . FileName } { psi . Arguments } ") ;
78
+ log . LogDebugMessage ( $ "{ logLabel } : { psi . FileName } { psi . Arguments } ") ;
73
79
using var proc = new Process ( ) ;
74
80
proc . OutputDataReceived += ( s , e ) => {
75
81
if ( e . Data != null ) {
@@ -91,6 +97,7 @@ public static int RunProcess (string command, string arguments, TaskLoggingHelpe
91
97
proc . Start ( ) ;
92
98
proc . BeginOutputReadLine ( ) ;
93
99
proc . BeginErrorReadLine ( ) ;
100
+ cancellationToken ? . Register ( ( ) => { try { proc . Kill ( ) ; } catch ( Exception ) { } } ) ;
94
101
proc . WaitForExit ( ) ;
95
102
96
103
if ( psi . RedirectStandardError ) {
@@ -101,9 +108,13 @@ public static int RunProcess (string command, string arguments, TaskLoggingHelpe
101
108
stdout_completed . WaitOne ( TimeSpan . FromSeconds ( 30 ) ) ;
102
109
}
103
110
111
+ log . LogDebugMessage ( $ "{ logLabel } : exit code == { proc . ExitCode } ") ;
104
112
if ( proc . ExitCode != 0 ) {
105
- var sb = MergeStdoutAndStderrMessages ( stdoutLines , stderrLines ) ;
106
- log . LogCodedError ( "XA0142" , Properties . Resources . XA0142 , $ "{ psi . FileName } { psi . Arguments } ", sb . ToString ( ) ) ;
113
+ if ( logWarningOnFailure ) {
114
+ var sb = MergeStdoutAndStderrMessages ( stdoutLines , stderrLines ) ;
115
+ log . LogCodedError ( "XA0142" , Properties . Resources . XA0142 , $ "{ psi . FileName } { psi . Arguments } ", sb . ToString ( ) ) ;
116
+ }
117
+ cancelTask ? . Invoke ( ) ;
107
118
}
108
119
109
120
try {
0 commit comments