forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateDepsFile.cs
300 lines (239 loc) · 12.3 KB
/
GenerateDepsFile.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
// 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.Extensions.DependencyModel;
using Newtonsoft.Json;
using NuGet.Packaging.Core;
using NuGet.ProjectModel;
using NuGet.RuntimeModel;
namespace Microsoft.NET.Build.Tasks
{
/// <summary>
/// Generates the $(project).deps.json file.
/// </summary>
public class GenerateDepsFile : TaskBase
{
[Required]
public string ProjectPath { get; set; }
public string AssetsFilePath { get; set; }
[Required]
public string DepsFilePath { get; set; }
[Required]
public string TargetFramework { get; set; }
public string RuntimeIdentifier { get; set; }
public string PlatformLibraryName { get; set; }
public ITaskItem[] RuntimeFrameworks { get; set; }
[Required]
public string AssemblyName { get; set; }
[Required]
public string AssemblyExtension { get; set; }
[Required]
public string AssemblyVersion { get; set; }
public ITaskItem[] AssemblySatelliteAssemblies { get; set; } = Array.Empty<ITaskItem>();
[Required]
public bool IncludeMainProject { get; set; }
// @(ReferencePath) that will be passed to
public ITaskItem[] ReferencePaths { get; set; } = Array.Empty<ITaskItem>();
// Full set of @(ReferenceDependencyPaths) found by RAR
public ITaskItem[] ReferenceDependencyPaths { get; set; } = Array.Empty<ITaskItem>();
// Full set of @(ReferenceSatellitePaths) found by RAR
public ITaskItem[] ReferenceSatellitePaths { get; set; } = Array.Empty<ITaskItem>();
// Subset of @(ReferencePath) that is not CopyLocal, used for compilation, but not runtime assets
public ITaskItem[] ReferenceAssemblies { get; set; } = Array.Empty<ITaskItem>();
// Runtime assets for self-contained deployment from runtime pack
public ITaskItem[] RuntimePackAssets { get; set; } = Array.Empty<ITaskItem>();
public ITaskItem CompilerOptions { get; set; }
public ITaskItem[] RuntimeStorePackages { get; set; }
// NuGet compilation assets
[Required]
public ITaskItem[] CompileReferences { get; set; }
// NuGet runtime assets for root directory: @(NativeCopyLocalItems), @(ResourceCopyLocalItems), @(RuntimeCopyLocalItems)
[Required]
public ITaskItem[] ResolvedNuGetFiles { get; set; }
// NuGet runtime assets for runtimes* directory
[Required]
public ITaskItem[] ResolvedRuntimeTargetsFiles { get; set; }
// CopyLocal subset ot of @(ReferencePath), @(ReferenceDependencyPath)
// Used to filter out non-runtime assemblies from deps file. Only project and direct references in this
// set will be written to deps file as runtime dependencies.
public string[] UserRuntimeAssemblies { get; set; }
public bool IsSelfContained { get; set; }
public bool IsSingleFile { get; set; }
public bool IncludeRuntimeFileVersions { get; set; }
public bool IncludeProjectsNotInAssetsFile { get; set; }
// List of runtime identifer (platform part only) to validate for runtime assets
// If set, the task will warn on any RIDs that aren't in the list
public string[] ValidRuntimeIdentifierPlatformsForAssets { get; set; }
[Required]
public string RuntimeGraphPath { get; set; }
List<ITaskItem> _filesWritten = new();
[Output]
public ITaskItem[] FilesWritten
{
get { return _filesWritten.ToArray(); }
}
private Dictionary<PackageIdentity, string> GetFilteredPackages()
{
Dictionary<PackageIdentity, string> filteredPackages = null;
if (RuntimeStorePackages != null && RuntimeStorePackages.Length > 0)
{
filteredPackages = new Dictionary<PackageIdentity, string>();
foreach (var package in RuntimeStorePackages)
{
filteredPackages.Add(
ItemUtilities.GetPackageIdentity(package),
package.GetMetadata(MetadataKeys.RuntimeStoreManifestNames));
}
}
return filteredPackages;
}
private void WriteDepsFile(string depsFilePath)
{
ProjectContext projectContext = null;
LockFileLookup lockFileLookup = null;
if (AssetsFilePath != null)
{
LockFile lockFile = new LockFileCache(this).GetLockFile(AssetsFilePath);
projectContext = lockFile.CreateProjectContext(
TargetFramework,
RuntimeIdentifier,
PlatformLibraryName,
RuntimeFrameworks,
IsSelfContained);
lockFileLookup = new LockFileLookup(lockFile);
}
CompilationOptions compilationOptions = CompilationOptionsConverter.ConvertFrom(CompilerOptions);
SingleProjectInfo mainProject = SingleProjectInfo.Create(
ProjectPath,
AssemblyName,
AssemblyExtension,
AssemblyVersion,
AssemblySatelliteAssemblies);
var userRuntimeAssemblySet = new HashSet<string>(UserRuntimeAssemblies ?? Enumerable.Empty<string>(), StringComparer.OrdinalIgnoreCase);
Func<ITaskItem, bool> isUserRuntimeAssembly = item => userRuntimeAssemblySet.Contains(item.ItemSpec);
IEnumerable<ReferenceInfo> referenceAssemblyInfos =
ReferenceInfo.CreateReferenceInfos(ReferenceAssemblies);
// If there is a generated asset file, the projectContext will contain most of the project references.
// So remove any project reference contained within projectContext from directReferences to avoid duplication
IEnumerable<ReferenceInfo> directReferences =
ReferenceInfo.CreateDirectReferenceInfos(
ReferencePaths,
ReferenceSatellitePaths,
lockFileLookup,
isUserRuntimeAssembly,
IncludeProjectsNotInAssetsFile);
IEnumerable<ReferenceInfo> dependencyReferences =
ReferenceInfo.CreateDependencyReferenceInfos(ReferenceDependencyPaths, ReferenceSatellitePaths, isUserRuntimeAssembly);
Dictionary<string, SingleProjectInfo> referenceProjects =
SingleProjectInfo.CreateProjectReferenceInfos(ReferencePaths, ReferenceSatellitePaths,
isUserRuntimeAssembly);
bool ShouldIncludeRuntimeAsset(ITaskItem item)
{
if (IsSelfContained)
{
if (!IsSingleFile || !item.GetMetadata(MetadataKeys.DropFromSingleFile).Equals("true"))
{
return true;
}
}
else if (item.HasMetadataValue(MetadataKeys.RuntimePackAlwaysCopyLocal, "true"))
{
return true;
}
return false;
}
IEnumerable<RuntimePackAssetInfo> runtimePackAssets =
RuntimePackAssets.Where(ShouldIncludeRuntimeAsset).Select(RuntimePackAssetInfo.FromItem);
DependencyContextBuilder builder;
if (projectContext != null)
{
// Generate the RID-fallback for self-contained builds.
//
// In order to support loading components with RID-specific assets,
// the AssemblyDependencyResolver requires a RID fallback graph.
// The component itself should not carry the RID fallback graph with it, because
// it would need to carry graph of all the RIDs and needs updates for newer RIDs.
// For framework dependent apps, the RID fallback graph comes from the core framework Microsoft.NETCore.App,
// so there is no need to write it into the app.
// If self-contained apps, the (applicable subset of) RID fallback graph needs to be written to the deps.json manifest.
//
// If a RID-graph is provided to the DependencyContextBuilder, it generates a RID-fallback
// graph with respect to the target RuntimeIdentifier.
RuntimeGraph runtimeGraph =
IsSelfContained ? new RuntimeGraphCache(this).GetRuntimeGraph(RuntimeGraphPath) : null;
builder = new DependencyContextBuilder(mainProject, IncludeRuntimeFileVersions, runtimeGraph, projectContext, lockFileLookup);
}
else
{
builder = new DependencyContextBuilder(
mainProject,
IncludeRuntimeFileVersions,
RuntimeFrameworks,
isSelfContained: IsSelfContained,
platformLibraryName: PlatformLibraryName,
runtimeIdentifier: RuntimeIdentifier,
targetFramework: TargetFramework);
}
builder = builder
.WithMainProjectInDepsFile(IncludeMainProject)
.WithReferenceAssemblies(referenceAssemblyInfos)
.WithDirectReferences(directReferences)
.WithDependencyReferences(dependencyReferences)
.WithReferenceProjectInfos(referenceProjects)
.WithRuntimePackAssets(runtimePackAssets)
.WithCompilationOptions(compilationOptions)
.WithReferenceAssembliesPath(FrameworkReferenceResolver.GetDefaultReferenceAssembliesPath())
.WithPackagesThatWereFiltered(GetFilteredPackages());
if (CompileReferences.Length > 0)
{
builder = builder.WithCompileReferences(ReferenceInfo.CreateReferenceInfos(CompileReferences));
}
var resolvedNuGetFiles = ResolvedNuGetFiles.Select(f => new ResolvedFile(f, false))
.Concat(ResolvedRuntimeTargetsFiles.Select(f => new ResolvedFile(f, true)));
builder = builder.WithResolvedNuGetFiles(resolvedNuGetFiles);
DependencyContext dependencyContext = builder.Build();
var writer = new DependencyContextWriter();
using (var fileStream = File.Create(depsFilePath))
{
writer.Write(dependencyContext, fileStream);
}
_filesWritten.Add(new TaskItem(depsFilePath));
if (ValidRuntimeIdentifierPlatformsForAssets != null)
{
var affectedLibs = new List<string>();
var affectedRids = new List<string>();
foreach (var lib in dependencyContext.RuntimeLibraries)
{
var warnOnRids = lib.RuntimeAssemblyGroups.Select(g => g.Runtime).Where(ShouldWarnOnRuntimeIdentifer)
.Concat(lib.NativeLibraryGroups.Select(g => g.Runtime).Where(ShouldWarnOnRuntimeIdentifer));
if (warnOnRids.Any())
{
affectedLibs.Add(lib.Name);
affectedRids.AddRange(warnOnRids);
}
}
if (affectedRids.Count > 0)
{
affectedLibs.Sort();
affectedRids.Sort();
Log.LogWarning(Strings.NonPortableRuntimeIdentifierDetected, string.Join(", ", affectedRids.Distinct()), string.Join(", ", affectedLibs.Distinct()));
}
}
}
private bool ShouldWarnOnRuntimeIdentifer(string runtimeIdentifier)
{
if (string.IsNullOrEmpty(runtimeIdentifier))
return false;
int separator = runtimeIdentifier.LastIndexOf('-');
string platform = separator < 0
? runtimeIdentifier
: runtimeIdentifier.Substring(0, separator);
return Array.IndexOf(ValidRuntimeIdentifierPlatformsForAssets, platform.ToLowerInvariant()) == -1;
}
protected override void ExecuteCore()
{
WriteDepsFile(DepsFilePath);
}
}
}