forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResolveTargetingPackAssets.cs
344 lines (289 loc) · 16.4 KB
/
ResolveTargetingPackAssets.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace Microsoft.NET.Build.Tasks
{
public class ResolveTargetingPackAssets : TaskBase
{
public ITaskItem[] FrameworkReferences { get; set; } = Array.Empty<ITaskItem>();
public ITaskItem[] ResolvedTargetingPacks { get; set; } = Array.Empty<ITaskItem>();
public ITaskItem[] RuntimeFrameworks { get; set; } = Array.Empty<ITaskItem>();
public bool GenerateErrorForMissingTargetingPacks { get; set; }
[Output]
public ITaskItem[] ReferencesToAdd { get; set; }
[Output]
public ITaskItem[] PlatformManifests { get; set; }
[Output]
public string PackageConflictPreferredPackages { get; set; }
[Output]
public ITaskItem[] PackageConflictOverrides { get; set; }
[Output]
public ITaskItem[] UsedRuntimeFrameworks { get; set; }
private Dictionary<string, List<string>> _assemblyProfiles;
public ResolveTargetingPackAssets()
{
// Hard-code assembly profiles for WindowDesktop targeting pack here until
// they are added to its FrameworkList.xml: https://github.com/dotnet/core-setup/issues/6210
_assemblyProfiles = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
var bothProfiles = new List<string>() { "WindowsForms", "WPF" };
foreach (var assemblyName in new []
{
"Accessibility",
"Microsoft.Win32.Registry",
"Microsoft.Win32.SystemEvents",
"System.CodeDom",
"System.Configuration.ConfigurationManager",
"System.Diagnostics.EventLog",
"System.DirectoryServices",
"System.IO.FileSystem.AccessControl",
"System.Media.SoundPlayer",
"System.Security.AccessControl",
"System.Security.Cryptography.Cng",
"System.Security.Cryptography.Pkcs",
"System.Security.Cryptography.ProtectedData",
"System.Security.Cryptography.Xml",
"System.Security.Permissions",
"System.Security.Principal.Windows",
"System.Threading.AccessControl",
"System.Windows.Extensions",
})
{
_assemblyProfiles[assemblyName] = bothProfiles;
}
var wpfProfile = new List<string>() { "WPF" };
foreach (var assemblyName in new []
{
"DirectWriteForwarder",
"PenImc_cor3",
"PresentationCore-CommonResources",
"PresentationCore",
"PresentationFramework-SystemCore",
"PresentationFramework-SystemData",
"PresentationFramework-SystemDrawing",
"PresentationFramework-SystemXml",
"PresentationFramework-SystemXmlLinq",
"PresentationFramework.Aero",
"PresentationFramework.Aero2",
"PresentationFramework.AeroLite",
"PresentationFramework.Classic",
"PresentationFramework",
"PresentationFramework.Luna",
"PresentationFramework.Royale",
"PresentationNative_cor3",
"PresentationUI",
"ReachFramework",
"System.Printing",
"System.Windows.Controls.Ribbon",
"System.Windows.Input.Manipulations",
"System.Windows.Presentation",
"System.Xaml",
"UIAutomationClient",
"UIAutomationClientSideProviders",
"UIAutomationProvider",
"UIAutomationTypes",
"WindowsBase",
"WPFgfx_cor3",
})
{
_assemblyProfiles[assemblyName] = wpfProfile;
}
var windowsFormsProfile = new List<string>() { "WindowsForms" };
foreach (var assemblyName in new[]
{
"System.Design",
"System.Drawing.Common",
"System.Drawing.Design",
"System.Drawing.Design.Primitives",
"System.Windows.Forms.Design",
"System.Windows.Forms.Design.Editors",
"System.Windows.Forms",
})
{
_assemblyProfiles[assemblyName] = windowsFormsProfile;
}
}
protected override void ExecuteCore()
{
List<TaskItem> referencesToAdd = new List<TaskItem>();
List<TaskItem> platformManifests = new List<TaskItem>();
PackageConflictPreferredPackages = string.Empty;
List<TaskItem> packageConflictOverrides = new List<TaskItem>();
var resolvedTargetingPacks = ResolvedTargetingPacks.ToDictionary(item => item.ItemSpec, StringComparer.OrdinalIgnoreCase);
foreach (var frameworkReference in FrameworkReferences)
{
ITaskItem targetingPack;
resolvedTargetingPacks.TryGetValue(frameworkReference.ItemSpec, out targetingPack);
string targetingPackRoot = targetingPack?.GetMetadata("Path");
if (string.IsNullOrEmpty(targetingPackRoot) || !Directory.Exists(targetingPackRoot))
{
if (GenerateErrorForMissingTargetingPacks)
{
Log.LogError(Strings.UnknownFrameworkReference, frameworkReference.ItemSpec);
}
}
else
{
string targetingPackFormat = targetingPack.GetMetadata("TargetingPackFormat");
if (targetingPackFormat.Equals("NETStandardLegacy", StringComparison.OrdinalIgnoreCase))
{
AddNetStandardTargetingPackAssets(targetingPack, targetingPackRoot, referencesToAdd);
}
else
{
string targetingPackTargetFramework = targetingPack.GetMetadata("TargetFramework");
if (string.IsNullOrEmpty(targetingPackTargetFramework))
{
targetingPackTargetFramework = "netcoreapp3.0";
}
string targetingPackDataPath = Path.Combine(targetingPackRoot, "data");
string[] possibleDllFolders = new[]
{
Path.Combine(targetingPackRoot, "ref", targetingPackTargetFramework),
targetingPackDataPath
};
string[] possibleManifestPaths = new[]
{
Path.Combine(targetingPackRoot, "build", targetingPackTargetFramework,
targetingPack.GetMetadata(MetadataKeys.PackageName) + ".PlatformManifest.txt"),
Path.Combine(targetingPackDataPath, "PlatformManifest.txt"),
Path.Combine(targetingPackDataPath,
targetingPack.GetMetadata(MetadataKeys.PackageName) + ".PlatformManifest.txt"),
};
string targetingPackDllFolder = possibleDllFolders.First(path =>
Directory.Exists(path) &&
Directory.GetFiles(path, "*.dll").Any());
string platformManifestPath = possibleManifestPaths.FirstOrDefault(File.Exists);
string packageOverridesPath = Path.Combine(targetingPackDataPath, "PackageOverrides.txt");
string frameworkListPath = Path.Combine(targetingPackDataPath, "FrameworkList.xml");
if (File.Exists(frameworkListPath))
{
AddReferencesFromFrameworkList(frameworkListPath, targetingPackDllFolder,
targetingPack, referencesToAdd);
}
else
{
foreach (var dll in Directory.GetFiles(targetingPackDllFolder, "*.dll"))
{
var reference = CreateReferenceItem(dll, targetingPack);
referencesToAdd.Add(reference);
}
}
if (platformManifestPath != null)
{
platformManifests.Add(new TaskItem(platformManifestPath));
}
if (File.Exists(packageOverridesPath))
{
packageConflictOverrides.Add(CreatePackageOverride(targetingPack.GetMetadata("RuntimeFrameworkName"), packageOverridesPath));
}
if (targetingPack.ItemSpec.Equals("Microsoft.NETCore.App", StringComparison.OrdinalIgnoreCase))
{
// Hardcode this for now. Load this from the targeting pack once we have "real" targeting packs
// https://github.com/dotnet/cli/issues/10581
PackageConflictPreferredPackages = "Microsoft.NETCore.App;runtime.linux-x64.Microsoft.NETCore.App;runtime.linux-x64.Microsoft.NETCore.App;runtime.linux-musl-x64.Microsoft.NETCore.App;runtime.linux-musl-x64.Microsoft.NETCore.App;runtime.rhel.6-x64.Microsoft.NETCore.App;runtime.rhel.6-x64.Microsoft.NETCore.App;runtime.osx-x64.Microsoft.NETCore.App;runtime.osx-x64.Microsoft.NETCore.App;runtime.freebsd-x64.Microsoft.NETCore.App;runtime.freebsd-x64.Microsoft.NETCore.App;runtime.win-x86.Microsoft.NETCore.App;runtime.win-x86.Microsoft.NETCore.App;runtime.win-arm.Microsoft.NETCore.App;runtime.win-arm.Microsoft.NETCore.App;runtime.win-arm64.Microsoft.NETCore.App;runtime.win-arm64.Microsoft.NETCore.App;runtime.linux-arm.Microsoft.NETCore.App;runtime.linux-arm.Microsoft.NETCore.App;runtime.linux-arm64.Microsoft.NETCore.App;runtime.linux-arm64.Microsoft.NETCore.App;runtime.tizen.4.0.0-armel.Microsoft.NETCore.App;runtime.tizen.4.0.0-armel.Microsoft.NETCore.App;runtime.tizen.5.0.0-armel.Microsoft.NETCore.App;runtime.tizen.5.0.0-armel.Microsoft.NETCore.App;runtime.win-x64.Microsoft.NETCore.App;runtime.win-x64.Microsoft.NETCore.App";
}
}
}
}
// Calculate which RuntimeFramework items should actually be used based on framework references
HashSet<string> frameworkReferenceNames = new HashSet<string>(FrameworkReferences.Select(fr => fr.ItemSpec), StringComparer.OrdinalIgnoreCase);
UsedRuntimeFrameworks = RuntimeFrameworks.Where(rf => frameworkReferenceNames.Contains(rf.GetMetadata(MetadataKeys.FrameworkName)))
.ToArray();
// Filter out duplicate references (which can happen when referencing two different profiles that overlap)
List<TaskItem> deduplicatedReferences = DeduplicateItems(referencesToAdd);
ReferencesToAdd = deduplicatedReferences.Distinct() .ToArray();
PlatformManifests = platformManifests.ToArray();
PackageConflictOverrides = packageConflictOverrides.ToArray();
}
// Get distinct items based on case-insensitive ItemSpec comparison
private static List<TaskItem> DeduplicateItems(List<TaskItem> items)
{
HashSet<string> seenItemSpecs = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
List<TaskItem> deduplicatedItems = new List<TaskItem>(items.Count);
foreach (var item in items)
{
if (seenItemSpecs.Add(item.ItemSpec))
{
deduplicatedItems.Add(item);
}
}
return deduplicatedItems;
}
private TaskItem CreatePackageOverride(string runtimeFrameworkName, string packageOverridesPath)
{
TaskItem packageOverride = new TaskItem(runtimeFrameworkName);
packageOverride.SetMetadata("OverriddenPackages", File.ReadAllText(packageOverridesPath));
return packageOverride;
}
private void AddNetStandardTargetingPackAssets(ITaskItem targetingPack, string targetingPackRoot, List<TaskItem> referencesToAdd)
{
string targetingPackTargetFramework = targetingPack.GetMetadata("TargetFramework");
string targetingPackAssetPath = Path.Combine(targetingPackRoot, "build", targetingPackTargetFramework, "ref");
foreach (var dll in Directory.GetFiles(targetingPackAssetPath, "*.dll"))
{
var reference = CreateReferenceItem(dll, targetingPack);
if (!Path.GetFileName(dll).Equals("netstandard.dll", StringComparison.OrdinalIgnoreCase))
{
reference.SetMetadata("Facade", "true");
}
referencesToAdd.Add(reference);
}
}
private void AddReferencesFromFrameworkList(string frameworkListPath, string targetingPackDllFolder,
ITaskItem targetingPack, List<TaskItem> referenceItems)
{
XDocument frameworkListDoc = XDocument.Load(frameworkListPath);
string profile = targetingPack.GetMetadata("Profile");
foreach (var fileElement in frameworkListDoc.Root.Elements("File"))
{
string assemblyName = fileElement.Attribute("AssemblyName").Value;
if (!string.IsNullOrEmpty(profile))
{
_assemblyProfiles.TryGetValue(assemblyName, out var assemblyProfiles);
if (assemblyProfiles == null)
{
// If profile was specified but this assembly doesn't belong to any profiles, don't reference it
continue;
}
if (!assemblyProfiles.Contains(profile, StringComparer.OrdinalIgnoreCase))
{
// Assembly wasn't in profile specified, so don't reference it
continue;
}
}
var dllPath = Path.Combine(targetingPackDllFolder, assemblyName + ".dll");
var referenceItem = CreateReferenceItem(dllPath, targetingPack);
referenceItem.SetMetadata("AssemblyVersion", fileElement.Attribute("AssemblyVersion").Value);
referenceItem.SetMetadata("FileVersion", fileElement.Attribute("FileVersion").Value);
referenceItem.SetMetadata("PublicKeyToken", fileElement.Attribute("PublicKeyToken").Value);
referenceItems.Add(referenceItem);
}
}
private TaskItem CreateReferenceItem(string dll, ITaskItem targetingPack)
{
var reference = new TaskItem(dll);
reference.SetMetadata(MetadataKeys.ExternallyResolved, "true");
reference.SetMetadata(MetadataKeys.Private, "false");
reference.SetMetadata("Visible", "false");
reference.SetMetadata(MetadataKeys.NuGetPackageId, targetingPack.GetMetadata(MetadataKeys.PackageName));
reference.SetMetadata(MetadataKeys.NuGetPackageVersion, targetingPack.GetMetadata(MetadataKeys.PackageVersion));
// TODO: Once we work out what metadata we should use here to display these references grouped under the targeting pack
// in solution explorer, set that metadata here.These metadata values are based on what PCLs were using.
// https://github.com/dotnet/sdk/issues/2802
reference.SetMetadata("WinMDFile", "false");
reference.SetMetadata("ReferenceGroupingDisplayName", targetingPack.ItemSpec);
reference.SetMetadata("ReferenceGrouping", targetingPack.ItemSpec);
reference.SetMetadata("ResolvedFrom", "TargetingPack");
reference.SetMetadata("IsSystemReference", "true");
reference.SetMetadata("FrameworkName", targetingPack.ItemSpec);
reference.SetMetadata("FrameworkVersion", targetingPack.GetMetadata(MetadataKeys.PackageVersion));
return reference;
}
}
}