| 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 WixToolsetTest.Sdk |
| 4 | { |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using System.IO; |
| 8 | using System.Linq; |
| 9 | using WixInternal.TestSupport; |
| 10 | |
| 11 | public enum BuildSystem |
| 12 | { |
| 13 | DotNetCoreSdk, |
| 14 | MSBuild, |
| 15 | MSBuild64, |
| 16 | } |
| 17 | |
| 18 | public static class MsbuildUtilities |
| 19 | { |
| 20 | public static MsbuildRunnerResult BuildProject(BuildSystem buildSystem, string projectPath, string[] arguments = null, string configuration = "Release", string verbosityLevel = "normal", bool suppressValidation = true) |
| 21 | { |
| 22 | var allArgs = new List<string> |
| 23 | { |
| 24 | $"-verbosity:{verbosityLevel}", |
| 25 | $"-p:Configuration={configuration}", |
| 26 | $"-p:SuppressValidation={suppressValidation}", |
| 27 | // Node reuse means that child msbuild processes can stay around after the build completes. |
| 28 | // Under that scenario, the root msbuild does not reliably close its streams which causes us to hang. |
| 29 | "-nr:false", |
| 30 | MsbuildUtilities.GetQuotedSwitch(buildSystem, "bl", Path.ChangeExtension(projectPath, ".binlog")) |
| 31 | }; |
| 32 | |
| 33 | if (arguments != null) |
| 34 | { |
| 35 | allArgs.AddRange(arguments); |
| 36 | } |
| 37 | |
| 38 | switch (buildSystem) |
| 39 | { |
| 40 | case BuildSystem.DotNetCoreSdk: |
| 41 | { |
| 42 | allArgs.Add(projectPath); |
| 43 | var result = DotnetRunner.Execute("msbuild", allArgs.ToArray()); |
| 44 | return new MsbuildRunnerResult |
| 45 | { |
| 46 | ExitCode = result.ExitCode, |
| 47 | Output = result.StandardOutput, |
| 48 | }; |
| 49 | } |
| 50 | case BuildSystem.MSBuild: |
| 51 | case BuildSystem.MSBuild64: |
| 52 | { |
| 53 | return MsbuildRunner.Execute(projectPath, allArgs.ToArray(), buildSystem == BuildSystem.MSBuild64); |
| 54 | } |
| 55 | default: |
| 56 | { |
| 57 | throw new NotImplementedException(); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | public static string GetQuotedSwitch(BuildSystem _, string switchName, string switchValue) |
| 63 | { |
| 64 | // If the value ends with a backslash, escape it. |
| 65 | if (switchValue?.EndsWith("\\") == true) |
| 66 | { |
| 67 | switchValue += @"\"; |
| 68 | } |
| 69 | |
| 70 | return $"-{switchName}:\"{switchValue}\""; |
| 71 | } |
| 72 | |
| 73 | public static string GetQuotedPropertySwitch(BuildSystem buildSystem, string propertyName, string propertyValue) |
| 74 | { |
| 75 | // If the value ends with a backslash, escape it. |
| 76 | if (propertyValue?.EndsWith("\\") == true) |
| 77 | { |
| 78 | propertyValue += @"\"; |
| 79 | } |
| 80 | |
| 81 | var quotedValue = "\"" + propertyValue + "\""; |
| 82 | |
| 83 | // If the value contains a semicolon then escape-quote it (wrap with the characters: \") to wrap the value |
| 84 | // instead of just quoting the value, otherwise dotnet.exe will not pass the value to MSBuild correctly. |
| 85 | if (buildSystem == BuildSystem.DotNetCoreSdk && propertyValue?.IndexOf(';') > -1) |
| 86 | { |
| 87 | quotedValue = "\\\"" + propertyValue + "\\\""; |
| 88 | } |
| 89 | |
| 90 | return $"-p:{propertyName}={quotedValue}"; |
| 91 | } |
| 92 | |
| 93 | public static IEnumerable<string> GetToolCommandLines(MsbuildRunnerResult result, string toolName, string operation, BuildSystem buildSystem) |
| 94 | { |
| 95 | var expectedToolExe = buildSystem == BuildSystem.DotNetCoreSdk ? $"{toolName}.dll\"" : $"{toolName}.exe"; |
| 96 | var expectedToolCommand = $"{expectedToolExe} {operation}"; |
| 97 | return result.Output.Where(line => line.Contains(expectedToolCommand)); |
| 98 | } |
| 99 | } |
| 100 | } |