-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathResolveTargetingPackAssets.cs
More file actions
734 lines (613 loc) · 32.8 KB
/
Copy pathResolveTargetingPackAssets.cs
File metadata and controls
734 lines (613 loc) · 32.8 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable disable
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using static Microsoft.DotNet.Cli.EnvironmentVariableNames;
namespace Microsoft.NET.Build.Tasks
{
[MSBuildMultiThreadableTask]
public class ResolveTargetingPackAssets : TaskBase, IMultiThreadableTask
{
public TaskEnvironment TaskEnvironment { get; set; } = TaskEnvironment.Fallback;
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; }
public bool NuGetRestoreSupported { get; set; } = true;
public bool DisableTransitiveFrameworkReferenceDownloads { get; set; }
public string NetCoreTargetingPackRoot { get; set; }
public string ProjectLanguage { get; set; }
[Output]
public ITaskItem[] ReferencesToAdd { get; set; }
[Output]
public ITaskItem[] AnalyzersToAdd { 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; }
public ResolveTargetingPackAssets()
{
}
protected override void ExecuteCore()
{
StronglyTypedInputs inputs = GetInputs();
string cacheKey = inputs.CacheKey();
bool allowCacheLookup = TaskEnvironment.GetEnvironmentVariable(ALLOW_TARGETING_PACK_CACHING) != "0";
ResolvedAssetsCacheEntry results;
if (allowCacheLookup && BuildEngine4 is IBuildEngine4 buildEngine4)
{
if (buildEngine4.GetRegisteredTaskObject(
cacheKey,
RegisteredTaskObjectLifetime.AppDomain /* really "until process exit" */)
is ResolvedAssetsCacheEntry cacheEntry)
{
// NOTE: It's conceivably possible that the user modified the targeting
// packs between builds. Since that is extremely rare and the standard
// user scenario reads the targeting pack contents over and over without
// modification, we're electing not to check for file modification and
// returning any cached results that we have.
results = cacheEntry;
}
else
{
results = Resolve(inputs, buildEngine4, allowCacheLookup);
buildEngine4.RegisterTaskObject(cacheKey, results, RegisteredTaskObjectLifetime.AppDomain, allowEarlyCollection: true);
}
}
else
{
results = Resolve(inputs, BuildEngine4, allowCacheLookup);
}
foreach (var error in results.Errors)
{
Log.LogError(error);
}
ReferencesToAdd = results.ReferencesToAdd;
AnalyzersToAdd = results.AnalyzersToAdd;
PlatformManifests = results.PlatformManifests;
PackageConflictOverrides = results.PackageConflictOverrides;
PackageConflictPreferredPackages = results.PackageConflictPreferredPackages;
UsedRuntimeFrameworks = results.UsedRuntimeFrameworks;
}
internal StronglyTypedInputs GetInputs() => new(
FrameworkReferences,
GetResolvedTargetingPacks(),
RuntimeFrameworks,
GenerateErrorForMissingTargetingPacks,
NuGetRestoreSupported,
DisableTransitiveFrameworkReferenceDownloads,
NetCoreTargetingPackRoot,
ProjectLanguage);
private TargetingPack[] GetResolvedTargetingPacks() => ResolvedTargetingPacks.Select(
item =>
{
string targetingPackPath = item.GetMetadata(MetadataKeys.Path);
AbsolutePath path = string.IsNullOrEmpty(targetingPackPath)
? default
: TaskEnvironment.GetAbsolutePath(targetingPackPath);
return new TargetingPack(
item.ItemSpec,
path,
item.GetMetadata("TargetingPackFormat"),
item.GetMetadata(MetadataKeys.TargetFramework),
item.GetMetadata(MetadataKeys.Profile),
item.GetMetadata(MetadataKeys.NuGetPackageId),
item.GetMetadata(MetadataKeys.NuGetPackageVersion),
item.GetMetadata(MetadataKeys.PackageConflictPreferredPackages));
}).ToArray();
private ResolvedAssetsCacheEntry Resolve(StronglyTypedInputs inputs, IBuildEngine4 buildEngine, bool allowCacheLookup)
{
List<TaskItem> referencesToAdd = new();
List<TaskItem> analyzersToAdd = new();
List<TaskItem> platformManifests = new();
List<TaskItem> packageConflictOverrides = new();
List<string> preferredPackages = new();
List<string> errors = new();
var resolvedTargetingPacks = inputs.ResolvedTargetingPacks
.ToDictionary(
tp => tp.Name,
StringComparer.OrdinalIgnoreCase);
FrameworkReference[] frameworkReferences = inputs.FrameworkReferences;
foreach (var frameworkReference in frameworkReferences)
{
bool foundTargetingPack = resolvedTargetingPacks.TryGetValue(frameworkReference.Name, out TargetingPack targetingPack);
AbsolutePath targetingPackRoot = targetingPack?.Path ?? default;
if (string.IsNullOrEmpty(targetingPackRoot.Value) || !Directory.Exists(targetingPackRoot))
{
if (inputs.GenerateErrorForMissingTargetingPacks)
{
if (!foundTargetingPack)
{
if (frameworkReference.Name.Equals("Microsoft.Maui.Essentials", StringComparison.OrdinalIgnoreCase))
{
errors.Add(Strings.UnknownFrameworkReference_MauiEssentials);
}
else
{
errors.Add(string.Format(Strings.UnknownFrameworkReference, frameworkReference.Name));
}
}
else
{
if (inputs.DisableTransitiveFrameworkReferences)
{
errors.Add(string.Format(Strings.TargetingPackNotRestored_TransitiveDisabled, frameworkReference.Name));
}
else if (inputs.NuGetRestoreSupported)
{
errors.Add(string.Format(Strings.TargetingPackNeedsRestore, frameworkReference.Name));
}
else
{
errors.Add(string.Format(
Strings.TargetingApphostPackMissingCannotRestore,
"Targeting",
$"{inputs.NetCoreTargetingPackRoot}\\{targetingPack.NuGetPackageId ?? ""}",
targetingPack.TargetFramework ?? "",
targetingPack.NuGetPackageId ?? "",
targetingPack.NuGetPackageVersion ?? ""
));
}
}
}
}
else
{
string targetingPackFormat = targetingPack.Format;
if (targetingPackFormat.Equals("NETStandardLegacy", StringComparison.OrdinalIgnoreCase))
{
AddNetStandardTargetingPackAssets(targetingPack, targetingPackRoot, referencesToAdd);
}
else
{
string targetingPackTargetFramework = targetingPack.TargetFramework;
if (string.IsNullOrEmpty(targetingPackTargetFramework))
{
targetingPackTargetFramework = "netcoreapp3.0";
}
AbsolutePath targetingPackDllFolder = TaskEnvironment.GetAbsolutePath(
Path.Combine(targetingPackRoot.OriginalValue, "ref", targetingPackTargetFramework));
// Fall back to netcoreapp5.0 folder if looking for net5.0 and it's not found
if (!Directory.Exists(targetingPackDllFolder) &&
targetingPackTargetFramework.Equals("net5.0", StringComparison.OrdinalIgnoreCase))
{
targetingPackTargetFramework = "netcoreapp5.0";
targetingPackDllFolder = TaskEnvironment.GetAbsolutePath(
Path.Combine(targetingPackRoot.OriginalValue, "ref", targetingPackTargetFramework));
}
string targetingPackOriginalDataPath = Path.Combine(targetingPackRoot.OriginalValue, "data");
AbsolutePath platformManifestPath = TaskEnvironment.GetAbsolutePath(
Path.Combine(targetingPackOriginalDataPath, "PlatformManifest.txt"));
AbsolutePath packageOverridesPath = TaskEnvironment.GetAbsolutePath(
Path.Combine(targetingPackOriginalDataPath, "PackageOverrides.txt"));
AbsolutePath frameworkListPath = TaskEnvironment.GetAbsolutePath(
Path.Combine(targetingPackOriginalDataPath, "FrameworkList.xml"));
FrameworkListDefinition definition = new(
frameworkListPath: frameworkListPath,
targetingPackRoot: targetingPackRoot,
targetingPackDllFolder: targetingPackDllFolder,
targetingPack.Name,
targetingPack.Profile,
targetingPack.NuGetPackageId,
targetingPack.NuGetPackageVersion,
inputs.ProjectLanguage);
AddItemsFromFrameworkList(definition, buildEngine, referencesToAdd, analyzersToAdd, allowCacheLookup);
if (File.Exists(platformManifestPath))
{
platformManifests.Add(new TaskItem(platformManifestPath.OriginalValue));
}
if (File.Exists(packageOverridesPath))
{
packageConflictOverrides.Add(CreatePackageOverride(targetingPack.NuGetPackageId, packageOverridesPath));
}
preferredPackages.AddRange(targetingPack.PackageConflictPreferredPackages.Split(';'));
}
}
}
// Calculate which RuntimeFramework items should actually be used based on framework references
HashSet<string> frameworkReferenceNames = new(frameworkReferences.Select(fr => fr.Name), StringComparer.OrdinalIgnoreCase);
// Filter out duplicate references (which can happen when referencing two different profiles that overlap)
List<TaskItem> deduplicatedReferences = DeduplicateItems(referencesToAdd);
List<TaskItem> deduplicatedAnalyzers = DeduplicateItems(analyzersToAdd);
ResolvedAssetsCacheEntry newCacheEntry = new()
{
ReferencesToAdd = deduplicatedReferences.Distinct().ToArray(),
AnalyzersToAdd = deduplicatedAnalyzers.Distinct().ToArray(),
PlatformManifests = platformManifests.ToArray(),
PackageConflictOverrides = packageConflictOverrides.ToArray(),
PackageConflictPreferredPackages = string.Join(";", preferredPackages),
UsedRuntimeFrameworks = inputs.RuntimeFrameworks.Where(rf => frameworkReferenceNames.Contains(rf.FrameworkName))
.Select(rf => rf.Item)
.ToArray(),
Errors = errors.ToArray(),
};
return newCacheEntry;
}
// Get distinct items based on case-insensitive ItemSpec comparison
private static List<TaskItem> DeduplicateItems(List<TaskItem> items)
{
HashSet<string> seenItemSpecs = new(StringComparer.OrdinalIgnoreCase);
List<TaskItem> deduplicatedItems = new(items.Count);
foreach (var item in items)
{
if (seenItemSpecs.Add(item.ItemSpec))
{
deduplicatedItems.Add(item);
}
}
return deduplicatedItems;
}
private static TaskItem CreatePackageOverride(string runtimeFrameworkName, AbsolutePath packageOverridesPath)
{
TaskItem packageOverride = new(runtimeFrameworkName);
packageOverride.SetMetadata("OverriddenPackages", File.ReadAllText(packageOverridesPath));
return packageOverride;
}
private void AddNetStandardTargetingPackAssets(TargetingPack targetingPack, AbsolutePath targetingPackRoot, List<TaskItem> referencesToAdd)
{
string targetingPackTargetFramework = targetingPack.TargetFramework;
AbsolutePath targetingPackAssetPath = TaskEnvironment.GetAbsolutePath(
Path.Combine(targetingPackRoot.OriginalValue, "build", targetingPackTargetFramework, "ref"));
foreach (var dll in Directory.GetFiles(targetingPackAssetPath, "*.dll"))
{
string itemSpec = Path.Combine(targetingPackAssetPath.OriginalValue, Path.GetFileName(dll));
var reference = CreateItem(
itemSpec,
targetingPack.Name,
targetingPack.NuGetPackageId,
targetingPack.NuGetPackageVersion);
if (!Path.GetFileName(dll).Equals("netstandard.dll", StringComparison.OrdinalIgnoreCase))
{
reference.SetMetadata("Facade", "true");
}
referencesToAdd.Add(reference);
}
}
private static void AddItemsFromFrameworkList(FrameworkListDefinition definition, IBuildEngine4 buildEngine4, List<TaskItem> referenceItems, List<TaskItem> analyzerItems, bool allowCacheLookup)
{
string frameworkListKey = definition.CacheKey();
if (allowCacheLookup && buildEngine4 is not null)
{
if (buildEngine4.GetRegisteredTaskObject(
frameworkListKey,
RegisteredTaskObjectLifetime.AppDomain)
is FrameworkList cacheEntry)
{
// As above, we are not even checking timestamps here
// and instead assuming that the targeting pack folder
// is fully immutable.
analyzerItems.AddRange(cacheEntry.Analyzers);
referenceItems.AddRange(cacheEntry.References);
return;
}
FrameworkList list = ReadFrameworkList(definition);
buildEngine4.RegisterTaskObject(frameworkListKey, list, RegisteredTaskObjectLifetime.AppDomain, allowEarlyCollection: true);
analyzerItems.AddRange(list.Analyzers);
referenceItems.AddRange(list.References);
return;
}
FrameworkList uncachedList = ReadFrameworkList(definition);
analyzerItems.AddRange(uncachedList.Analyzers);
referenceItems.AddRange(uncachedList.References);
}
private static FrameworkList ReadFrameworkList(FrameworkListDefinition definition)
{
XDocument frameworkListDoc = XDocument.Load(definition.FrameworkListPath);
string profile = definition.Profile;
bool usePathElementsInFrameworkListAsFallBack =
TestFirstFileInFrameworkListUsingAssemblyNameConvention(definition.TargetingPackDllFolder, frameworkListDoc);
List<TaskItem> referenceItemsFromThisFramework = new();
List<TaskItem> analyzerItemsFromThisFramework = new();
foreach (var fileElement in frameworkListDoc.Root.Elements("File"))
{
if (!string.IsNullOrEmpty(profile))
{
var profileAttributeValue = fileElement.Attribute("Profile")?.Value;
if (profileAttributeValue == null)
{
// If profile was specified but this assembly doesn't belong to any profiles, don't reference it
continue;
}
var assemblyProfiles = profileAttributeValue.Split(';');
if (!assemblyProfiles.Contains(profile, StringComparer.OrdinalIgnoreCase))
{
// Assembly wasn't in profile specified, so don't reference it
continue;
}
}
string referencedByDefaultAttributeValue = fileElement.Attribute("ReferencedByDefault")?.Value;
if (referencedByDefaultAttributeValue != null &&
referencedByDefaultAttributeValue.Equals("false", StringComparison.OrdinalIgnoreCase))
{
// Don't automatically reference this assembly if it has ReferencedByDefault="false"
continue;
}
string itemType = fileElement.Attribute("Type")?.Value;
bool isAnalyzer = itemType?.Equals("Analyzer", StringComparison.OrdinalIgnoreCase) ?? false;
string assemblyName = fileElement.Attribute("AssemblyName").Value;
string dllPath = usePathElementsInFrameworkListAsFallBack || isAnalyzer ?
Path.Combine(definition.TargetingPackRoot.OriginalValue, fileElement.Attribute("Path").Value) :
GetDllPathViaAssemblyName(definition.TargetingPackDllFolder.OriginalValue, assemblyName);
var item = CreateItem(dllPath, definition.FrameworkReferenceName, definition.NuGetPackageId, definition.NuGetPackageVersion);
item.SetMetadata("AssemblyName", assemblyName);
item.SetMetadata("AssemblyVersion", fileElement.Attribute("AssemblyVersion").Value);
item.SetMetadata("FileVersion", fileElement.Attribute("FileVersion").Value);
item.SetMetadata("PublicKeyToken", fileElement.Attribute("PublicKeyToken").Value);
if (isAnalyzer)
{
string itemLanguage = fileElement.Attribute("Language")?.Value;
if (itemLanguage != null)
{
// expect cs instead of C#, fs rather than F# per NuGet conventions
string projectLanguage = definition.ProjectLanguage?.Replace('#', 's');
if (projectLanguage == null || !projectLanguage.Equals(itemLanguage, StringComparison.OrdinalIgnoreCase))
{
continue;
}
}
analyzerItemsFromThisFramework.Add(item);
}
else
{
referenceItemsFromThisFramework.Add(item);
}
}
return new FrameworkList
{
Analyzers = analyzerItemsFromThisFramework.ToArray(),
References = referenceItemsFromThisFramework.ToArray(),
};
}
/// <summary>
/// Due to https://github.com/dotnet/sdk/issues/12098 we fall back to use "Path" when "AssemblyName" will
/// not resolve the actual dll.
/// </summary>
/// <returns>if use we should use "Path" element in frameworkList as a fallback</returns>
private static bool TestFirstFileInFrameworkListUsingAssemblyNameConvention(AbsolutePath targetingPackDllFolder,
XDocument frameworkListDoc)
{
bool usePathElementsInFrameworkListPathAsFallBack;
var firstFileElement = frameworkListDoc.Root.Elements("File").FirstOrDefault();
if (firstFileElement == null)
{
usePathElementsInFrameworkListPathAsFallBack = false;
}
else
{
string assemblyName = firstFileElement.Attribute("AssemblyName").Value;
string dllPath = Path.Combine(targetingPackDllFolder, assemblyName + ".dll");
usePathElementsInFrameworkListPathAsFallBack = !File.Exists(dllPath);
}
return usePathElementsInFrameworkListPathAsFallBack;
}
private static string GetDllPathViaAssemblyName(string targetingPackDllFolder, string assemblyName)
{
var dllPath = Path.Combine(targetingPackDllFolder, assemblyName + ".dll");
return dllPath;
}
private static TaskItem CreateItem(string dll, ITaskItem targetingPack)
{
return CreateItem(
dll,
targetingPack.ItemSpec,
targetingPack.GetMetadata(MetadataKeys.NuGetPackageId),
targetingPack.GetMetadata(MetadataKeys.NuGetPackageVersion));
}
private static TaskItem CreateItem(string dll, string frameworkReferenceName, string nuGetPackageId, string nuGetPackageVersion)
{
var reference = new TaskItem(dll);
reference.SetMetadata(MetadataKeys.ExternallyResolved, "true");
reference.SetMetadata(MetadataKeys.Private, "false");
reference.SetMetadata(MetadataKeys.NuGetPackageId, nuGetPackageId);
reference.SetMetadata(MetadataKeys.NuGetPackageVersion, nuGetPackageVersion);
reference.SetMetadata("FrameworkReferenceName", frameworkReferenceName);
reference.SetMetadata("FrameworkReferenceVersion", nuGetPackageVersion);
return reference;
}
internal class StronglyTypedInputs
{
public FrameworkReference[] FrameworkReferences { get; private set; }
public TargetingPack[] ResolvedTargetingPacks { get; private set; }
public RuntimeFramework[] RuntimeFrameworks { get; private set; }
public bool GenerateErrorForMissingTargetingPacks { get; private set; }
public bool NuGetRestoreSupported { get; private set; }
public bool DisableTransitiveFrameworkReferences { get; private set; }
public string NetCoreTargetingPackRoot { get; private set; }
public string ProjectLanguage { get; private set; }
public StronglyTypedInputs(
ITaskItem[] frameworkReferences,
TargetingPack[] resolvedTargetingPacks,
ITaskItem[] runtimeFrameworks,
bool generateErrorForMissingTargetingPacks,
bool nuGetRestoreSupported,
bool disableTransitiveFrameworkReferences,
string netCoreTargetingPackRoot,
string projectLanguage)
{
FrameworkReferences = frameworkReferences.Select(fr => new FrameworkReference(fr.ItemSpec)).ToArray();
ResolvedTargetingPacks = resolvedTargetingPacks;
RuntimeFrameworks = runtimeFrameworks.Select(item => new RuntimeFramework(item.ItemSpec, item.GetMetadata(MetadataKeys.FrameworkName), item)).ToArray();
GenerateErrorForMissingTargetingPacks = generateErrorForMissingTargetingPacks;
NuGetRestoreSupported = nuGetRestoreSupported;
DisableTransitiveFrameworkReferences = disableTransitiveFrameworkReferences;
NetCoreTargetingPackRoot = netCoreTargetingPackRoot;
ProjectLanguage = projectLanguage;
}
public string CacheKey()
{
StringBuilder cacheKeyBuilder = new(nameof(ResolveTargetingPackAssets) + nameof(StronglyTypedInputs));
cacheKeyBuilder.AppendLine();
foreach (var frameworkReference in FrameworkReferences)
{
cacheKeyBuilder.AppendLine(frameworkReference.CacheKey());
}
cacheKeyBuilder.AppendLine();
foreach (var resolvedTargetingPack in ResolvedTargetingPacks)
{
cacheKeyBuilder.AppendLine(resolvedTargetingPack.CacheKey());
}
cacheKeyBuilder.AppendLine(nameof(RuntimeFrameworks));
foreach (var runtimeFramework in RuntimeFrameworks)
{
cacheKeyBuilder.AppendLine(runtimeFramework.CacheKey());
}
cacheKeyBuilder.AppendLine($"{nameof(GenerateErrorForMissingTargetingPacks)}={GenerateErrorForMissingTargetingPacks}");
cacheKeyBuilder.AppendLine($"{nameof(NuGetRestoreSupported)}={NuGetRestoreSupported}");
cacheKeyBuilder.AppendLine($"{nameof(DisableTransitiveFrameworkReferences)}={DisableTransitiveFrameworkReferences}");
cacheKeyBuilder.AppendLine($"{nameof(NetCoreTargetingPackRoot)}={NetCoreTargetingPackRoot}");
cacheKeyBuilder.AppendLine($"{nameof(ProjectLanguage)}={ProjectLanguage}");
return cacheKeyBuilder.ToString();
}
}
private class FrameworkList
{
public IReadOnlyList<TaskItem> Analyzers;
public IReadOnlyList<TaskItem> References;
}
internal class FrameworkReference
{
public string Name { get; private set; }
public FrameworkReference(string name)
{
Name = name;
}
public string CacheKey()
{
return $"FrameworkReference: {Name}";
}
}
internal class TargetingPack
{
public string Name { get; private set; }
public AbsolutePath Path { get; private set; }
public string Format { get; private set; }
public string TargetFramework { get; private set; }
public string Profile { get; private set; }
public string NuGetPackageId { get; private set; }
public string NuGetPackageVersion { get; private set; }
public string PackageConflictPreferredPackages { get; private set; }
public TargetingPack(
string name,
AbsolutePath path,
string format,
string targetFramework,
string profile,
string nuGetPackageId,
string nuGetPackageVersion,
string packageConflictPreferredPackages)
{
Name = name;
Path = path;
Format = format;
TargetFramework = targetFramework;
Profile = profile;
NuGetPackageId = nuGetPackageId;
NuGetPackageVersion = nuGetPackageVersion;
PackageConflictPreferredPackages = packageConflictPreferredPackages;
}
public string CacheKey()
{
// AbsolutePath carries both the resolved (Value) and user-input (OriginalValue) forms;
// both contribute to outputs, so both must be in the key.
StringBuilder builder = new();
builder.AppendLine(nameof(TargetingPack));
builder.AppendLine(Name);
builder.AppendLine(Path.Value);
builder.AppendLine(Path.OriginalValue);
builder.AppendLine(Format);
builder.AppendLine(TargetFramework);
builder.AppendLine(Profile);
builder.AppendLine(NuGetPackageId);
builder.AppendLine(NuGetPackageVersion);
builder.AppendLine(PackageConflictPreferredPackages);
return builder.ToString();
}
}
internal class RuntimeFramework
{
public string Name { get; private set; }
public string FrameworkName { get; private set; }
public readonly ITaskItem Item;
public RuntimeFramework(string name, string frameworkName, ITaskItem item)
{
Name = name;
FrameworkName = frameworkName;
Item = item;
}
public string CacheKey()
{
// NOTE: The cache key should include any metadata added to runtimeFramework items
// in ProcessFrameworkReferences.
return $"{nameof(RuntimeFramework)}: {Name} ({FrameworkName} {Item?.GetMetadata(MetadataKeys.Version)} {Item?.GetMetadata(MetadataKeys.Profile)})";
}
}
internal readonly struct FrameworkListDefinition
{
public readonly AbsolutePath FrameworkListPath;
public readonly AbsolutePath TargetingPackRoot;
public readonly AbsolutePath TargetingPackDllFolder;
public readonly string ProjectLanguage;
public readonly string FrameworkReferenceName;
public readonly string Profile;
public readonly string NuGetPackageId;
public readonly string NuGetPackageVersion;
public FrameworkListDefinition(AbsolutePath frameworkListPath,
AbsolutePath targetingPackRoot,
AbsolutePath targetingPackDllFolder,
string frameworkReferenceName,
string profile,
string nuGetPackageId,
string nuGetPackageVersion,
string projectLanguage)
{
FrameworkListPath = frameworkListPath;
TargetingPackRoot = targetingPackRoot;
TargetingPackDllFolder = targetingPackDllFolder;
ProjectLanguage = projectLanguage;
FrameworkReferenceName = frameworkReferenceName;
Profile = profile;
NuGetPackageId = nuGetPackageId;
NuGetPackageVersion = nuGetPackageVersion;
}
/// <summary>
/// Construct a key for the framework-specific cache lookup.
/// </summary>
public string CacheKey()
{
// IMPORTANT: any input changes that can affect the output should be included in this key.
// AbsolutePath carries both the resolved (Value) and user-input (OriginalValue) forms;
// both contribute to outputs, so both must be in the key.
StringBuilder keyBuilder = new(nameof(FrameworkListDefinition));
keyBuilder.AppendLine();
keyBuilder.AppendLine(FrameworkListPath.Value);
keyBuilder.AppendLine(FrameworkListPath.OriginalValue);
keyBuilder.AppendLine(TargetingPackRoot.Value);
keyBuilder.AppendLine(TargetingPackRoot.OriginalValue);
keyBuilder.AppendLine(TargetingPackDllFolder.Value);
keyBuilder.AppendLine(TargetingPackDllFolder.OriginalValue);
keyBuilder.AppendLine(FrameworkReferenceName);
keyBuilder.AppendLine(Profile);
keyBuilder.AppendLine(NuGetPackageId);
keyBuilder.AppendLine(NuGetPackageVersion);
keyBuilder.AppendLine(ProjectLanguage);
string frameworkListKey = keyBuilder.ToString();
return frameworkListKey;
}
}
private class ResolvedAssetsCacheEntry
{
public ITaskItem[] ReferencesToAdd;
public ITaskItem[] AnalyzersToAdd;
public ITaskItem[] PlatformManifests;
public string PackageConflictPreferredPackages;
public ITaskItem[] PackageConflictOverrides;
public ITaskItem[] UsedRuntimeFrameworks;
public string[] Errors;
}
}
}