-
Notifications
You must be signed in to change notification settings - Fork 353
/
GetCompatiblePackageTargetFrameworks.cs
162 lines (143 loc) · 7.51 KB
/
GetCompatiblePackageTargetFrameworks.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.DotNet.Build.Tasks;
using NuGet.Frameworks;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.DotNet.PackageTesting
{
public class GetCompatiblePackageTargetFrameworks : BuildTask
{
private static List<NuGetFramework> allTargetFrameworks = allTargetFrameworks = new();
private static Dictionary<NuGetFramework, HashSet<NuGetFramework>> packageTfmMapping = new();
[Required]
public string[] PackagePaths { get; set; }
[Output]
public ITaskItem[] TestProjects { get; set; }
public override bool Execute()
{
bool result = true;
List<ITaskItem> testProjects = new List<ITaskItem>();
try
{
Initialize();
foreach (var packagePath in PackagePaths)
{
Package package = NupkgParser.CreatePackageObject(packagePath);
List<NuGetFramework> packageTargetFrameworks = package.PackageAssets.Where(t => t.AssetType != AssetType.RuntimeAsset).Select(t => t.TargetFramework).Distinct().ToList();
List<NuGetFramework> frameworksToTest = GetTestFrameworks(packageTargetFrameworks);
testProjects.AddRange(CreateItemFromTestFramework(package.PackageId, package.Version, frameworksToTest, GetRidsFromPackage(package)));
}
// Removing empty items.
TestProjects = testProjects.Where(tfm => tfm.ItemSpec != "").ToArray();
}
catch (Exception e)
{
Log.LogErrorFromException(e, showStackTrace: false);
}
return result && !Log.HasLoggedErrors;
}
public static List<NuGetFramework> GetTestFrameworks(List<NuGetFramework> packageTargetFrameworks)
{
List<NuGetFramework> frameworksToTest = new List<NuGetFramework>();
// Testing the package installation on all tfms linked with package targetframeworks.
foreach (var item in packageTargetFrameworks)
{
if (packageTfmMapping.ContainsKey(item))
frameworksToTest.AddRange(packageTfmMapping[item].ToList());
}
// Pruning the test matrix by removing the frameworks we dont want to test.
frameworksToTest = frameworksToTest.Where(tfm => allTargetFrameworks.Contains(tfm)).ToList();
// Adding the frameworks in the packages to the test matrix;
frameworksToTest.AddRange(packageTargetFrameworks);
frameworksToTest = frameworksToTest.Distinct().ToList();
return frameworksToTest;
}
public static void Initialize()
{
// Defining the set of known frameworks that we care to test
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.NetCoreApp20);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.NetCoreApp21);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.NetCoreApp30);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.NetCoreApp31);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.Net50);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.Net45);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.Net451);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.Net452);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.Net46);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.Net461);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.Net462);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.Net463);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.NetStandard10);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.NetStandard11);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.NetStandard12);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.NetStandard13);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.NetStandard14);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.NetStandard15);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.NetStandard16);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.NetStandard17);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.NetStandard20);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.NetStandard21);
allTargetFrameworks.Add(FrameworkConstants.CommonFrameworks.UAP10);
// creating a map framework in package => frameworks to test based on default compatibilty mapping.
foreach (var item in DefaultFrameworkMappings.Instance.CompatibilityMappings)
{
NuGetFramework forwardTfm = item.SupportedFrameworkRange.Max;
NuGetFramework reverseTfm = item.TargetFrameworkRange.Min;
if (packageTfmMapping.ContainsKey(forwardTfm))
{
packageTfmMapping[forwardTfm].Add(reverseTfm);
}
else
{
packageTfmMapping.Add(forwardTfm, new HashSet<NuGetFramework> { reverseTfm });
}
}
}
public List<ITaskItem> CreateItemFromTestFramework(string packageId, string version, List<NuGetFramework> testFrameworks, string rids)
{
List<ITaskItem> testprojects = new List<ITaskItem>();
foreach (var framework in testFrameworks)
{
var supportedPackage = new TaskItem(packageId);
supportedPackage.SetMetadata("Version", version);
supportedPackage.SetMetadata("TargetFramework", framework.ToString());
supportedPackage.SetMetadata("TargetFrameworkShort", framework.GetShortFolderName());
if (!String.IsNullOrEmpty(rids))
{
supportedPackage.SetMetadata("RuntimeIdentifiers", rids);
}
testprojects.Add(supportedPackage);
}
return testprojects;
}
public string GetRidsFromPackage(Package package)
{
List<string> rids = new List<string>();
foreach (var item in package.PackageAssets)
{
if (item.AssetType == AssetType.RuntimeAsset)
{
string testRid = item.Rid;
string testArch = testRid == "browser" ? "-wasm" : "-x64";
if (testRid == "unix")
{
if (!rids.Contains("linux" + testArch))
rids.Add("linux" + testArch);
if (!rids.Contains("osx" + testArch))
rids.Add("osx" + testArch);
}
else
{
if (!rids.Contains(testRid + testArch))
rids.Add(testRid + testArch);
}
}
}
return string.Join(";", rids);
}
}
}