forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FilterResolvedFiles.cs
96 lines (78 loc) · 3.02 KB
/
FilterResolvedFiles.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
// 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 NuGet.Packaging.Core;
using NuGet.ProjectModel;
namespace Microsoft.NET.Build.Tasks
{
/// <summary>
/// Filters out the assemblies from the list based on a given package closure.
/// </summary>
public class FilterResolvedFiles : TaskBase
{
private readonly List<ITaskItem> _assembliesToPublish = new();
private readonly List<ITaskItem> _packagesResolved = new();
public string AssetsFilePath { get; set; }
[Required]
public ITaskItem[] ResolvedFiles { get; set; }
[Required]
public ITaskItem[] PackagesToPrune { get; set; }
[Required]
public string TargetFramework { get; set; }
public string RuntimeIdentifier { get; set; }
public string PlatformLibraryName { get; set; }
public bool IsSelfContained { get; set; }
/// <summary>
/// All the assemblies to publish.
/// </summary>
[Output]
public ITaskItem[] AssembliesToPublish
{
get; private set;
}
[Output]
public ITaskItem[] PublishedPackages
{
get; private set;
}
protected override void ExecuteCore()
{
var lockFileCache = new LockFileCache(this);
LockFile lockFile = lockFileCache.GetLockFile(AssetsFilePath);
ProjectContext projectContext = lockFile.CreateProjectContext(
TargetFramework,
RuntimeIdentifier,
PlatformLibraryName,
runtimeFrameworks: null,
IsSelfContained);
var packageClosure = new HashSet<PackageIdentity>();
foreach (var packageItem in PackagesToPrune)
{
var pkgName = packageItem.ItemSpec;
if (!string.IsNullOrEmpty(pkgName))
{
packageClosure.UnionWith(projectContext.GetTransitiveList(pkgName, ignoreIfNotFound: true));
}
}
var packagesToPublish = new HashSet<PackageIdentity>();
foreach (var resolvedFile in ResolvedFiles)
{
var resolvedPkg = ItemUtilities.GetPackageIdentity(resolvedFile);
if (resolvedPkg != null && !packageClosure.Contains(resolvedPkg))
{
_assembliesToPublish.Add(resolvedFile);
packagesToPublish.Add(resolvedPkg);
}
}
AssembliesToPublish = _assembliesToPublish.ToArray();
foreach (var resolvedPkg in packagesToPublish)
{
TaskItem item = new(resolvedPkg.Id);
item.SetMetadata("Version", resolvedPkg.Version.ToString());
_packagesResolved.Add(item);
}
PublishedPackages = _packagesResolved.ToArray();
}
}
}