public static DotNetCliCommandResult Execute(DotNetCliCommand parameters) { using (var process = new Process { StartInfo = BuildStartInfo(parameters.CliPath, parameters.GenerateResult.ArtifactsPaths.BuildArtifactsDirectoryPath, parameters.Arguments, parameters.EnvironmentVariables) }) using (var outputReader = new AsyncProcessOutputReader(process, parameters.LogOutput, parameters.Logger)) using (new ConsoleExitHandler(process, parameters.Logger)) { parameters.Logger.WriteLineInfo($"// start {parameters.CliPath ?? "dotnet"} {parameters.Arguments} in {parameters.GenerateResult.ArtifactsPaths.BuildArtifactsDirectoryPath}"); var stopwatch = Stopwatch.StartNew(); process.Start(); outputReader.BeginRead(); if (!process.WaitForExit((int)parameters.Timeout.TotalMilliseconds)) { parameters.Logger.WriteLineError($"// command took longer than the timeout: {parameters.Timeout.TotalSeconds:0.##}s. Killing the process tree!"); outputReader.CancelRead(); process.KillTree(); return(DotNetCliCommandResult.Failure(stopwatch.Elapsed, $"The configured timeout {parameters.Timeout} was reached!" + outputReader.GetErrorText(), outputReader.GetOutputText())); } stopwatch.Stop(); outputReader.StopRead(); parameters.Logger.WriteLineInfo($"// command took {stopwatch.Elapsed.TotalSeconds:0.##}s and exited with {process.ExitCode}"); return(process.ExitCode <= 0 ? DotNetCliCommandResult.Success(stopwatch.Elapsed, outputReader.GetOutputText()) : DotNetCliCommandResult.Failure(stopwatch.Elapsed, outputReader.GetOutputText(), outputReader.GetErrorText())); } }
public DotNetCliCommandResult AddPackages() { var executionTime = new TimeSpan(0); var stdOutput = new StringBuilder(); foreach (var cmd in GetAddPackagesCommands(BuildPartition)) { var result = DotNetCliCommandExecutor.Execute(WithArguments(cmd)); if (!result.IsSuccess) { return(result); } executionTime += result.ExecutionTime; stdOutput.Append(result.StandardOutput); } return(DotNetCliCommandResult.Success(executionTime, stdOutput.ToString())); }
public static DotNetCliCommandResult Execute(DotNetCliCommand parameters) { using (var process = new Process { StartInfo = BuildStartInfo(parameters.CliPath, parameters.GenerateResult.ArtifactsPaths.BuildArtifactsDirectoryPath, parameters.Arguments, parameters.EnvironmentVariables) }) { parameters.Logger.WriteLineInfo($"// start {parameters.CliPath ?? "dotnet"} {parameters.Arguments} in {parameters.GenerateResult.ArtifactsPaths.BuildArtifactsDirectoryPath}"); var standardOutput = new StringBuilder(1000); var standardError = new StringBuilder(); process.OutputDataReceived += (sender, args) => standardOutput.AppendLine(args.Data); process.ErrorDataReceived += (sender, args) => standardError.AppendLine(args.Data); var stopwatch = Stopwatch.StartNew(); process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); if (!process.WaitForExit((int)parameters.Timeout.TotalMilliseconds)) { parameters.Logger.WriteLineError($"// command took more that the timeout: {parameters.Timeout.TotalSeconds:0.##}s. Killing the process tree!"); process.KillTree(); return(DotNetCliCommandResult.Failure(stopwatch.Elapsed, $"The configured timeout {parameters.Timeout} was reached!" + standardError.ToString(), standardOutput.ToString())); } stopwatch.Stop(); parameters.Logger.WriteLineInfo($"// command took {stopwatch.Elapsed.TotalSeconds:0.##}s and exited with {process.ExitCode}"); return(process.ExitCode <= 0 ? DotNetCliCommandResult.Success(stopwatch.Elapsed, standardOutput.ToString()) : DotNetCliCommandResult.Failure(stopwatch.Elapsed, standardError.ToString(), standardOutput.ToString())); } }
public static DotNetCliCommandResult Execute(DotNetCliCommand parameters) { using (var process = new Process { StartInfo = BuildStartInfo(parameters.CliPath, parameters.GenerateResult.ArtifactsPaths.BuildArtifactsDirectoryPath, parameters.Arguments, parameters.EnvironmentVariables) }) { parameters.Logger.WriteLineInfo($"// start {parameters.CliPath ?? "dotnet"} {parameters.Arguments} in {parameters.GenerateResult.ArtifactsPaths.BuildArtifactsDirectoryPath}"); var stopwatch = Stopwatch.StartNew(); process.Start(); string standardOutput = process.StandardOutput.ReadToEnd(); string standardError = process.StandardError.ReadToEnd(); process.WaitForExit(); stopwatch.Stop(); parameters.Logger.WriteLineInfo($"// command took {stopwatch.Elapsed.TotalSeconds:0.##}s and exited with {process.ExitCode}"); return(process.ExitCode <= 0 ? DotNetCliCommandResult.Success(stopwatch.Elapsed, standardOutput) : DotNetCliCommandResult.Failure(stopwatch.Elapsed, standardError, standardOutput)); } }