forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateDepsFile.cs
283 lines (235 loc) · 11.4 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
// 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 Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.Extensions.DependencyModel;
using Newtonsoft.Json;
using NuGet.Packaging.Core;
using NuGet.ProjectModel;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
namespace Microsoft.NET.Build.Tasks
{
/// <summary>
/// Generates the $(project).deps.json file.
/// </summary>
public class GenerateDepsFile : TaskBase
{
[Required]
public string ProjectPath { get; set; }
[Required]
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; }
[Required]
public string AssemblyName { get; set; }
[Required]
public string AssemblyExtension { get; set; }
[Required]
public string AssemblyVersion { get; set; }
[Required]
public ITaskItem[] AssemblySatelliteAssemblies { get; set; }
[Required]
public ITaskItem[] ReferencePaths { get; set; }
[Required]
public ITaskItem[] ReferenceSatellitePaths { get; set; }
[Required]
public ITaskItem[] FilesToSkip { get; set; }
public ITaskItem CompilerOptions { get; set; }
public ITaskItem[] PrivateAssetsPackageReferences { get; set; }
public string[] TargetManifestFileList { get; set; }
public bool IsSelfContained { get; set; }
List<ITaskItem> _filesWritten = new List<ITaskItem>();
[Output]
public ITaskItem[] FilesWritten
{
get { return _filesWritten.ToArray(); }
}
private Dictionary<string, HashSet<string>> compileFilesToSkip = new Dictionary<string, HashSet<string>>(StringComparer.OrdinalIgnoreCase);
private Dictionary<string, HashSet<string>> runtimeFilesToSkip = new Dictionary<string, HashSet<string>>(StringComparer.OrdinalIgnoreCase);
private Dictionary<PackageIdentity, StringBuilder> GetFilteredPackages()
{
Dictionary<PackageIdentity, StringBuilder> filteredPackages = null;
if (TargetManifestFileList != null && TargetManifestFileList.Length > 0)
{
filteredPackages = new Dictionary<PackageIdentity, StringBuilder>();
foreach (var targetManifestFile in TargetManifestFileList)
{
Log.LogMessage(MessageImportance.Low, string.Format(CultureInfo.CurrentCulture, Strings.ParsingFiles, targetManifestFile));
var packagesSpecified = StoreArtifactParser.Parse(targetManifestFile);
var targetManifestFileName = Path.GetFileName(targetManifestFile);
foreach (var pkg in packagesSpecified)
{
Log.LogMessage(MessageImportance.Low, string.Format(CultureInfo.CurrentCulture, Strings.PackageInfoLog, pkg.Id, pkg.Version));
StringBuilder fileList;
if (filteredPackages.TryGetValue(pkg, out fileList))
{
fileList.Append($";{targetManifestFileName}");
}
else
{
filteredPackages.Add(pkg, new StringBuilder(targetManifestFileName));
}
}
}
}
return filteredPackages;
}
protected override void ExecuteCore()
{
LoadFilesToSkip();
LockFile lockFile = new LockFileCache(BuildEngine4).GetLockFile(AssetsFilePath);
CompilationOptions compilationOptions = CompilationOptionsConverter.ConvertFrom(CompilerOptions);
SingleProjectInfo mainProject = SingleProjectInfo.Create(
ProjectPath,
AssemblyName,
AssemblyExtension,
AssemblyVersion,
AssemblySatelliteAssemblies);
IEnumerable<ReferenceInfo> frameworkReferences =
ReferenceInfo.CreateFrameworkReferenceInfos(ReferencePaths);
IEnumerable<ReferenceInfo> directReferences =
ReferenceInfo.CreateDirectReferenceInfos(ReferencePaths, ReferenceSatellitePaths);
Dictionary<string, SingleProjectInfo> referenceProjects = SingleProjectInfo.CreateProjectReferenceInfos(
ReferencePaths,
ReferenceSatellitePaths);
IEnumerable<string> privateAssets = PackageReferenceConverter.GetPackageIds(PrivateAssetsPackageReferences);
ProjectContext projectContext = lockFile.CreateProjectContext(
NuGetUtils.ParseFrameworkName(TargetFramework),
RuntimeIdentifier,
PlatformLibraryName,
IsSelfContained);
DependencyContext dependencyContext = new DependencyContextBuilder(mainProject, projectContext)
.WithFrameworkReferences(frameworkReferences)
.WithDirectReferences(directReferences)
.WithReferenceProjectInfos(referenceProjects)
.WithPrivateAssets(privateAssets)
.WithCompilationOptions(compilationOptions)
.WithReferenceAssembliesPath(FrameworkReferenceResolver.GetDefaultReferenceAssembliesPath())
.WithPackagesThatWhereFiltered(GetFilteredPackages())
.Build();
if (compileFilesToSkip.Any() || runtimeFilesToSkip.Any())
{
dependencyContext = TrimFilesToSkip(dependencyContext);
}
var writer = new DependencyContextWriter();
using (var fileStream = File.Create(DepsFilePath))
{
writer.Write(dependencyContext, fileStream);
}
_filesWritten.Add(new TaskItem(DepsFilePath));
}
private void LoadFilesToSkip()
{
foreach (var fileToSkip in FilesToSkip)
{
string packageId, packageSubPath;
NuGetUtils.GetPackageParts(fileToSkip.ItemSpec, out packageId, out packageSubPath);
if (String.IsNullOrEmpty(packageId) || String.IsNullOrEmpty(packageSubPath))
{
continue;
}
var itemType = fileToSkip.GetMetadata(nameof(ConflictResolution.ConflictItemType));
var packagesWithFilesToSkip = (itemType == nameof(ConflictResolution.ConflictItemType.Reference)) ? compileFilesToSkip : runtimeFilesToSkip;
HashSet<string> filesToSkipForPackage;
if (!packagesWithFilesToSkip.TryGetValue(packageId, out filesToSkipForPackage))
{
packagesWithFilesToSkip[packageId] = filesToSkipForPackage = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
}
filesToSkipForPackage.Add(packageSubPath);
}
}
private DependencyContext TrimFilesToSkip(DependencyContext sourceDeps)
{
return new DependencyContext(sourceDeps.Target,
sourceDeps.CompilationOptions,
TrimCompilationLibraries(sourceDeps.CompileLibraries),
TrimRuntimeLibraries(sourceDeps.RuntimeLibraries),
sourceDeps.RuntimeGraph);
}
private IEnumerable<RuntimeLibrary> TrimRuntimeLibraries(IReadOnlyList<RuntimeLibrary> runtimeLibraries)
{
foreach (var runtimeLibrary in runtimeLibraries)
{
HashSet<string> filesToSkip;
if (runtimeFilesToSkip.TryGetValue(runtimeLibrary.Name, out filesToSkip))
{
yield return new RuntimeLibrary(runtimeLibrary.Type,
runtimeLibrary.Name,
runtimeLibrary.Version,
runtimeLibrary.Hash,
TrimAssetGroups(runtimeLibrary.RuntimeAssemblyGroups, filesToSkip).ToArray(),
TrimAssetGroups(runtimeLibrary.NativeLibraryGroups, filesToSkip).ToArray(),
TrimResourceAssemblies(runtimeLibrary.ResourceAssemblies, filesToSkip),
runtimeLibrary.Dependencies,
runtimeLibrary.Serviceable,
runtimeLibrary.Path,
runtimeLibrary.HashPath,
runtimeLibrary.RuntimeStoreManifestName);
}
else
{
yield return runtimeLibrary;
}
}
}
private IEnumerable<RuntimeAssetGroup> TrimAssetGroups(IEnumerable<RuntimeAssetGroup> assetGroups, ISet<string> filesToTrim)
{
foreach (var assetGroup in assetGroups)
{
yield return new RuntimeAssetGroup(assetGroup.Runtime, TrimAssemblies(assetGroup.AssetPaths, filesToTrim));
}
}
private IEnumerable<ResourceAssembly> TrimResourceAssemblies(IEnumerable<ResourceAssembly> resourceAssemblies, ISet<string> filesToTrim)
{
foreach (var resourceAssembly in resourceAssemblies)
{
if (!filesToTrim.Contains(resourceAssembly.Path))
{
yield return resourceAssembly;
}
}
}
private IEnumerable<CompilationLibrary> TrimCompilationLibraries(IReadOnlyList<CompilationLibrary> compileLibraries)
{
foreach (var compileLibrary in compileLibraries)
{
HashSet<string> filesToSkip;
if (compileFilesToSkip.TryGetValue(compileLibrary.Name, out filesToSkip))
{
yield return new CompilationLibrary(compileLibrary.Type,
compileLibrary.Name,
compileLibrary.Version,
compileLibrary.Hash,
TrimAssemblies(compileLibrary.Assemblies, filesToSkip),
compileLibrary.Dependencies,
compileLibrary.Serviceable,
compileLibrary.Path,
compileLibrary.HashPath);
}
else
{
yield return compileLibrary;
}
}
}
private IEnumerable<string> TrimAssemblies(IEnumerable<string> assemblies, ISet<string> filesToTrim)
{
foreach (var assembly in assemblies)
{
if (!filesToTrim.Contains(assembly))
{
yield return assembly;
}
}
}
}
}