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;
|
2016-03-14 20:20:35 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 20:24:36 +00:00
|
|
|
|
public void EnsureSuccessful(bool suppressOutput = false)
|
2016-02-02 18:04:50 +00:00
|
|
|
|
{
|
|
|
|
|
if(ExitCode != 0)
|
|
|
|
|
{
|
2016-03-14 20:20:35 +00:00
|
|
|
|
StringBuilder message = new StringBuilder($"Command failed with exit code {ExitCode}: {StartInfo.FileName} {StartInfo.Arguments}");
|
|
|
|
|
|
2016-03-07 20:24:36 +00:00
|
|
|
|
if (!suppressOutput)
|
2016-03-14 20:20:35 +00:00
|
|
|
|
{
|
2016-03-07 20:24:36 +00:00
|
|
|
|
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}");
|
|
|
|
|
}
|
2016-03-14 20:20:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new BuildFailureException(message.ToString());
|
2016-02-02 18:04:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|