forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollectSDKReferencesDesignTime.cs
95 lines (78 loc) · 3.63 KB
/
CollectSDKReferencesDesignTime.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
// 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;
namespace Microsoft.NET.Build.Tasks
{
/// <summary>
/// Aggregates the sdk specified as project items and implicit packages references.
/// Note: this task is not used temporarily as a hack for RTM to be able to get all
/// implicit SDKs across all TFMs. In U1 we will switch back to this task when multiple
/// TFM support is added to Dependencies logic.
/// Tracking issue https://github.com/dotnet/roslyn-project-system/issues/587
/// </summary>
public class CollectSDKReferencesDesignTime : TaskBase
{
[Required]
public ITaskItem[] SdkReferences { get; set; }
[Required]
public ITaskItem[] PackageReferences { get; set; }
[Required]
public string DefaultImplicitPackages { get; set; }
[Output]
public ITaskItem[] SDKReferencesDesignTime { get; set; }
private HashSet<string> ImplicitPackageReferences { get; set; }
protected override void ExecuteCore()
{
ImplicitPackageReferences = GetImplicitPackageReferences(DefaultImplicitPackages);
var sdkDesignTimeList = new List<ITaskItem>(SdkReferences);
sdkDesignTimeList.AddRange(GetImplicitPackageReferences());
SDKReferencesDesignTime = sdkDesignTimeList.ToArray();
}
internal static HashSet<string> GetImplicitPackageReferences(string defaultImplicitPackages)
{
var implicitPackageReferences = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
if (string.IsNullOrEmpty(defaultImplicitPackages))
{
return implicitPackageReferences;
}
var packageNames = defaultImplicitPackages.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
if (packageNames.Length == 0)
{
return implicitPackageReferences;
}
foreach (var packageReference in packageNames)
{
implicitPackageReferences.Add(packageReference);
}
return implicitPackageReferences;
}
private IEnumerable<ITaskItem> GetImplicitPackageReferences()
{
var implicitPackages = new List<ITaskItem>();
foreach (var packageReference in PackageReferences)
{
var isImplicitlyDefined = false;
var isImplicitlyDefinedString = packageReference.GetMetadata(MetadataKeys.IsImplicitlyDefined);
if (string.IsNullOrEmpty(isImplicitlyDefinedString))
{
isImplicitlyDefined = ImplicitPackageReferences.Contains(packageReference.ItemSpec);
}
else
{
bool.TryParse(isImplicitlyDefinedString, out isImplicitlyDefined);
}
if (isImplicitlyDefined)
{
var newTaskItem = new TaskItem(packageReference.ItemSpec);
newTaskItem.SetMetadata(MetadataKeys.SDKPackageItemSpec, string.Empty);
newTaskItem.SetMetadata(MetadataKeys.Name, packageReference.ItemSpec);
newTaskItem.SetMetadata(MetadataKeys.IsImplicitlyDefined, "True");
newTaskItem.SetMetadata(MetadataKeys.Version, packageReference.GetMetadata(MetadataKeys.Version));
implicitPackages.Add(newTaskItem);
}
}
return implicitPackages;
}
}
}