3
3
4
4
namespace Buildalyzer . Environment ;
5
5
6
- internal class ProcessRunner : IDisposable
6
+ internal sealed class ProcessRunner : IDisposable
7
7
{
8
8
private readonly ILogger Logger ;
9
-
10
- public List < string > Output { get ; } = new List < string > ( ) ;
11
- public List < string > Error { get ; } = new List < string > ( ) ;
9
+ private readonly ProcessDataCollector Collector ;
12
10
13
11
public int ExitCode => Process . ExitCode ;
14
12
13
+ public ProcessData Data => Collector . Data ;
14
+
15
15
private Process Process { get ; }
16
16
17
17
public Action Exited { get ; set ; }
@@ -35,7 +35,10 @@ public ProcessRunner(
35
35
UseShellExecute = false ,
36
36
RedirectStandardOutput = true ,
37
37
RedirectStandardError = true
38
- }
38
+ } ,
39
+
40
+ // Raises Process.Exited immediately instead of when checked via .WaitForExit() or .HasExited
41
+ EnableRaisingEvents = true ,
39
42
} ;
40
43
41
44
// Copy over environment variables
@@ -48,25 +51,11 @@ public ProcessRunner(
48
51
}
49
52
}
50
53
51
- Process . EnableRaisingEvents = true ; // Raises Process.Exited immediately instead of when checked via .WaitForExit() or .HasExited
52
- Process . Exited += ProcessExited ;
54
+ Process . OutputDataReceived += OutputDataReceived ;
55
+ Process . ErrorDataReceived += ErrorDataReceived ;
56
+ Process . Exited += OnExit ;
53
57
54
- Process . OutputDataReceived += ( _ , e ) =>
55
- {
56
- if ( ! string . IsNullOrEmpty ( e . Data ) )
57
- {
58
- Output . Add ( e . Data ) ;
59
- Logger . LogDebug ( "{Data}{NewLine}" , e . Data , System . Environment . NewLine ) ;
60
- }
61
- } ;
62
- Process . ErrorDataReceived += ( _ , e ) =>
63
- {
64
- if ( ! string . IsNullOrEmpty ( e . Data ) )
65
- {
66
- Error . Add ( e . Data ) ;
67
- Logger . LogDebug ( "{Data}{NewLine}" , e . Data , System . Environment . NewLine ) ;
68
- }
69
- } ;
58
+ Collector = new ( Process ) ;
70
59
}
71
60
72
61
public ProcessRunner Start ( )
@@ -83,16 +72,6 @@ public ProcessRunner Start()
83
72
return this ;
84
73
}
85
74
86
- private void ProcessExited ( object ? sender , EventArgs e )
87
- {
88
- Exited ? . Invoke ( ) ;
89
- Logger . LogDebug (
90
- "Process {Id} exited with code {ExitCode}{NewLine}" ,
91
- Process . Id ,
92
- Process . ExitCode ,
93
- System . Environment . NewLine ) ;
94
- }
95
-
96
75
public void WaitForExit ( ) => Process . WaitForExit ( ) ;
97
76
98
77
public bool WaitForExit ( int timeout )
@@ -108,9 +87,40 @@ public bool WaitForExit(int timeout)
108
87
return exited ;
109
88
}
110
89
90
+ private void OutputDataReceived ( object sender , DataReceivedEventArgs e )
91
+ {
92
+ if ( ! string . IsNullOrEmpty ( e . Data ) )
93
+ {
94
+ Logger . LogDebug ( "{Data}{NewLine}" , e . Data , NewLine ) ;
95
+ }
96
+ }
97
+
98
+ private void ErrorDataReceived ( object sender , DataReceivedEventArgs e )
99
+ {
100
+ if ( ! string . IsNullOrEmpty ( e . Data ) )
101
+ {
102
+ Logger . LogError ( "{Data}{NewLine}" , e . Data , NewLine ) ;
103
+ }
104
+ }
105
+
106
+ private void OnExit ( object ? sender , EventArgs e )
107
+ {
108
+ Exited ? . Invoke ( ) ;
109
+ Logger . LogDebug (
110
+ "Process {Id} exited with code {ExitCode}{NewLine}" ,
111
+ Process . Id ,
112
+ Process . ExitCode ,
113
+ NewLine ) ;
114
+ }
115
+
111
116
public void Dispose ( )
112
117
{
113
- Process . Exited -= ProcessExited ;
118
+ Process . OutputDataReceived -= OutputDataReceived ;
119
+ Process . ErrorDataReceived -= ErrorDataReceived ;
120
+ Process . Exited -= OnExit ;
114
121
Process . Close ( ) ;
122
+ Collector . Dispose ( ) ;
115
123
}
124
+
125
+ private static string NewLine => System . Environment . NewLine ;
116
126
}
0 commit comments