public async Task <string> ExecuteAsync(string buildConfiguration, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var projectDir = Path.GetDirectoryName(_projectFile); OutputCapture capture = _outputSink.StartCapture(); IEnumerable <string> args = new[] { "msbuild", _projectFile, } .Concat(BuildFlags); if (buildConfiguration != null) { args = args.Append($"/p:Configuration=\"{buildConfiguration}\""); } var processSpec = new ProcessSpec { Executable = DotNetMuxer.MuxerPathOrDefault(), WorkingDirectory = projectDir, Arguments = args, OutputCapture = capture }; _reporter.Verbose($"Running MSBuild target '{TargetName}' on '{_projectFile}'"); var exitCode = await _processRunner.RunAsync(processSpec, cancellationToken); if (exitCode != 0) { _reporter.Error($"Failed to build the project file '{Path.GetFileName(_projectFile)}'"); DumpMSBuildOutput(capture); return(null); } Match targetPathMatch = capture.Lines .Select(line => Regex.Match(line, @"Bundling\.TargetPath=<(.+)>")) .FirstOrDefault(match => match.Success); if (targetPathMatch == null) { _reporter.Error("Failed to determine the path of the output assembly."); DumpMSBuildOutput(capture); return(null); } return(targetPathMatch.Groups[1].Value); }
// May not be necessary in the future. See https://github.com/dotnet/corefx/issues/12039 public async Task <int> RunAsync(ProcessSpec processSpec, CancellationToken cancellationToken) { if (processSpec == null) { throw new ArgumentNullException(nameof(processSpec)); } int exitCode; var stopwatch = new Stopwatch(); using (Process process = CreateProcess(processSpec)) using (var processState = new ProcessState(process)) { cancellationToken.Register(() => processState.TryKill()); stopwatch.Start(); process.Start(); _reporter.Verbose($"Started '{processSpec.Executable}' with process id {process.Id}"); if (processSpec.IsOutputCaptured) { await Task.WhenAll( processState.Task, ConsumeStreamAsync(process.StandardOutput, processSpec.OutputCapture.AddLine), ConsumeStreamAsync(process.StandardError, processSpec.OutputCapture.AddLine) ); } else { await processState.Task; } exitCode = process.ExitCode; stopwatch.Stop(); _reporter.Verbose($"Process id {process.Id} ran for {stopwatch.ElapsedMilliseconds}ms"); } return(exitCode); }
private Process CreateProcess(ProcessSpec processSpec) { var process = new Process { EnableRaisingEvents = true, StartInfo = { FileName = processSpec.Executable, Arguments = ArgumentEscaper.EscapeAndConcatenate(processSpec.Arguments), UseShellExecute = false, WorkingDirectory = processSpec.WorkingDirectory, RedirectStandardOutput = processSpec.IsOutputCaptured, RedirectStandardError = processSpec.IsOutputCaptured, } }; foreach (System.Collections.Generic.KeyValuePair <string, string> env in processSpec.EnvironmentVariables) { process.StartInfo.Environment.Add(env.Key, env.Value); } return(process); }