Make all command execution synchronous

- This fixes a bunch of weird races I was seeing building
larger solutions.
This commit is contained in:
David Fowler 2015-10-21 03:11:27 -07:00
parent c1e2d152e3
commit 5872aa0f0c
5 changed files with 29 additions and 30 deletions

View file

@ -11,7 +11,6 @@ namespace Microsoft.DotNet.Cli.Utils
{
public class Command
{
private TaskCompletionSource<int> _processTcs;
private Process _process;
private StringWriter _stdOutCapture;
@ -36,8 +35,6 @@ namespace Microsoft.DotNet.Cli.Utils
RedirectStandardOutput = true
};
_processTcs = new TaskCompletionSource<int>();
_process = new Process()
{
StartInfo = psi
@ -112,34 +109,37 @@ namespace Microsoft.DotNet.Cli.Utils
return false;
}
public async Task<CommandResult> RunAsync()
public CommandResult Execute()
{
ThrowIfRunning();
_running = true;
_process.OutputDataReceived += (sender, args) =>
{
ProcessData(args.Data, _stdOutCapture, _stdOutForward, _stdOutHandler);
};
_process.ErrorDataReceived += (sender, args) =>
{
ProcessData(args.Data, _stdErrCapture, _stdErrForward, _stdErrHandler);
};
_process.EnableRaisingEvents = true;
_process.Exited += (sender, _) =>
_processTcs.SetResult(_process.ExitCode);
#if DEBUG
var sw = Stopwatch.StartNew();
Reporter.Output.WriteLine($"> {_process.StartInfo.FileName} {_process.StartInfo.Arguments}".White());
Reporter.Output.WriteLine($"> {FormatProcessInfo(_process.StartInfo)}".White());
#endif
_process.Start();
_process.BeginOutputReadLine();
_process.BeginErrorReadLine();
var exitCode = await _processTcs.Task;
_process.WaitForExit();
var exitCode = _process.ExitCode;
#if DEBUG
var message = $"> {_process.StartInfo.FileName} {_process.StartInfo.Arguments} exited with {exitCode} in {sw.ElapsedMilliseconds} ms.";
var message = $"> {FormatProcessInfo(_process.StartInfo)} exited with {exitCode} in {sw.ElapsedMilliseconds} ms.";
if (exitCode == 0)
{
Reporter.Output.WriteLine(message.Green().Bold());
@ -206,6 +206,16 @@ namespace Microsoft.DotNet.Cli.Utils
return this;
}
private string FormatProcessInfo(ProcessStartInfo info)
{
if (string.IsNullOrWhiteSpace(info.Arguments))
{
return info.FileName;
}
return info.FileName + " " + info.Arguments;
}
private void ThrowIfRunning([CallerMemberName] string memberName = null)
{
if (_running)

View file

@ -24,8 +24,7 @@ namespace Microsoft.DotNet.Cli
return Command.Create("dotnet-" + args[1], "--help")
.ForwardStdErr()
.ForwardStdOut()
.RunAsync()
.Result
.Execute()
.ExitCode;
}
else
@ -39,8 +38,7 @@ namespace Microsoft.DotNet.Cli
return Command.Create("dotnet-" + args[0], args.Skip(1))
.ForwardStdErr()
.ForwardStdOut()
.RunAsync()
.Result
.Execute()
.ExitCode;
}
}

View file

@ -25,8 +25,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Csc
var result = RunCsc($"-noconfig @\"{responseFileArg.Value}\"")
.ForwardStdErr()
.ForwardStdOut()
.RunAsync()
.Result;
.Execute();
return result.ExitCode;
});

View file

@ -113,8 +113,7 @@ namespace Microsoft.DotNet.Tools.Compiler
var compileResult = Command.Create("dotnet-compile", $"--framework {projectDependency.Framework} --configuration {configuration} --no-project-dependencies {projectDependency.Project.ProjectDirectory}")
.ForwardStdOut()
.ForwardStdErr()
.RunAsync()
.Result;
.Execute();
if (compileResult.ExitCode != 0)
{
@ -223,9 +222,7 @@ namespace Microsoft.DotNet.Tools.Compiler
Console.Out.WriteLine(line);
}
})
.RunAsync()
.GetAwaiter()
.GetResult();
.Execute();
foreach (var diag in diagnostics)
{
@ -300,9 +297,7 @@ namespace Microsoft.DotNet.Tools.Compiler
var result = Command.Create("resgen", $"{fileName} {resourcesFile}")
.ForwardStdErr()
.ForwardStdOut()
.RunAsync()
.GetAwaiter()
.GetResult();
.Execute();
if (result.ExitCode != 0)
{

View file

@ -101,8 +101,7 @@ namespace Microsoft.DotNet.Tools.Publish
var result = Command.Create("dotnet-compile", $"--framework {context.TargetFramework.DotNetFrameworkName} {context.ProjectFile.ProjectDirectory}")
.ForwardStdErr()
.ForwardStdOut()
.RunAsync()
.Result;
.Execute();
if (result.ExitCode != 0)
{
@ -187,9 +186,7 @@ exec ""$DIR/corerun"" ""$DIR/{context.ProjectFile.Name}.exe"" $*
Command.Create("chmod", $"a+x {outputExe}")
.ForwardStdOut()
.ForwardStdErr()
.RunAsync()
.GetAwaiter()
.GetResult();
.Execute();
return 0;
}