Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 45 additions & 48 deletions src/GitHub.Api/NewTaskSystem/ProcessTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class ProcessWrapper
private readonly Action onEnd;
private readonly Action<Exception, string> onError;
private readonly CancellationToken token;
private readonly List<string> errors = new List<string>();

public Process Process { get; }
public StreamWriter Input { get; private set; }
Expand All @@ -78,39 +77,6 @@ public ProcessWrapper(Process process, IOutputProcessor outputProcessor,

public void Run()
{
if (Process.StartInfo.RedirectStandardOutput)
{
Process.OutputDataReceived += (s, e) =>
{
//Logger.Trace("OutputData \"" + (e.Data == null ? "'null'" : e.Data) + "\"");

string encodedData = null;
if (e.Data != null)
{
encodedData = Encoding.UTF8.GetString(Encoding.Default.GetBytes(e.Data));
}
outputProcessor.LineReceived(encodedData);
};
}

if (Process.StartInfo.RedirectStandardError)
{
Process.ErrorDataReceived += (s, e) =>
{
//if (e.Data != null)
//{
// Logger.Trace("ErrorData \"" + (e.Data == null ? "'null'" : e.Data) + "\"");
//}

string encodedData = null;
if (e.Data != null)
{
encodedData = Encoding.UTF8.GetString(Encoding.Default.GetBytes(e.Data));
errors.Add(encodedData);
}
};
}

try
{
Process.Start();
Expand All @@ -133,34 +99,65 @@ public void Run()
return;
}

if (Process.StartInfo.RedirectStandardOutput)
Process.BeginOutputReadLine();
if (Process.StartInfo.RedirectStandardError)
Process.BeginErrorReadLine();
if (Process.StartInfo.RedirectStandardInput)
Input = new StreamWriter(Process.StandardInput.BaseStream, new UTF8Encoding(false));

onStart?.Invoke();
var errors = new List<string>();

onStart?.Invoke();
if (Process.StartInfo.CreateNoWindow)
{
while (!WaitForExit(500))
if (Process.StartInfo.RedirectStandardOutput)
{
if (token.IsCancellationRequested)
var outputStream = Process.StandardOutput;
var line = outputStream.ReadLine();
while (line != null)
{
if (!Process.HasExited)
Process.Kill();
Process.Close();
onEnd?.Invoke();
token.ThrowIfCancellationRequested();
outputProcessor.LineReceived(line);

if (token.IsCancellationRequested)
{
if (!Process.HasExited)
Process.Kill();

Process.Close();
onEnd?.Invoke();
token.ThrowIfCancellationRequested();
}

line = outputStream.ReadLine();
}
outputProcessor.LineReceived(null);
}

if (Process.ExitCode != 0 && errors.Count > 0)
if (Process.StartInfo.RedirectStandardError)
{
onError?.Invoke(null, String.Join(Environment.NewLine, errors.ToArray()));
var errorStream = Process.StandardError;
var errorLine = errorStream.ReadLine();
while (errorLine != null)
{
errors.Add(errorLine);

if (token.IsCancellationRequested)
{
if (!Process.HasExited)
Process.Kill();

Process.Close();
onEnd?.Invoke();
token.ThrowIfCancellationRequested();
}

errorLine = errorStream.ReadLine();
}

if (Process.ExitCode != 0 && errors.Count > 0)
{
onError?.Invoke(null, string.Join(Environment.NewLine, errors.ToArray()));
}
}
}

onEnd?.Invoke();
}

Expand Down