Make all command execution synchronous
- This fixes a bunch of weird races I was seeing building larger solutions.
This commit is contained in:
parent
c1e2d152e3
commit
5872aa0f0c
5 changed files with 29 additions and 30 deletions
|
@ -11,7 +11,6 @@ namespace Microsoft.DotNet.Cli.Utils
|
||||||
{
|
{
|
||||||
public class Command
|
public class Command
|
||||||
{
|
{
|
||||||
private TaskCompletionSource<int> _processTcs;
|
|
||||||
private Process _process;
|
private Process _process;
|
||||||
|
|
||||||
private StringWriter _stdOutCapture;
|
private StringWriter _stdOutCapture;
|
||||||
|
@ -36,8 +35,6 @@ namespace Microsoft.DotNet.Cli.Utils
|
||||||
RedirectStandardOutput = true
|
RedirectStandardOutput = true
|
||||||
};
|
};
|
||||||
|
|
||||||
_processTcs = new TaskCompletionSource<int>();
|
|
||||||
|
|
||||||
_process = new Process()
|
_process = new Process()
|
||||||
{
|
{
|
||||||
StartInfo = psi
|
StartInfo = psi
|
||||||
|
@ -112,34 +109,37 @@ namespace Microsoft.DotNet.Cli.Utils
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<CommandResult> RunAsync()
|
public CommandResult Execute()
|
||||||
{
|
{
|
||||||
ThrowIfRunning();
|
ThrowIfRunning();
|
||||||
_running = true;
|
_running = true;
|
||||||
|
|
||||||
_process.OutputDataReceived += (sender, args) =>
|
_process.OutputDataReceived += (sender, args) =>
|
||||||
|
{
|
||||||
ProcessData(args.Data, _stdOutCapture, _stdOutForward, _stdOutHandler);
|
ProcessData(args.Data, _stdOutCapture, _stdOutForward, _stdOutHandler);
|
||||||
|
};
|
||||||
|
|
||||||
_process.ErrorDataReceived += (sender, args) =>
|
_process.ErrorDataReceived += (sender, args) =>
|
||||||
|
{
|
||||||
ProcessData(args.Data, _stdErrCapture, _stdErrForward, _stdErrHandler);
|
ProcessData(args.Data, _stdErrCapture, _stdErrForward, _stdErrHandler);
|
||||||
|
};
|
||||||
|
|
||||||
_process.EnableRaisingEvents = true;
|
_process.EnableRaisingEvents = true;
|
||||||
|
|
||||||
_process.Exited += (sender, _) =>
|
|
||||||
_processTcs.SetResult(_process.ExitCode);
|
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
var sw = Stopwatch.StartNew();
|
var sw = Stopwatch.StartNew();
|
||||||
Reporter.Output.WriteLine($"> {_process.StartInfo.FileName} {_process.StartInfo.Arguments}".White());
|
Reporter.Output.WriteLine($"> {FormatProcessInfo(_process.StartInfo)}".White());
|
||||||
#endif
|
#endif
|
||||||
_process.Start();
|
_process.Start();
|
||||||
_process.BeginOutputReadLine();
|
_process.BeginOutputReadLine();
|
||||||
_process.BeginErrorReadLine();
|
_process.BeginErrorReadLine();
|
||||||
|
|
||||||
var exitCode = await _processTcs.Task;
|
_process.WaitForExit();
|
||||||
|
|
||||||
|
var exitCode = _process.ExitCode;
|
||||||
|
|
||||||
#if DEBUG
|
#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)
|
if (exitCode == 0)
|
||||||
{
|
{
|
||||||
Reporter.Output.WriteLine(message.Green().Bold());
|
Reporter.Output.WriteLine(message.Green().Bold());
|
||||||
|
@ -206,6 +206,16 @@ namespace Microsoft.DotNet.Cli.Utils
|
||||||
return this;
|
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)
|
private void ThrowIfRunning([CallerMemberName] string memberName = null)
|
||||||
{
|
{
|
||||||
if (_running)
|
if (_running)
|
||||||
|
|
|
@ -24,8 +24,7 @@ namespace Microsoft.DotNet.Cli
|
||||||
return Command.Create("dotnet-" + args[1], "--help")
|
return Command.Create("dotnet-" + args[1], "--help")
|
||||||
.ForwardStdErr()
|
.ForwardStdErr()
|
||||||
.ForwardStdOut()
|
.ForwardStdOut()
|
||||||
.RunAsync()
|
.Execute()
|
||||||
.Result
|
|
||||||
.ExitCode;
|
.ExitCode;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -39,8 +38,7 @@ namespace Microsoft.DotNet.Cli
|
||||||
return Command.Create("dotnet-" + args[0], args.Skip(1))
|
return Command.Create("dotnet-" + args[0], args.Skip(1))
|
||||||
.ForwardStdErr()
|
.ForwardStdErr()
|
||||||
.ForwardStdOut()
|
.ForwardStdOut()
|
||||||
.RunAsync()
|
.Execute()
|
||||||
.Result
|
|
||||||
.ExitCode;
|
.ExitCode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,8 +25,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Csc
|
||||||
var result = RunCsc($"-noconfig @\"{responseFileArg.Value}\"")
|
var result = RunCsc($"-noconfig @\"{responseFileArg.Value}\"")
|
||||||
.ForwardStdErr()
|
.ForwardStdErr()
|
||||||
.ForwardStdOut()
|
.ForwardStdOut()
|
||||||
.RunAsync()
|
.Execute();
|
||||||
.Result;
|
|
||||||
|
|
||||||
return result.ExitCode;
|
return result.ExitCode;
|
||||||
});
|
});
|
||||||
|
|
|
@ -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}")
|
var compileResult = Command.Create("dotnet-compile", $"--framework {projectDependency.Framework} --configuration {configuration} --no-project-dependencies {projectDependency.Project.ProjectDirectory}")
|
||||||
.ForwardStdOut()
|
.ForwardStdOut()
|
||||||
.ForwardStdErr()
|
.ForwardStdErr()
|
||||||
.RunAsync()
|
.Execute();
|
||||||
.Result;
|
|
||||||
|
|
||||||
if (compileResult.ExitCode != 0)
|
if (compileResult.ExitCode != 0)
|
||||||
{
|
{
|
||||||
|
@ -223,9 +222,7 @@ namespace Microsoft.DotNet.Tools.Compiler
|
||||||
Console.Out.WriteLine(line);
|
Console.Out.WriteLine(line);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.RunAsync()
|
.Execute();
|
||||||
.GetAwaiter()
|
|
||||||
.GetResult();
|
|
||||||
|
|
||||||
foreach (var diag in diagnostics)
|
foreach (var diag in diagnostics)
|
||||||
{
|
{
|
||||||
|
@ -300,9 +297,7 @@ namespace Microsoft.DotNet.Tools.Compiler
|
||||||
var result = Command.Create("resgen", $"{fileName} {resourcesFile}")
|
var result = Command.Create("resgen", $"{fileName} {resourcesFile}")
|
||||||
.ForwardStdErr()
|
.ForwardStdErr()
|
||||||
.ForwardStdOut()
|
.ForwardStdOut()
|
||||||
.RunAsync()
|
.Execute();
|
||||||
.GetAwaiter()
|
|
||||||
.GetResult();
|
|
||||||
|
|
||||||
if (result.ExitCode != 0)
|
if (result.ExitCode != 0)
|
||||||
{
|
{
|
||||||
|
|
|
@ -101,8 +101,7 @@ namespace Microsoft.DotNet.Tools.Publish
|
||||||
var result = Command.Create("dotnet-compile", $"--framework {context.TargetFramework.DotNetFrameworkName} {context.ProjectFile.ProjectDirectory}")
|
var result = Command.Create("dotnet-compile", $"--framework {context.TargetFramework.DotNetFrameworkName} {context.ProjectFile.ProjectDirectory}")
|
||||||
.ForwardStdErr()
|
.ForwardStdErr()
|
||||||
.ForwardStdOut()
|
.ForwardStdOut()
|
||||||
.RunAsync()
|
.Execute();
|
||||||
.Result;
|
|
||||||
|
|
||||||
if (result.ExitCode != 0)
|
if (result.ExitCode != 0)
|
||||||
{
|
{
|
||||||
|
@ -187,9 +186,7 @@ exec ""$DIR/corerun"" ""$DIR/{context.ProjectFile.Name}.exe"" $*
|
||||||
Command.Create("chmod", $"a+x {outputExe}")
|
Command.Create("chmod", $"a+x {outputExe}")
|
||||||
.ForwardStdOut()
|
.ForwardStdOut()
|
||||||
.ForwardStdErr()
|
.ForwardStdErr()
|
||||||
.RunAsync()
|
.Execute();
|
||||||
.GetAwaiter()
|
|
||||||
.GetResult();
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue