1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
// 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.
namespace WixBuildTools.TestSupport
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
public static class MsbuildRunner
{
private static readonly string VswhereRelativePath = @"Microsoft Visual Studio\Installer\vswhere.exe";
private static readonly string[] VswhereFindArguments = new[] { "-property", "installationPath" };
private static readonly string Msbuild15RelativePath = @"MSBuild\15.0\Bin\MSBuild.exe";
private static readonly string Msbuild16RelativePath = @"MSBuild\Current\Bin\MSBuild.exe";
private static string Msbuild15Path;
private static string Msbuild16Path;
public static MsbuildRunnerResult Execute(string projectPath, string[] arguments = null) => InitAndExecute(String.Empty, projectPath, arguments);
public static MsbuildRunnerResult ExecuteWithMsbuild15(string projectPath, string[] arguments = null) => InitAndExecute("15", projectPath, arguments);
public static MsbuildRunnerResult ExecuteWithMsbuild16(string projectPath, string[] arguments = null) => InitAndExecute("16", projectPath, arguments);
private static MsbuildRunnerResult InitAndExecute(string msbuildVersion, string projectPath, string[] arguments)
{
if (Msbuild15Path == null && Msbuild16Path == null)
{
var vswherePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), VswhereRelativePath);
if (!File.Exists(vswherePath))
{
throw new InvalidOperationException($"Failed to find vswhere at: {vswherePath}");
}
var result = RunProcessCaptureOutput(vswherePath, VswhereFindArguments);
if (result.ExitCode != 0)
{
throw new InvalidOperationException($"Failed to execute vswhere.exe, exit code: {result.ExitCode}");
}
Msbuild15Path = String.Empty;
Msbuild16Path = String.Empty;
foreach (var installPath in result.Output)
{
if (String.IsNullOrEmpty(Msbuild16Path))
{
var path = Path.Combine(installPath, Msbuild16RelativePath);
if (File.Exists(path))
{
Msbuild16Path = path;
}
}
if (String.IsNullOrEmpty(Msbuild15Path))
{
var path = Path.Combine(installPath, Msbuild15RelativePath);
if (File.Exists(path))
{
Msbuild15Path = path;
}
}
}
}
var msbuildPath = !String.IsNullOrEmpty(Msbuild15Path) ? Msbuild15Path : Msbuild16Path;
if (msbuildVersion == "15")
{
msbuildPath = Msbuild15Path;
}
else if (msbuildVersion == "16")
{
msbuildPath = Msbuild16Path;
}
return ExecuteCore(msbuildVersion, msbuildPath, projectPath, arguments);
}
private static MsbuildRunnerResult ExecuteCore(string msbuildVersion, string msbuildPath, string projectPath, string[] arguments)
{
if (String.IsNullOrEmpty(msbuildPath))
{
throw new InvalidOperationException($"Failed to find an installed MSBuild{msbuildVersion}");
}
var total = new List<string>
{
projectPath
};
if (arguments != null)
{
total.AddRange(arguments);
}
var workingFolder = Path.GetDirectoryName(projectPath);
return RunProcessCaptureOutput(msbuildPath, total.ToArray(), workingFolder);
}
private static MsbuildRunnerResult RunProcessCaptureOutput(string executablePath, string[] arguments = null, string workingFolder = null)
{
var startInfo = new ProcessStartInfo(executablePath)
{
Arguments = CombineArguments(arguments),
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
WorkingDirectory = workingFolder,
};
var exitCode = 0;
var output = new List<string>();
using (var process = Process.Start(startInfo))
{
process.OutputDataReceived += (s, e) => { if (e.Data != null) { output.Add(e.Data); } };
process.ErrorDataReceived += (s, e) => { if (e.Data != null) { output.Add(e.Data); } };
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
exitCode = process.ExitCode;
}
return new MsbuildRunnerResult { ExitCode = exitCode, Output = output.ToArray() };
}
private static string CombineArguments(string[] arguments)
{
if (arguments == null)
{
return null;
}
var sb = new StringBuilder();
foreach (var arg in arguments)
{
if (sb.Length > 0)
{
sb.Append(' ');
}
if (arg.IndexOf(' ') > -1)
{
sb.Append("\"");
sb.Append(arg);
sb.Append("\"");
}
else
{
sb.Append(arg);
}
}
return sb.ToString();
}
}
}
|