@joebigelow / wix / commits / 8f288810

Support MSBuild16 in runner

Rob Mensching committed May 7, 2019 at 23:01 UTC 8f288810a4bdeb7a397073ac9682f0478583be44
4 files changed +71 -24
src/WixBuildTools.MsgGen/WixBuildTools.MsgGen.csproj
+1 -1
@@ -18,7 +18,7 @@
18 </ItemGroup>
19
20 <ItemGroup>
21 - <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta-63102-01" PrivateAssets="All" />
21 + <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta2-18618-05" PrivateAssets="All" />
22 <PackageReference Include="Nerdbank.GitVersioning" Version="2.1.65" PrivateAssets="All" />
23 </ItemGroup>
24
src/WixBuildTools.TestSupport/MsbuildRunner.cs
+67 -20
@@ -8,37 +8,84 @@ namespace WixBuildTools.TestSupport
8 using System.IO;
9 using System.Text;
10
11 - public class MsbuildRunner
11 + public static class MsbuildRunner
12 {
13 private static readonly string VswhereRelativePath = @"Microsoft Visual Studio\Installer\vswhere.exe";
14 - private static readonly string[] VswhereFindArguments = new[] { "-property", "installationPath", "-latest" };
15 - private static readonly string Msbuild15RelativePath = @"MSBuild\15.0\bin\MSBuild.exe";
14 + private static readonly string[] VswhereFindArguments = new[] { "-property", "installationPath" };
15 + private static readonly string Msbuild15RelativePath = @"MSBuild\15.0\Bin\MSBuild.exe";
16 + private static readonly string Msbuild16RelativePath = @"MSBuild\Current\Bin\MSBuild.exe";
17
17 - public MsbuildRunner()
18 + private static string Msbuild15Path;
19 + private static string Msbuild16Path;
20 +
21 + public static MsbuildRunnerResult Execute(string projectPath, string[] arguments = null) => InitAndExecute(String.Empty, projectPath, arguments);
22 +
23 + public static MsbuildRunnerResult ExecuteWithMsbuild15(string projectPath, string[] arguments = null) => InitAndExecute("15", projectPath, arguments);
24 +
25 + public static MsbuildRunnerResult ExecuteWithMsbuild16(string projectPath, string[] arguments = null) => InitAndExecute("16", projectPath, arguments);
26 +
27 + private static MsbuildRunnerResult InitAndExecute(string msbuildVersion, string projectPath, string[] arguments)
28 {
19 - var vswherePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), VswhereRelativePath);
20 - if (!File.Exists(vswherePath))
29 + if (Msbuild15Path == null && Msbuild16Path == null)
30 {
22 - throw new InvalidOperationException($"Failed to find vswhere at: {vswherePath}");
31 + var vswherePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), VswhereRelativePath);
32 + if (!File.Exists(vswherePath))
33 + {
34 + throw new InvalidOperationException($"Failed to find vswhere at: {vswherePath}");
35 + }
36 +
37 + var result = RunProcessCaptureOutput(vswherePath, VswhereFindArguments);
38 + if (result.ExitCode != 0)
39 + {
40 + throw new InvalidOperationException($"Failed to execute vswhere.exe, exit code: {result.ExitCode}");
41 + }
42 +
43 + Msbuild15Path = String.Empty;
44 + Msbuild16Path = String.Empty;
45 +
46 + foreach (var installPath in result.Output)
47 + {
48 + if (String.IsNullOrEmpty(Msbuild16Path))
49 + {
50 + var path = Path.Combine(installPath, Msbuild16RelativePath);
51 + if (File.Exists(path))
52 + {
53 + Msbuild16Path = path;
54 + }
55 + }
56 +
57 + if (String.IsNullOrEmpty(Msbuild15Path))
58 + {
59 + var path = Path.Combine(installPath, Msbuild15RelativePath);
60 + if (File.Exists(path))
61 + {
62 + Msbuild15Path = path;
63 + }
64 + }
65 + }
66 }
67
25 - var result = RunProcessCaptureOutput(vswherePath, VswhereFindArguments);
26 - if (result.ExitCode != 0)
68 + var msbuildPath = Msbuild15Path ?? Msbuild16Path;
69 +
70 + if (msbuildVersion == "15")
71 {
28 - throw new InvalidOperationException($"Failed to execute vswhere.exe, exit code: {result.ExitCode}");
72 + msbuildPath = Msbuild15Path;
73 }
30 -
31 - this.Msbuild15Path = Path.Combine(result.Output[0], Msbuild15RelativePath);
32 - if (!File.Exists(this.Msbuild15Path))
74 + else if (msbuildVersion == "16")
75 {
34 - throw new InvalidOperationException($"Failed to find MSBuild v15 at: {this.Msbuild15Path}");
76 + msbuildPath = Msbuild16Path;
77 }
36 - }
78
38 - private string Msbuild15Path { get; }
79 + return ExecuteCore(msbuildVersion, msbuildPath, projectPath, arguments);
80 + }
81
40 - public MsbuildRunnerResult Execute(string projectPath, string[] arguments = null)
82 + private static MsbuildRunnerResult ExecuteCore(string msbuildVersion, string msbuildPath, string projectPath, string[] arguments)
83 {
84 + if (String.IsNullOrEmpty(msbuildPath))
85 + {
86 + throw new InvalidOperationException($"Failed to find an installed MSBuild{msbuildVersion}");
87 + }
88 +
89 var total = new List<string>
90 {
91 projectPath
@@ -50,7 +97,7 @@ namespace WixBuildTools.TestSupport
97 }
98
99 var workingFolder = Path.GetDirectoryName(projectPath);
53 - return RunProcessCaptureOutput(this.Msbuild15Path, total.ToArray(), workingFolder);
100 + return RunProcessCaptureOutput(msbuildPath, total.ToArray(), workingFolder);
101 }
102
103 private static MsbuildRunnerResult RunProcessCaptureOutput(string executablePath, string[] arguments = null, string workingFolder = null)
@@ -70,8 +117,8 @@ namespace WixBuildTools.TestSupport
117
118 using (var process = Process.Start(startInfo))
119 {
73 - process.OutputDataReceived += (s, e) => { if (e.Data != null) output.Add(e.Data); };
74 - process.ErrorDataReceived += (s, e) => { if (e.Data != null) output.Add(e.Data); };
120 + process.OutputDataReceived += (s, e) => { if (e.Data != null) { output.Add(e.Data); } };
121 + process.ErrorDataReceived += (s, e) => { if (e.Data != null) { output.Add(e.Data); } };
122
123 process.BeginErrorReadLine();
124 process.BeginOutputReadLine();
src/WixBuildTools.TestSupport/WixBuildTools.TestSupport.csproj
+2 -2
@@ -16,12 +16,12 @@
16 </ItemGroup>
17
18 <ItemGroup>
19 - <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta-63102-01" PrivateAssets="All" />
19 + <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta2-18618-05 " PrivateAssets="All" />
20 <PackageReference Include="Nerdbank.GitVersioning" Version="2.1.65" PrivateAssets="All" />
21 </ItemGroup>
22
23 <ItemGroup>
24 - <PackageReference Include="xunit" Version="2.4.0" />
24 + <PackageReference Include="xunit.assert" Version="2.4.1" />
25 </ItemGroup>
26
27 </Project>
src/WixBuildTools.XsdGen/WixBuildTools.XsdGen.csproj
+1 -1
@@ -19,7 +19,7 @@
19 </ItemGroup>
20
21 <ItemGroup>
22 - <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta-63102-01" PrivateAssets="All" />
22 + <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta2-18618-05" PrivateAssets="All" />
23 <PackageReference Include="Nerdbank.GitVersioning" Version="2.1.65" PrivateAssets="All" />
24 </ItemGroup>
25