forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectContext.cs
175 lines (146 loc) · 6.99 KB
/
ProjectContext.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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NuGet.ProjectModel;
namespace Microsoft.NET.Build.Tasks
{
public class ProjectContext
{
private readonly LockFile _lockFile;
private readonly LockFileTarget _lockFileTarget;
private readonly string _platformLibraryName;
public bool IsPortable { get; }
public LockFileTargetLibrary PlatformLibrary { get; }
public LockFile LockFile => _lockFile;
public LockFileTarget LockFileTarget => _lockFileTarget;
public ProjectContext(LockFile lockFile, LockFileTarget lockFileTarget, string platformLibraryName)
{
_lockFile = lockFile;
_lockFileTarget = lockFileTarget;
_platformLibraryName = platformLibraryName;
PlatformLibrary = _lockFileTarget.GetLibrary(_platformLibraryName);
IsPortable = PlatformLibrary != null && string.IsNullOrEmpty(_lockFileTarget.RuntimeIdentifier);
}
public IEnumerable<LockFileTargetLibrary> GetRuntimeLibraries(IEnumerable<string> privateAssetPackageIds)
{
IEnumerable<LockFileTargetLibrary> runtimeLibraries = _lockFileTarget.Libraries;
Dictionary<string, LockFileTargetLibrary> libraryLookup =
runtimeLibraries.ToDictionary(e => e.Name, StringComparer.OrdinalIgnoreCase);
HashSet<string> allExclusionList = new HashSet<string>();
if (IsPortable)
{
allExclusionList.UnionWith(_lockFileTarget.GetPlatformExclusionList(PlatformLibrary, libraryLookup));
}
if (privateAssetPackageIds?.Any() == true)
{
HashSet<string> privateAssetsExclusionList =
GetPrivateAssetsExclusionList(
privateAssetPackageIds,
libraryLookup);
allExclusionList.UnionWith(privateAssetsExclusionList);
}
return runtimeLibraries.Filter(allExclusionList).ToArray();
}
public IEnumerable<LockFileTargetLibrary> GetCompileLibraries(IEnumerable<string> compilePrivateAssetPackageIds)
{
IEnumerable<LockFileTargetLibrary> compileLibraries = _lockFileTarget.Libraries;
if (compilePrivateAssetPackageIds?.Any() == true)
{
Dictionary<string, LockFileTargetLibrary> libraryLookup =
compileLibraries.ToDictionary(e => e.Name, StringComparer.OrdinalIgnoreCase);
HashSet<string> privateAssetsExclusionList =
GetPrivateAssetsExclusionList(
compilePrivateAssetPackageIds,
libraryLookup);
compileLibraries = compileLibraries.Filter(privateAssetsExclusionList);
}
return compileLibraries.ToArray();
}
public IEnumerable<string> GetTopLevelDependencies()
{
Dictionary<string, LockFileTargetLibrary> libraryLookup =
LockFileTarget.Libraries.ToDictionary(l => l.Name, StringComparer.OrdinalIgnoreCase);
return LockFile
.ProjectFileDependencyGroups
.Where(dg => dg.FrameworkName == string.Empty ||
dg.FrameworkName == LockFileTarget.TargetFramework.DotNetFrameworkName)
.SelectMany(g => g.Dependencies)
.Select(projectFileDependency =>
{
int separatorIndex = projectFileDependency.IndexOf(' ');
string libraryName = separatorIndex > 0 ?
projectFileDependency.Substring(0, separatorIndex) :
projectFileDependency;
if (!string.IsNullOrEmpty(libraryName) && libraryLookup.ContainsKey(libraryName))
{
return libraryName;
}
return null;
})
.Where(libraryName => libraryName != null)
.ToArray();
}
public HashSet<string> GetPrivateAssetsExclusionList(
IEnumerable<string> privateAssetPackageIds,
IDictionary<string, LockFileTargetLibrary> libraryLookup)
{
var nonPrivateAssets = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var nonPrivateAssetsToSearch = new Stack<string>();
var privateAssetsToSearch = new Stack<string>();
// Start with the top-level dependencies, and put them into "private" or "non-private" buckets
var privateAssetPackagesLookup = new HashSet<string>(privateAssetPackageIds, StringComparer.OrdinalIgnoreCase);
foreach (var topLevelDependency in GetTopLevelDependencies())
{
if (!privateAssetPackagesLookup.Contains(topLevelDependency))
{
nonPrivateAssetsToSearch.Push(topLevelDependency);
nonPrivateAssets.Add(topLevelDependency);
}
else
{
privateAssetsToSearch.Push(topLevelDependency);
}
}
LockFileTargetLibrary library;
string libraryName;
// Walk all the non-private assets' dependencies and mark them as non-private
while (nonPrivateAssetsToSearch.Count > 0)
{
libraryName = nonPrivateAssetsToSearch.Pop();
if (libraryLookup.TryGetValue(libraryName, out library))
{
foreach (var dependency in library.Dependencies)
{
if (!nonPrivateAssets.Contains(dependency.Id))
{
nonPrivateAssetsToSearch.Push(dependency.Id);
nonPrivateAssets.Add(dependency.Id);
}
}
}
}
// Go through assets marked private and their dependencies
// For libraries not marked as non-private, mark them down as private
var privateAssetsToExclude = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
while (privateAssetsToSearch.Count > 0)
{
libraryName = privateAssetsToSearch.Pop();
if (libraryLookup.TryGetValue(libraryName, out library))
{
privateAssetsToExclude.Add(libraryName);
foreach (var dependency in library.Dependencies)
{
if (!nonPrivateAssets.Contains(dependency.Id))
{
privateAssetsToSearch.Push(dependency.Id);
}
}
}
}
return privateAssetsToExclude;
}
}
}