-
-
Notifications
You must be signed in to change notification settings - Fork 774
Expand file tree
/
Copy pathProcessWrapper.cs
More file actions
120 lines (106 loc) · 3.74 KB
/
Copy pathProcessWrapper.cs
File metadata and controls
120 lines (106 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using Cake.Core.Diagnostics;
namespace Cake.Core.IO
{
internal sealed class ProcessWrapper : IProcess
{
private readonly Process _process;
private readonly ICakeLog _log;
private readonly Func<string, string> _filterError;
private readonly Func<string, string> _filterOutput;
private readonly ConcurrentQueue<string> _consoleErrorQueue;
private readonly ConcurrentQueue<string> _consoleOutputQueue;
private readonly Func<string, string> _standardOutputHandler;
private readonly Func<string, string> _standardErrorHandler;
public ProcessWrapper(Process process, ICakeLog log, Func<string, string> filterOutput, Func<string, string> standardOutputHandler,
Func<string, string> filterError, Func<string, string> standardErrorHandler)
{
_process = process;
_log = log;
_filterOutput = filterOutput ?? (source => "[REDACTED]");
_consoleOutputQueue = new ConcurrentQueue<string>();
_standardOutputHandler = standardOutputHandler ?? (output => output);
_filterError = filterError ?? (source => "[REDACTED]");
_consoleErrorQueue = new ConcurrentQueue<string>();
_standardErrorHandler = standardErrorHandler ?? (output => output);
}
public void WaitForExit()
{
_process.WaitForExit();
}
public bool WaitForExit(int milliseconds)
{
if (_process.WaitForExit(milliseconds))
{
return true;
}
_process.Refresh();
if (!_process.HasExited)
{
_process.Kill();
}
return false;
}
public int GetExitCode()
{
return _process.ExitCode;
}
internal void StandardErrorReceived(string standardError)
{
var redirectedError = _standardErrorHandler(standardError);
if (redirectedError != null)
{
_consoleErrorQueue.Enqueue(redirectedError);
}
}
public IEnumerable<string> GetStandardError()
{
while (!_consoleErrorQueue.IsEmpty || !_process.HasExited)
{
string line;
if (!_consoleErrorQueue.TryDequeue(out line))
{
continue;
}
_log.Debug(log => log("{0}", _filterError(line)));
yield return line;
}
}
internal void StandardOutputReceived(string standardOutput)
{
var redirectedOutput = _standardOutputHandler(standardOutput);
if (redirectedOutput != null)
{
_consoleOutputQueue.Enqueue(redirectedOutput);
}
}
public IEnumerable<string> GetStandardOutput()
{
while (!_consoleOutputQueue.IsEmpty || !_process.HasExited)
{
string line;
if (!_consoleOutputQueue.TryDequeue(out line))
{
continue;
}
_log.Debug(log => log("{0}", _filterOutput(line)));
yield return line;
}
}
public void Kill()
{
_process.Kill();
_process.WaitForExit();
}
public void Dispose()
{
_process.Dispose();
}
}
}