dotnet-installer/scripts/Microsoft.DotNet.Cli.Build.Framework/CommandResult.cs

50 lines
1.6 KiB
C#
Raw Normal View History

2016-02-02 18:04:50 +00:00
// 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;
2016-02-02 18:04:50 +00:00
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(bool suppressOutput = false)
2016-02-02 18:04:50 +00:00
{
if(ExitCode != 0)
{
StringBuilder message = new StringBuilder($"Command failed with exit code {ExitCode}: {StartInfo.FileName} {StartInfo.Arguments}");
if (!suppressOutput)
{
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());
2016-02-02 18:04:50 +00:00
}
}
}
}