33
44namespace Buildalyzer . Environment ;
55
6- internal class ProcessRunner : IDisposable
6+ internal sealed class ProcessRunner : IDisposable
77{
88 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 ;
1210
1311 public int ExitCode => Process . ExitCode ;
1412
13+ public ProcessData Data => Collector . Data ;
14+
1515 private Process Process { get ; }
1616
1717 public Action Exited { get ; set ; }
@@ -35,7 +35,10 @@ public ProcessRunner(
3535 UseShellExecute = false ,
3636 RedirectStandardOutput = true ,
3737 RedirectStandardError = true
38- }
38+ } ,
39+
40+ // Raises Process.Exited immediately instead of when checked via .WaitForExit() or .HasExited
41+ EnableRaisingEvents = true ,
3942 } ;
4043
4144 // Copy over environment variables
@@ -48,25 +51,11 @@ public ProcessRunner(
4851 }
4952 }
5053
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 ;
5357
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 ) ;
7059 }
7160
7261 public ProcessRunner Start ( )
@@ -83,16 +72,6 @@ public ProcessRunner Start()
8372 return this ;
8473 }
8574
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-
9675 public void WaitForExit ( ) => Process . WaitForExit ( ) ;
9776
9877 public bool WaitForExit ( int timeout )
@@ -108,9 +87,40 @@ public bool WaitForExit(int timeout)
10887 return exited ;
10988 }
11089
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+
111116 public void Dispose ( )
112117 {
113- Process . Exited -= ProcessExited ;
118+ Process . OutputDataReceived -= OutputDataReceived ;
119+ Process . ErrorDataReceived -= ErrorDataReceived ;
120+ Process . Exited -= OnExit ;
114121 Process . Close ( ) ;
122+ Collector . Dispose ( ) ;
115123 }
124+
125+ private static string NewLine => System . Environment . NewLine ;
116126}
0 commit comments