Skip to content

Commit 207f85c

Browse files
committed
fixed capture output and errors in runprogramtask
1 parent 20d6076 commit 207f85c

2 files changed

Lines changed: 55 additions & 19 deletions

File tree

src/FlubuCore/Tasks/Process/IRunProgramTask.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ public interface IRunProgramTask : ITaskOfT<int, IRunProgramTask>, IExternalProc
3333
/// <returns></returns>
3434
IRunProgramTask CaptureErrorOutput();
3535

36+
/// <summary>
37+
/// Capture output of the running program.
38+
/// </summary>
39+
/// <returns></returns>
40+
IRunProgramTask CaptureOutput(bool capture);
41+
42+
/// <summary>
43+
/// Capture error output of the running program.
44+
/// </summary>
45+
/// <returns></returns>
46+
IRunProgramTask CaptureErrorOutput(bool capture);
47+
3648
/// <summary>
3749
/// Gets the whole output of the executed command.
3850
/// </summary>

src/FlubuCore/Tasks/Process/RunProgramTask.cs

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public class RunProgramTask : TaskBase<int, IRunProgramTask>, IRunProgramTask
2424

2525
private string _workingFolder;
2626

27-
private bool _captureOutput;
27+
private bool _captureOutput = true;
2828

29-
private bool _captureErrorOutput;
29+
private bool _captureErrorOutput = true;
3030

3131
private LogLevel _outputLogLevel = LogLevel.Info;
3232

@@ -110,13 +110,27 @@ public IRunProgramTask CaptureOutput()
110110
return this;
111111
}
112112

113+
/// <inheritdoc />
114+
public IRunProgramTask CaptureOutput(bool capture)
115+
{
116+
_captureOutput = capture;
117+
return this;
118+
}
119+
113120
/// <inheritdoc />
114121
public IRunProgramTask CaptureErrorOutput()
115122
{
116123
_captureErrorOutput = true;
117124
return this;
118125
}
119126

127+
/// <inheritdoc />
128+
public IRunProgramTask CaptureErrorOutput(bool capture)
129+
{
130+
_captureErrorOutput = false;
131+
return this;
132+
}
133+
120134
/// <inheritdoc />
121135
/// <summary>
122136
/// Get the output produced by executable.
@@ -222,25 +236,35 @@ protected override int DoExecute(ITaskContextInternal context)
222236
ICommand command = _commandFactory.Create(cmd, _arguments.Select(x => x.arg));
223237
string workingFolder = _workingFolder ?? rootDir;
224238
command
225-
.CaptureStdErr()
226-
.CaptureStdOut()
227-
.WorkingDirectory(workingFolder)
228-
.OnErrorLine(l =>
229-
{
230-
if (_outputLogLevel >= LogLevel.Error)
231-
DoLogInfo(l);
239+
.WorkingDirectory(workingFolder);
232240

233-
if (_captureErrorOutput)
234-
_errorOutput.AppendLine(l);
235-
})
236-
.OnOutputLine(l =>
237-
{
238-
if (_outputLogLevel >= LogLevel.Info)
239-
DoLogInfo(l);
241+
if (_captureOutput)
242+
{
243+
command
244+
.CaptureStdOut()
245+
.OnOutputLine(l =>
246+
{
247+
if (_outputLogLevel >= LogLevel.Info)
248+
DoLogInfo(l);
249+
250+
if (_captureOutput)
251+
_output.AppendLine(l);
252+
});
253+
}
240254

241-
if (_captureOutput)
242-
_output.AppendLine(l);
243-
});
255+
if (_captureErrorOutput)
256+
{
257+
command
258+
.CaptureStdErr()
259+
.OnErrorLine(l =>
260+
{
261+
if (_outputLogLevel >= LogLevel.Error)
262+
DoLogInfo(l);
263+
264+
if (_captureErrorOutput)
265+
_errorOutput.AppendLine(l);
266+
});
267+
}
244268

245269
string commandArgs = null;
246270
ProcessAdditionalOptions(context);

0 commit comments

Comments
 (0)