Как определить состояние msbuild из командной строки или приложения C # - PullRequest
23 голосов
/ 10 ноября 2011

Я пишу приложение проверки, сборки и развертывания на C #, и мне нужно знать, как лучше всего определить, был ли мой вызов на msbuild.exe успешным или нет.Я пытался использовать код ошибки из процесса, но я не уверен, что он всегда точный.

Есть ли способ (с помощью приведенного ниже кода), который я могу сказать, успешно ли завершено msbuild.exe?

try
{
    Process msbProcess = new Process();
    msbProcess.StartInfo.FileName = this.MSBuildPath;
    msbProcess.StartInfo.Arguments = msbArguments;
    msbProcess.Start();
    msbProcess.WaitForExit();

    if (msbProcess.ExitCode != 0)
    {
        //
    }
    else
    {
        //
    }

    msbProcess.Close();
}
catch (Exception ex)
{
    //
}

Ответы [ 2 ]

31 голосов
/ 10 ноября 2011

Насколько я смог определить, MSBuild возвращает код выхода> 0, когда обнаруживает ошибку. Если он не обнаружил никаких ошибок, он возвращает код выхода 0. Я никогда не видел его выхода с кодом <0. </p>

Я использую его в командном файле:

msbuild <args>
if errorlevel 1 goto errorDone

За четыре года использования этого способа у меня никогда не было оснований сомневаться в правильности этого подхода.

Несколько вопросов на форумах MSDN задают одно и то же. Например: http://social.msdn.microsoft.com/forums/en-US/msbuild/thread/a4ae6b2b-9b1f-4e59-86b4-370f44d73a85. По сути, стандартным ответом является «если errorlevel равен 0, то ошибки не было».

3 голосов
/ 17 июля 2018

Извините, если я немного опоздал на вечеринку ... но спустя почти 7 лет после того, как вопрос был опубликован, я хотел получить полный ответ на него. Я провел несколько тестов, используя приведенный ниже код, и вот выводы:


Анализ

msbuild.exe возвращает 1 при возникновении хотя бы одной ошибки сборки и возвращает 0 при успешном завершении сборки. В настоящее время программа не учитывает предупреждения, что означает, что успешная сборка с предупреждениями заставляет msbuild.exe по-прежнему возвращать 0.

Другие ошибки, такие как: попытка создать несуществующий проект или предоставление неверного аргумента (например, /myInvalidArgument), также приведет к тому, что msbuild.exe вернет 1.


Исходный код

Следующий код C # является полной реализацией для создания ваших любимых проектов путем запуска msbuild.exe из командной строки. Не забудьте настроить все необходимые параметры среды перед компиляцией проектов.

Ваш BuildControl класс:

using System;

namespace Example
{
    public sealed class BuildControl
    {
        // ...

        public bool BuildStuff()
        {
            MsBuilder builder = new MsBuilder(@"C:\...\project.csproj", "Release", "x86")
            {
                Target = "Rebuild", // for rebuilding instead of just building
            };
            bool success = builder.Build(out string buildOutput);
            Console.WriteLine(buildOutput);
            return success;
        }

        // ...
    }
}

MsBuilder класс: Строит вещи, вызывая MsBuild.exe из командной строки:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Example
{
    public sealed class MsBuilder
    {
        public string ProjectPath { get; }
        public string LogPath { get; set; }

        public string Configuration { get; }
        public string Platform { get; }

        public int MaxCpuCount { get; set; } = 1;
        public string Target { get; set; } = "Build";

        public string MsBuildPath { get; set; } =
            @"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\MsBuild.exe";

        public string BuildOutput { get; private set; }

        public MsBuilder(string projectPath, string configuration, string platform)
        {
            ProjectPath = !string.IsNullOrWhiteSpace(projectPath) ? projectPath : throw new ArgumentNullException(nameof(projectPath));
            if (!File.Exists(ProjectPath)) throw new FileNotFoundException(projectPath);
            Configuration = !string.IsNullOrWhiteSpace(configuration) ? configuration : throw new ArgumentNullException(nameof(configuration));
            Platform = !string.IsNullOrWhiteSpace(platform) ? platform : throw new ArgumentNullException(nameof(platform));
            LogPath = Path.Combine(Path.GetDirectoryName(ProjectPath), $"{Path.GetFileName(ProjectPath)}.{Configuration}-{Platform}.msbuild.log");
        }

        public bool Build(out string buildOutput)
        {
            List<string> arguments = new List<string>()
            {
                $"/nologo",
                $"\"{ProjectPath}\"",
                $"/p:Configuration={Configuration}",
                $"/p:Platform={Platform}",
                $"/t:{Target}",
                $"/maxcpucount:{(MaxCpuCount > 0 ? MaxCpuCount : 1)}",
                $"/fileLoggerParameters:LogFile=\"{LogPath}\";Append;Verbosity=diagnostic;Encoding=UTF-8",
            };

            using (CommandLineProcess cmd = new CommandLineProcess(MsBuildPath, string.Join(" ", arguments)))
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine($"Build started: Project: '{ProjectPath}', Configuration: {Configuration}, Platform: {Platform}");

                // Call MsBuild:
                int exitCode = cmd.Run(out string processOutput, out string processError);

                // Check result:
                sb.AppendLine(processOutput);
                if (exitCode == 0)
                {
                    sb.AppendLine("Build completed successfully!");
                    buildOutput = sb.ToString();
                    return true;
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(processError))
                        sb.AppendLine($"MSBUILD PROCESS ERROR: {processError}");
                    sb.AppendLine("Build failed!");
                    buildOutput = sb.ToString();
                    return false;
                }
            }
        }

    }
}

CommandLineProcess class - запускает процесс командной строки и ожидает его завершения. Все стандартные выходные данные / ошибки фиксируются, и отдельное окно для процесса не запускается:

using System;
using System.Diagnostics;
using System.IO;

namespace Example
{
    public sealed class CommandLineProcess : IDisposable
    {
        public string Path { get; }
        public string Arguments { get; }
        public bool IsRunning { get; private set; }
        public int? ExitCode { get; private set; }

        private Process Process;
        private readonly object Locker = new object();

        public CommandLineProcess(string path, string arguments)
        {
            Path = path ?? throw new ArgumentNullException(nameof(path));
            if (!File.Exists(path)) throw new ArgumentException($"Executable not found: {path}");
            Arguments = arguments;
        }

        public int Run(out string output, out string err)
        {
            lock (Locker)
            {
                if (IsRunning) throw new Exception("The process is already running");

                Process = new Process()
                {
                    EnableRaisingEvents = true,
                    StartInfo = new ProcessStartInfo()
                    {
                        FileName = Path,
                        Arguments = Arguments,
                        UseShellExecute = false,
                        RedirectStandardOutput = true,
                        RedirectStandardError = true,
                        CreateNoWindow = true,
                    },
                };

                if (!Process.Start()) throw new Exception("Process could not be started");
                output = Process.StandardOutput.ReadToEnd();
                err = Process.StandardError.ReadToEnd();
                Process.WaitForExit();
                try { Process.Refresh(); } catch { }
                return (ExitCode = Process.ExitCode).Value;
            }
        }

        public void Kill()
        {
            lock (Locker)
            {
                try { Process?.Kill(); }
                catch { }
                IsRunning = false;
                Process = null;
            }
        }

        public void Dispose()
        {
            try { Process?.Dispose(); }
            catch { }
        }
    }
}

PS : я использую Visual Studio 2017 / .NET 4.7.2

...