-
-
Notifications
You must be signed in to change notification settings - Fork 728
/
MSBuildResolver.cs
223 lines (197 loc) · 8.6 KB
/
MSBuildResolver.cs
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using Cake.Core;
using Cake.Core.IO;
namespace Cake.Common.Tools.MSBuild
{
internal static class MSBuildResolver
{
public static FilePath GetMSBuildPath(IFileSystem fileSystem, ICakeEnvironment environment, MSBuildToolVersion version, MSBuildPlatform buildPlatform)
{
if (environment.Platform.Family == PlatformFamily.OSX)
{
var macMSBuildPath = new FilePath("/Library/Frameworks/Mono.framework/Versions/Current/Commands/msbuild");
if (fileSystem.Exist(macMSBuildPath))
{
return macMSBuildPath;
}
var brewMSBuildPath = new FilePath("/usr/local/bin/msbuild");
if (fileSystem.Exist(brewMSBuildPath))
{
return brewMSBuildPath;
}
throw new CakeException("Could not resolve MSBuild.");
}
else if (environment.Platform.Family == PlatformFamily.Linux)
{
var linuxMSBuildPath = new FilePath("/usr/bin/msbuild");
if (fileSystem.Exist(linuxMSBuildPath))
{
return linuxMSBuildPath;
}
throw new CakeException("Could not resolve MSBuild.");
}
var binPath = version == MSBuildToolVersion.Default
? GetHighestAvailableMSBuildVersion(fileSystem, environment, buildPlatform)
: GetMSBuildPath(fileSystem, environment, (MSBuildVersion)version, buildPlatform);
if (binPath == null)
{
throw new CakeException("Could not resolve MSBuild.");
}
// Get the MSBuild path.
return binPath.CombineWithFilePath("MSBuild.exe");
}
private static DirectoryPath GetHighestAvailableMSBuildVersion(IFileSystem fileSystem, ICakeEnvironment environment, MSBuildPlatform buildPlatform)
{
var versions = new[]
{
MSBuildVersion.MSBuild16,
MSBuildVersion.MSBuild15,
MSBuildVersion.MSBuild14,
MSBuildVersion.MSBuild12,
MSBuildVersion.MSBuild4,
MSBuildVersion.MSBuild35,
MSBuildVersion.MSBuild20
};
foreach (var version in versions)
{
var path = GetMSBuildPath(fileSystem, environment, version, buildPlatform);
if (fileSystem.Exist(path))
{
return path;
}
}
return null;
}
private static DirectoryPath GetMSBuildPath(IFileSystem fileSystem, ICakeEnvironment environment, MSBuildVersion version, MSBuildPlatform buildPlatform)
{
switch (version)
{
case MSBuildVersion.MSBuild16:
return GetVisualStudio2019Path(fileSystem, environment, buildPlatform);
case MSBuildVersion.MSBuild15:
return GetVisualStudio2017Path(fileSystem, environment, buildPlatform);
case MSBuildVersion.MSBuild14:
return GetVisualStudioPath(environment, buildPlatform, "14.0");
case MSBuildVersion.MSBuild12:
return GetVisualStudioPath(environment, buildPlatform, "12.0");
case MSBuildVersion.MSBuild4:
return GetFrameworkPath(environment, buildPlatform, "v4.0.30319");
case MSBuildVersion.MSBuild35:
return GetFrameworkPath(environment, buildPlatform, "v3.5");
case MSBuildVersion.MSBuild20:
return GetFrameworkPath(environment, buildPlatform, "v2.0.50727");
default:
return null;
}
}
private static DirectoryPath GetVisualStudioPath(ICakeEnvironment environment, MSBuildPlatform buildPlatform, string version)
{
// Get the bin path.
var programFilesPath = environment.GetSpecialPath(SpecialPath.ProgramFilesX86);
var binPath = programFilesPath.Combine(string.Concat("MSBuild/", version, "/Bin"));
if (buildPlatform == MSBuildPlatform.Automatic)
{
if (environment.Platform.Is64Bit)
{
binPath = binPath.Combine("amd64");
}
}
if (buildPlatform == MSBuildPlatform.x64)
{
binPath = binPath.Combine("amd64");
}
return binPath;
}
private static DirectoryPath GetVisualStudio2017Path(IFileSystem fileSystem, ICakeEnvironment environment,
MSBuildPlatform buildPlatform)
{
var vsEditions = new[]
{
"Enterprise",
"Professional",
"Community",
"BuildTools"
};
var visualStudio2017Path = environment.GetSpecialPath(SpecialPath.ProgramFilesX86);
foreach (var edition in vsEditions)
{
// Get the bin path.
var binPath = visualStudio2017Path.Combine(string.Concat("Microsoft Visual Studio/2017/", edition, "/MSBuild/15.0/Bin"));
if (fileSystem.Exist(binPath))
{
if (buildPlatform == MSBuildPlatform.Automatic)
{
if (environment.Platform.Is64Bit)
{
binPath = binPath.Combine("amd64");
}
}
if (buildPlatform == MSBuildPlatform.x64)
{
binPath = binPath.Combine("amd64");
}
return binPath;
}
}
return visualStudio2017Path.Combine("Microsoft Visual Studio/2017/Professional/MSBuild/15.0/Bin");
}
private static DirectoryPath GetVisualStudio2019Path(IFileSystem fileSystem, ICakeEnvironment environment,
MSBuildPlatform buildPlatform)
{
var vsEditions = new[]
{
"Enterprise",
"Professional",
"Community",
"BuildTools"
};
var visualStudio2019Path = environment.GetSpecialPath(SpecialPath.ProgramFilesX86);
foreach (var edition in vsEditions)
{
// Get the bin path.
var binPath = visualStudio2019Path.Combine(string.Concat("Microsoft Visual Studio/2019/", edition, "/MSBuild/Current/Bin")); // Change from Current to 16.0 after stable version released
if (fileSystem.Exist(binPath))
{
if (buildPlatform == MSBuildPlatform.Automatic)
{
if (environment.Platform.Is64Bit)
{
binPath = binPath.Combine("amd64");
}
}
if (buildPlatform == MSBuildPlatform.x64)
{
binPath = binPath.Combine("amd64");
}
return binPath;
}
}
return visualStudio2019Path.Combine("Microsoft Visual Studio/2019/Professional/MSBuild/16.0/Bin");
}
private static DirectoryPath GetFrameworkPath(ICakeEnvironment environment, MSBuildPlatform buildPlatform, string version)
{
// Get the Microsoft .NET folder.
var windowsFolder = environment.GetSpecialPath(SpecialPath.Windows);
var netFolder = windowsFolder.Combine("Microsoft.NET");
if (buildPlatform == MSBuildPlatform.Automatic)
{
// Get the framework folder.
var is64Bit = environment.Platform.Is64Bit;
var frameWorkFolder = is64Bit ? netFolder.Combine("Framework64") : netFolder.Combine("Framework");
return frameWorkFolder.Combine(version);
}
if (buildPlatform == MSBuildPlatform.x86)
{
return netFolder.Combine("Framework").Combine(version);
}
if (buildPlatform == MSBuildPlatform.x64)
{
return netFolder.Combine("Framework64").Combine(version);
}
throw new NotSupportedException();
}
}
}