-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathCompatibilityProvider.cs
More file actions
276 lines (237 loc) · 10.6 KB
/
CompatibilityProvider.cs
File metadata and controls
276 lines (237 loc) · 10.6 KB
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
// 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.Concurrent;
using System.Collections.Generic;
using System.Linq;
namespace NuGet.Frameworks
{
public class CompatibilityProvider : IFrameworkCompatibilityProvider
{
private readonly IFrameworkNameProvider _mappings;
private readonly FrameworkExpander _expander;
private static readonly NuGetFrameworkFullComparer FullComparer = NuGetFrameworkFullComparer.Instance;
private readonly ConcurrentDictionary<CompatibilityCacheKey, bool> _cache;
public CompatibilityProvider(IFrameworkNameProvider mappings)
{
_mappings = mappings ?? throw new ArgumentNullException(nameof(mappings));
_expander = new FrameworkExpander(mappings);
_cache = new ConcurrentDictionary<CompatibilityCacheKey, bool>();
}
/// <summary>
/// Check if the frameworks are compatible.
/// </summary>
/// <param name="target">Project framework</param>
/// <param name="candidate">Other framework to check against the project framework</param>
/// <returns>True if framework supports other</returns>
public bool IsCompatible(NuGetFramework target, NuGetFramework candidate)
{
if (target == null) throw new ArgumentNullException(nameof(target));
if (candidate == null) throw new ArgumentNullException(nameof(candidate));
// check the cache for a solution
var cacheKey = new CompatibilityCacheKey(target, candidate);
if (!_cache.TryGetValue(cacheKey, out bool result))
{
result = IsCompatibleCore(target, candidate) == true;
_cache.TryAdd(cacheKey, result);
}
return result;
}
/// <summary>
/// Actual compatibility check without caching
/// </summary>
private bool? IsCompatibleCore(NuGetFramework target, NuGetFramework candidate)
{
bool? result = null;
// check if they are the exact same
if (FullComparer.Equals(target, candidate))
{
return true;
}
// special cased frameworks
if (!target.IsSpecificFramework
|| !candidate.IsSpecificFramework)
{
result = IsSpecialFrameworkCompatible(target, candidate);
}
if (result == null)
{
if (target.IsPCL || candidate.IsPCL)
{
// PCL compat logic
result = IsPCLCompatible(target, candidate);
}
else
{
// regular framework compat check
result = IsCompatibleWithTarget(target, candidate);
}
}
return result;
}
private bool? IsSpecialFrameworkCompatible(NuGetFramework target, NuGetFramework candidate)
{
// TODO: Revist these
if (target.IsAny
|| candidate.IsAny)
{
return true;
}
if (target.IsUnsupported)
{
return false;
}
if (candidate.IsAgnostic)
{
return true;
}
if (candidate.IsUnsupported)
{
return false;
}
return null;
}
private bool IsPCLCompatible(NuGetFramework target, NuGetFramework candidate)
{
if (target.IsPCL && !candidate.IsPCL)
{
return IsCompatibleWithTarget(target, candidate);
}
IEnumerable<NuGetFramework>? targetFrameworks;
IEnumerable<NuGetFramework>? candidateFrameworks;
if (target.IsPCL)
{
// do not include optional frameworks here since we might be unable to tell what is optional on the other framework
if (!_mappings.TryGetPortableFrameworks(target.Profile, includeOptional: false, out targetFrameworks))
{
targetFrameworks = Array.Empty<NuGetFramework>();
}
}
else
{
targetFrameworks = new NuGetFramework[] { target };
}
if (candidate.IsPCL)
{
// include optional frameworks here, the larger the list the more compatible it is
if (!_mappings.TryGetPortableFrameworks(candidate.Profile, includeOptional: true, out candidateFrameworks))
{
candidateFrameworks = Array.Empty<NuGetFramework>();
}
}
else
{
candidateFrameworks = new NuGetFramework[] { candidate };
}
// check if we this is a compatible superset
return PCLInnerCompare(targetFrameworks, candidateFrameworks);
}
private bool PCLInnerCompare(IEnumerable<NuGetFramework> targetFrameworks, IEnumerable<NuGetFramework> candidateFrameworks)
{
// TODO: Does this check need to make sure multiple frameworks aren't matched against a single framework from the other list?
return targetFrameworks.Count() <= candidateFrameworks.Count() && targetFrameworks.All(f => candidateFrameworks.Any(ff => IsCompatible(f, ff)));
}
private bool IsCompatibleWithTarget(NuGetFramework target, NuGetFramework candidate)
{
// find all possible substitutions
var targetSet = new List<NuGetFramework>() { target };
targetSet.AddRange(_expander.Expand(target));
var candidateSet = new List<NuGetFramework>() { candidate };
candidateSet.AddRange(GetEquivalentFrameworksClosure(candidate));
// check for compat
foreach (var currentCandidate in candidateSet)
{
if (targetSet.Any(framework => IsCompatibleWithTargetCore(framework, currentCandidate)))
{
return true;
}
}
return false;
}
private static bool IsCompatibleWithTargetCore(NuGetFramework target, NuGetFramework candidate)
{
bool result = true;
bool isNet6Era = target.IsNet5Era && target.Version.Major >= 6;
if (isNet6Era && target.HasPlatform && !NuGetFramework.FrameworkNameComparer.Equals(target, candidate))
{
if (candidate.Framework.Equals(FrameworkConstants.FrameworkIdentifiers.MonoAndroid, StringComparison.OrdinalIgnoreCase))
{
result = result && StringComparer.OrdinalIgnoreCase.Equals(target.Platform, "android");
}
else if (candidate.Framework.Equals(FrameworkConstants.FrameworkIdentifiers.Tizen, StringComparison.OrdinalIgnoreCase))
{
result = result && StringComparer.OrdinalIgnoreCase.Equals(target.Platform, "tizen");
}
else
{
result = false;
}
}
else
{
result = NuGetFramework.FrameworkNameComparer.Equals(target, candidate)
&& IsVersionCompatible(target.Version, candidate.Version)
&& StringComparer.OrdinalIgnoreCase.Equals(target.Profile, candidate.Profile);
if (target.IsNet5Era && candidate.HasPlatform)
{
// If targeting a TFM with .1 revision for the Platform version, then that means
// it is targeting CsWinRT 3.0 and we need to make sure the candidate is also doing
// the same. Similarly, if it is not targeting .1, then we need to make sure the candidate
// is also not targeting it for it to be compatible. The .1 revision was added in .NET 10.
if (result
// Scope to .NET 10 and later for the Windows 10 TFM
&& target.Version.Major >= 10
&& StringComparer.OrdinalIgnoreCase.Equals(target.Platform, "windows")
&& target.PlatformVersion.Major >= 10
&& candidate.PlatformVersion.Major >= 10
// Check if both are targeting the same version of CsWinRT support
&& (target.PlatformVersion.Revision == 1) != (candidate.PlatformVersion.Revision == 1))
{
result = false;
}
result = result
&& StringComparer.OrdinalIgnoreCase.Equals(target.Platform, candidate.Platform)
&& IsVersionCompatible(target.PlatformVersion, candidate.PlatformVersion);
}
}
return result;
}
private static bool IsVersionCompatible(Version target, Version candidate)
{
return candidate == FrameworkConstants.EmptyVersion || candidate <= target;
}
/// <summary>
/// Find all equivalent frameworks, and their equivalent frameworks.
/// Example:
/// Mappings:
/// A <‒> B
/// B <‒> C
/// C <‒> D
/// For A we need to find B, C, and D so we must retrieve equivalent frameworks for A, B, and C
/// also as we discover them.
/// </summary>
private IEnumerable<NuGetFramework> GetEquivalentFrameworksClosure(NuGetFramework framework)
{
// add the current framework to the seen list to avoid returning it later
var seen = new HashSet<NuGetFramework>() { framework };
var toExpand = new Stack<NuGetFramework>();
toExpand.Push(framework);
while (toExpand.Count > 0)
{
var frameworkToExpand = toExpand.Pop();
if (_mappings.TryGetEquivalentFrameworks(frameworkToExpand, out IEnumerable<NuGetFramework>? compatibleFrameworks))
{
foreach (var curFramework in compatibleFrameworks)
{
if (seen.Add(curFramework))
{
yield return curFramework;
toExpand.Push(curFramework);
}
}
}
}
yield break;
}
}
}