-
Notifications
You must be signed in to change notification settings - Fork 694
/
Copy pathMSBuildProjectFrameworkUtility.cs
314 lines (276 loc) · 12.9 KB
/
MSBuildProjectFrameworkUtility.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using NuGet.Common;
using NuGet.Frameworks;
namespace NuGet.Commands
{
public static class MSBuildProjectFrameworkUtility
{
/// <summary>
/// Determine the target framework of an msbuild project.
/// Returns the <see cref="FrameworkName"/> equivalent representation.
/// </summary>
/// <remarks>
/// Do not use this when expecting a project to target a .NET 5 era framework.
/// </remarks>
public static IEnumerable<string> GetProjectFrameworkStrings(
string projectFilePath,
string targetFrameworks,
string targetFramework,
string targetFrameworkMoniker,
string targetPlatformIdentifier,
string targetPlatformVersion,
string targetPlatformMinVersion)
{
return GetProjectFrameworkStrings(
projectFilePath,
targetFrameworks,
targetFramework,
targetFrameworkMoniker,
targetPlatformIdentifier,
targetPlatformVersion,
targetPlatformMinVersion,
isManagementPackProject: false,
isXnaWindowsPhoneProject: false);
}
/// <summary>
/// Given the properties from an msbuild project file and a the file path, infer the target framework.
/// This method prioritizes projects without a framework, such as vcxproj and accounts for the mismatching arguments in UAP projects, where the TFI and TFV are set but should be ignored.
/// Likewise, this method will *ignore* unnecessary properties, such as TPI and TPV when profiles are used, and frameworks that do not support platforms have some default values.
/// </summary>
/// <returns>The inferred framework. Unsupported otherwise.</returns>
public static NuGetFramework GetProjectFramework(
string projectFilePath,
string targetFrameworkMoniker,
string targetPlatformMoniker,
string targetPlatformMinVersion)
{
return GetProjectFramework(
projectFilePath,
targetFrameworkMoniker,
targetPlatformMoniker,
targetPlatformIdentifier: null,
targetPlatformVersion: null,
targetPlatformMinVersion,
isXnaWindowsPhoneProject: false,
isManagementPackProject: false);
}
/// <summary>
/// Determine the target framework of an msbuild project.
/// Returns the <see cref="FrameworkName"/> equivalent representation.
/// </summary>
/// <remarks>
/// Do not use this when expecting a project to target a .NET 5 era framework.
/// </remarks>
public static IEnumerable<string> GetProjectFrameworkStrings(
string projectFilePath,
string targetFrameworks,
string targetFramework,
string targetFrameworkMoniker,
string targetPlatformIdentifier,
string targetPlatformVersion,
string targetPlatformMinVersion,
bool isXnaWindowsPhoneProject,
bool isManagementPackProject)
{
return GetProjectFrameworks(
projectFilePath,
targetFrameworks,
targetFramework,
targetFrameworkMoniker,
targetPlatformMoniker: null,
targetPlatformIdentifier,
targetPlatformVersion,
targetPlatformMinVersion,
isXnaWindowsPhoneProject,
isManagementPackProject);
}
internal static IEnumerable<string> GetProjectFrameworks(
string projectFilePath,
string targetFrameworks,
string targetFramework,
string targetFrameworkMoniker,
string targetPlatformMoniker,
string targetPlatformIdentifier,
string targetPlatformVersion,
string targetPlatformMinVersion,
bool isXnaWindowsPhoneProject,
bool isManagementPackProject)
{
var frameworks = new SortedSet<string>(StringComparer.OrdinalIgnoreCase);
// TargetFrameworks property
frameworks.UnionWith(MSBuildStringUtility.Split(targetFrameworks));
if (frameworks.Count > 0)
{
return frameworks;
}
// TargetFramework property
var currentFrameworkString = MSBuildStringUtility.TrimAndGetNullForEmpty(targetFramework);
if (!string.IsNullOrEmpty(currentFrameworkString))
{
frameworks.Add(currentFrameworkString);
return frameworks;
}
return new string[] { GetProjectFramework(
projectFilePath,
targetFrameworkMoniker,
targetPlatformMoniker,
targetPlatformIdentifier,
targetPlatformVersion,
targetPlatformMinVersion,
isXnaWindowsPhoneProject,
isManagementPackProject).DotNetFrameworkName };
}
internal static NuGetFramework GetProjectFramework(
string projectFilePath,
string targetFrameworkMoniker,
string targetPlatformMoniker,
string targetPlatformIdentifier,
string targetPlatformVersion,
string targetPlatformMinVersion,
bool isXnaWindowsPhoneProject,
bool isManagementPackProject)
{
// C++ check
if (projectFilePath?.EndsWith(".vcxproj", StringComparison.OrdinalIgnoreCase) == true)
{
// The C++ project does not have a TargetFrameworkMoniker property set.
// We hard-code the return value to Native.
return NuGetFramework.Parse("Native, Version=0.0");
}
// The MP project does not have a TargetFrameworkMoniker property set.
// We hard-code the return value to SCMPInfra.
if (isManagementPackProject)
{
return NuGetFramework.Parse("SCMPInfra, Version=0.0");
}
// UAP/Windows store projects
var platformMoniker = MSBuildStringUtility.TrimAndGetNullForEmpty(targetPlatformMoniker);
var platformMonikerIdentifier = GetParts(platformMoniker).FirstOrDefault();
var platformIdentifier = MSBuildStringUtility.TrimAndGetNullForEmpty(targetPlatformIdentifier);
var platformMinVersion = MSBuildStringUtility.TrimAndGetNullForEmpty(targetPlatformMinVersion);
var platformVersion = MSBuildStringUtility.TrimAndGetNullForEmpty(targetPlatformVersion);
var effectivePlatformVersion = platformMinVersion ?? platformVersion;
var effectivePlatformIdentifier = platformMonikerIdentifier ?? platformIdentifier;
// Check for JS project
if (projectFilePath?.EndsWith(".jsproj", StringComparison.OrdinalIgnoreCase) == true)
{
// JavaScript apps do not have a TargetFrameworkMoniker property set.
// We read the TargetPlatformIdentifier and targetPlatformMinVersion instead
// use the default values for JS if they were not given
// Prefer moniker over individual properties
if (!string.IsNullOrEmpty(platformMoniker))
{
return GetFrameworkFromMoniker(platformMonikerIdentifier, platformMoniker, platformMinVersion);
}
if (string.IsNullOrEmpty(effectivePlatformVersion))
{
effectivePlatformVersion = "0.0";
}
if (string.IsNullOrEmpty(effectivePlatformIdentifier))
{
effectivePlatformIdentifier = FrameworkConstants.FrameworkIdentifiers.Windows;
}
return NuGetFramework.Parse($"{effectivePlatformIdentifier}, Version={effectivePlatformVersion}");
}
if (StringComparer.OrdinalIgnoreCase.Equals(effectivePlatformIdentifier, "UAP"))
{
// Use the platform id and versions, this is done for UAP projects
// Prefer moniker over individual properties
if (!string.IsNullOrEmpty(platformMoniker))
{
return GetFrameworkFromMoniker(effectivePlatformIdentifier, platformMoniker, platformMinVersion);
}
if (!string.IsNullOrEmpty(effectivePlatformVersion))
{
return NuGetFramework.Parse($"{effectivePlatformIdentifier}, Version={effectivePlatformVersion}");
}
}
// TargetFrameworkMoniker
var currentFrameworkString = MSBuildStringUtility.TrimAndGetNullForEmpty(targetFrameworkMoniker);
if (!string.IsNullOrEmpty(currentFrameworkString))
{
// XNA project lies about its true identity, reporting itself as a normal .NET 4.0 project.
// We detect it and changes its target framework to Silverlight4-WindowsPhone71
if (isXnaWindowsPhoneProject
&& ".NETFramework,Version=v4.0".Equals(currentFrameworkString, StringComparison.OrdinalIgnoreCase))
{
currentFrameworkString = "Silverlight,Version=v4.0,Profile=WindowsPhone71";
return NuGetFramework.Parse(currentFrameworkString);
}
NuGetFramework framework = NuGetFramework.ParseComponents(currentFrameworkString, platformMoniker);
return framework;
}
// Default to unsupported it no framework was found.
return NuGetFramework.UnsupportedFramework;
}
private static NuGetFramework GetFrameworkFromMoniker(string platformIdentifier, string platformMoniker, string platformMinVersion)
{
if (!string.IsNullOrEmpty(platformMinVersion))
{
return NuGetFramework.Parse($"{platformIdentifier}, Version={platformMinVersion}");
}
else
{
return NuGetFramework.Parse(platformMoniker);
}
}
private static string[] GetParts(string targetPlatformMoniker)
{
return targetPlatformMoniker != null ?
targetPlatformMoniker.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray() :
new string[] { };
}
/// <summary>
/// Parse project framework strings into NuGetFrameworks.
/// </summary>
public static IEnumerable<NuGetFramework> GetProjectFrameworks(IEnumerable<string> frameworkStrings)
{
if (frameworkStrings == null)
{
throw new ArgumentNullException(nameof(frameworkStrings));
}
var frameworks = new List<NuGetFramework>();
foreach (var frameworkString in frameworkStrings)
{
var parsed = NuGetFramework.Parse(frameworkString);
// Replace if needed
parsed = GetProjectFrameworkReplacement(parsed);
// Add only unique frameworks
if (!frameworks.Contains(parsed))
{
frameworks.Add(parsed);
}
}
return frameworks;
}
/// <summary>
/// Parse existing nuget framework for .net core 4.5.1 or 4.5 and return compatible framework instance
/// </summary>
public static NuGetFramework GetProjectFrameworkReplacement(NuGetFramework framework)
{
if (framework == null)
{
throw new ArgumentNullException(nameof(framework));
}
// if the framework is .net core 4.5.1 return windows 8.1
if (framework.Framework.Equals(FrameworkConstants.FrameworkIdentifiers.NetCore)
&& framework.Version.Equals(Version.Parse("4.5.1.0")))
{
return new NuGetFramework(FrameworkConstants.FrameworkIdentifiers.Windows,
new Version("8.1"), framework.Profile);
}
// if the framework is .net core 4.5 return 8.0
if (framework.Framework.Equals(FrameworkConstants.FrameworkIdentifiers.NetCore)
&& framework.Version.Equals(Version.Parse("4.5.0.0")))
{
return new NuGetFramework(FrameworkConstants.FrameworkIdentifiers.Windows,
new Version("8.0"), framework.Profile);
}
return framework;
}
}
}