dotnet-installer/scripts/Microsoft.DotNet.Cli.Build.Framework/CommandResult.cs
Eric Erhardt 9a4936ae0d When a failed command is executed in "silent" mode, it is not easy to figure out why it failed in the build logs.
Fix: Add Standard Output and Error to the BuildFailureException message thrown by CommandResult.EnsureSuccessful.
2016-03-14 15:20:35 -05:00

47 lines
No EOL
1.5 KiB
C#

// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Text;
using System.Diagnostics;
namespace Microsoft.DotNet.Cli.Build.Framework
{
public struct CommandResult
{
public static readonly CommandResult Empty = new CommandResult();
public ProcessStartInfo StartInfo { get; }
public int ExitCode { get; }
public string StdOut { get; }
public string StdErr { get; }
public CommandResult(ProcessStartInfo startInfo, int exitCode, string stdOut, string stdErr)
{
StartInfo = startInfo;
ExitCode = exitCode;
StdOut = stdOut;
StdErr = stdErr;
}
public void EnsureSuccessful()
{
if(ExitCode != 0)
{
StringBuilder message = new StringBuilder($"Command failed with exit code {ExitCode}: {StartInfo.FileName} {StartInfo.Arguments}");
if (!string.IsNullOrEmpty(StdOut))
{
message.AppendLine($"{Environment.NewLine}Standard Output:{Environment.NewLine}{StdOut}");
}
if (!string.IsNullOrEmpty(StdErr))
{
message.AppendLine($"{Environment.NewLine}Standard Error:{Environment.NewLine}{StdErr}");
}
throw new BuildFailureException(message.ToString());
}
}
}
}