aboutsummaryrefslogtreecommitdiff
path: root/src/wix/test/WixToolsetTest.Sdk/MsbuildUtilities.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/wix/test/WixToolsetTest.Sdk/MsbuildUtilities.cs')
-rw-r--r--src/wix/test/WixToolsetTest.Sdk/MsbuildUtilities.cs98
1 files changed, 98 insertions, 0 deletions
diff --git a/src/wix/test/WixToolsetTest.Sdk/MsbuildUtilities.cs b/src/wix/test/WixToolsetTest.Sdk/MsbuildUtilities.cs
new file mode 100644
index 00000000..2e07af3a
--- /dev/null
+++ b/src/wix/test/WixToolsetTest.Sdk/MsbuildUtilities.cs
@@ -0,0 +1,98 @@
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
3namespace WixToolsetTest.Sdk
4{
5 using System;
6 using System.Collections.Generic;
7 using System.IO;
8 using System.Linq;
9 using WixBuildTools.TestSupport;
10
11 public enum BuildSystem
12 {
13 DotNetCoreSdk,
14 MSBuild,
15 MSBuild64,
16 }
17
18 public static class MsbuildUtilities
19 {
20 public static readonly string WixMsbuildPath = Path.Combine(Path.GetDirectoryName(new Uri(typeof(MsbuildUtilities).Assembly.CodeBase).AbsolutePath), "..", "publish", "WixToolset.Sdk");
21 public static readonly string WixPropsPath = Path.Combine(WixMsbuildPath, "build", "WixToolset.Sdk.props");
22
23 public static MsbuildRunnerResult BuildProject(BuildSystem buildSystem, string projectPath, string[] arguments = null, string configuration = "Release", bool? outOfProc = null, string verbosityLevel = "normal")
24 {
25 var allArgs = new List<string>
26 {
27 $"-verbosity:{verbosityLevel}",
28 $"-p:Configuration={configuration}",
29 GetQuotedPropertySwitch(buildSystem, "WixMSBuildProps", MsbuildUtilities.WixPropsPath),
30 // Node reuse means that child msbuild processes can stay around after the build completes.
31 // Under that scenario, the root msbuild does not reliably close its streams which causes us to hang.
32 "-nr:false",
33 };
34
35 if (outOfProc.HasValue)
36 {
37 allArgs.Add($"-p:RunWixToolsOutOfProc={outOfProc.Value}");
38 }
39
40 if (arguments != null)
41 {
42 allArgs.AddRange(arguments);
43 }
44
45 switch (buildSystem)
46 {
47 case BuildSystem.DotNetCoreSdk:
48 {
49 allArgs.Add(projectPath);
50 var result = DotnetRunner.Execute("msbuild", allArgs.ToArray());
51 return new MsbuildRunnerResult
52 {
53 ExitCode = result.ExitCode,
54 Output = result.StandardOutput,
55 };
56 }
57 case BuildSystem.MSBuild:
58 case BuildSystem.MSBuild64:
59 {
60 return MsbuildRunner.Execute(projectPath, allArgs.ToArray(), buildSystem == BuildSystem.MSBuild64);
61 }
62 default:
63 {
64 throw new NotImplementedException();
65 }
66 }
67 }
68
69 public static string GetQuotedPropertySwitch(BuildSystem buildSystem, string propertyName, string valueToQuote)
70 {
71 switch (buildSystem)
72 {
73 case BuildSystem.DotNetCoreSdk:
74 {
75 return $"-p:{propertyName}=\\\"{valueToQuote}\\\"";
76 }
77 case BuildSystem.MSBuild:
78 case BuildSystem.MSBuild64:
79 {
80 return $"-p:{propertyName}=\"{valueToQuote}\"";
81 }
82 default:
83 {
84 throw new NotImplementedException();
85 }
86 }
87 }
88
89 public static IEnumerable<string> GetToolCommandLines(MsbuildRunnerResult result, string toolName, string operation, BuildSystem buildSystem, bool? outOfProc = null)
90 {
91 var expectedOutOfProc = buildSystem == BuildSystem.DotNetCoreSdk || outOfProc.HasValue && outOfProc.Value;
92 var expectedToolExe = !expectedOutOfProc ? $"({toolName}.exe)" :
93 buildSystem == BuildSystem.DotNetCoreSdk ? $"{toolName}.dll\"" : $"{toolName}.exe";
94 var expectedToolCommand = $"{expectedToolExe} {operation}";
95 return result.Output.Where(line => line.Contains(expectedToolCommand));
96 }
97 }
98}