| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixInternal.TestSupport |
| 4 | { |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using System.IO; |
| 8 | |
| 9 | public class MsbuildRunner : ExternalExecutable |
| 10 | { |
| 11 | private static readonly string VswhereFindArguments = "-property installationPath -version [17.0,18.0)"; |
| 12 | private static readonly string MsbuildCurrentRelativePath = @"MSBuild\Current\Bin\MSBuild.exe"; |
| 13 | private static readonly string MsbuildCurrentRelativePath64 = @"MSBuild\Current\Bin\amd64\MSBuild.exe"; |
| 14 | |
| 15 | private static readonly object InitLock = new object(); |
| 16 | |
| 17 | private static bool Initialized; |
| 18 | private static MsbuildRunner MsbuildCurrentRunner; |
| 19 | private static MsbuildRunner MsbuildCurrentRunner64; |
| 20 | |
| 21 | public static MsbuildRunnerResult Execute(string projectPath, string[] arguments = null, bool x64 = false) => |
| 22 | InitAndExecute(String.Empty, projectPath, arguments, x64); |
| 23 | |
| 24 | public static MsbuildRunnerResult ExecuteWithMsbuildCurrent(string projectPath, string[] arguments = null, bool x64 = false) => |
| 25 | InitAndExecute("Current", projectPath, arguments, x64); |
| 26 | |
| 27 | private static MsbuildRunnerResult InitAndExecute(string msbuildVersion, string projectPath, string[] arguments, bool x64) |
| 28 | { |
| 29 | lock (InitLock) |
| 30 | { |
| 31 | if (!Initialized) |
| 32 | { |
| 33 | Initialized = true; |
| 34 | var vswhereResult = VswhereRunner.Execute(VswhereFindArguments, true); |
| 35 | if (vswhereResult.ExitCode != 0) |
| 36 | { |
| 37 | throw new InvalidOperationException($"Failed to execute vswhere.exe, exit code: {vswhereResult.ExitCode}. Output:\r\n{String.Join("\r\n", vswhereResult.StandardOutput)}"); |
| 38 | } |
| 39 | |
| 40 | string msbuildCurrentPath = null; |
| 41 | string msbuildCurrentPath64 = null; |
| 42 | |
| 43 | foreach (var installPath in vswhereResult.StandardOutput) |
| 44 | { |
| 45 | if (msbuildCurrentPath == null) |
| 46 | { |
| 47 | var path = Path.Combine(installPath, MsbuildCurrentRelativePath); |
| 48 | if (File.Exists(path)) |
| 49 | { |
| 50 | msbuildCurrentPath = path; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | if (msbuildCurrentPath64 == null) |
| 55 | { |
| 56 | var path = Path.Combine(installPath, MsbuildCurrentRelativePath64); |
| 57 | if (File.Exists(path)) |
| 58 | { |
| 59 | msbuildCurrentPath64 = path; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (msbuildCurrentPath != null) |
| 65 | { |
| 66 | MsbuildCurrentRunner = new MsbuildRunner(msbuildCurrentPath); |
| 67 | } |
| 68 | |
| 69 | if (msbuildCurrentPath64 != null) |
| 70 | { |
| 71 | MsbuildCurrentRunner64 = new MsbuildRunner(msbuildCurrentPath64); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | MsbuildRunner runner = x64 ? MsbuildCurrentRunner64 : MsbuildCurrentRunner; |
| 77 | |
| 78 | if (runner == null) |
| 79 | { |
| 80 | throw new InvalidOperationException($"Failed to find an installed{(x64 ? " 64-bit" : String.Empty)} MSBuild{msbuildVersion}"); |
| 81 | } |
| 82 | |
| 83 | return runner.ExecuteCore(projectPath, arguments); |
| 84 | } |
| 85 | |
| 86 | private MsbuildRunner(string exePath) : base(exePath) { } |
| 87 | |
| 88 | private MsbuildRunnerResult ExecuteCore(string projectPath, string[] arguments) |
| 89 | { |
| 90 | var total = new List<string> |
| 91 | { |
| 92 | projectPath, |
| 93 | }; |
| 94 | |
| 95 | if (arguments != null) |
| 96 | { |
| 97 | total.AddRange(arguments); |
| 98 | } |
| 99 | |
| 100 | var args = CombineArguments(total); |
| 101 | var mergeErrorIntoOutput = true; |
| 102 | var workingFolder = Path.GetDirectoryName(projectPath); |
| 103 | var result = this.Run(args, mergeErrorIntoOutput, workingFolder); |
| 104 | |
| 105 | return new MsbuildRunnerResult |
| 106 | { |
| 107 | ExitCode = result.ExitCode, |
| 108 | Output = result.StandardOutput, |
| 109 | }; |
| 110 | } |
| 111 | } |
| 112 | } |