-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathResolvePublishAssemblies.cs
109 lines (88 loc) · 4.19 KB
/
ResolvePublishAssemblies.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
// 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.Collections.Generic;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.Globalization;
using NuGet.ProjectModel;
using NuGet.Packaging.Core;
namespace Microsoft.NET.Build.Tasks
{
/// <summary>
/// Resolves the assemblies to be published for a .NET app.
/// </summary>
public class ResolvePublishAssemblies : TaskBase
{
private readonly List<ITaskItem> _assembliesToPublish = new List<ITaskItem>();
private readonly List<ITaskItem> _packagesResolved = new List<ITaskItem>();
[Required]
public string ProjectPath { get; set; }
[Required]
public string AssetsFilePath { get; set; }
[Required]
public string TargetFramework { get; set; }
public string RuntimeIdentifier { get; set; }
public string PlatformLibraryName { get; set; }
public ITaskItem[] PrivateAssetsPackageReferences { get; set; }
public bool PreserveCacheLayout { get; set; }
public string[] FilterProjectFiles { get; set; }
/// <summary>
/// All the assemblies to publish.
/// </summary>
[Output]
public ITaskItem[] AssembliesToPublish
{
get { return _assembliesToPublish.ToArray(); }
}
[Output]
public ITaskItem[] PackagesResolved
{
get { return _packagesResolved.ToArray(); }
}
protected override void ExecuteCore()
{
var lockFileCache = new LockFileCache(BuildEngine4);
LockFile lockFile = lockFileCache.GetLockFile(AssetsFilePath);
IEnumerable<string> privateAssetsPackageIds = PackageReferenceConverter.GetPackageIds(PrivateAssetsPackageReferences);
IPackageResolver packageResolver = NuGetPackageResolver.CreateResolver(lockFile, ProjectPath);
HashSet<PackageIdentity> packagestoBeFiltered = null;
if (FilterProjectFiles != null && FilterProjectFiles.Length > 0)
{
packagestoBeFiltered = new HashSet<PackageIdentity>();
foreach (var filterProjectFile in FilterProjectFiles)
{
Log.LogMessage(MessageImportance.Low, string.Format(CultureInfo.CurrentCulture, Strings.ParsingFiles, filterProjectFile));
var packagesSpecified = CacheArtifactParser.Parse(filterProjectFile);
foreach (var pkg in packagesSpecified)
{
Log.LogMessage(MessageImportance.Low, string.Format(CultureInfo.CurrentCulture, Strings.PackageInfoLog, pkg.Id, pkg.Version));
}
packagestoBeFiltered.UnionWith(packagesSpecified);
}
}
ProjectContext projectContext = lockFile.CreateProjectContext(
NuGetUtils.ParseFrameworkName(TargetFramework),
RuntimeIdentifier,
PlatformLibraryName);
projectContext.PackagesToBeFiltered = packagestoBeFiltered;
var assemblyResolver =
new PublishAssembliesResolver(packageResolver)
.WithPrivateAssets(privateAssetsPackageIds)
.WithPreserveCacheLayout(PreserveCacheLayout);
IEnumerable<ResolvedFile> resolvedAssemblies = assemblyResolver.Resolve(projectContext);
foreach (ResolvedFile resolvedAssembly in resolvedAssemblies)
{
TaskItem item = new TaskItem(resolvedAssembly.SourcePath);
item.SetMetadata("DestinationSubPath", resolvedAssembly.DestinationSubPath);
item.SetMetadata("AssetType", resolvedAssembly.Asset.ToString().ToLower());
_assembliesToPublish.Add(item);
}
foreach (var resolvedPkg in assemblyResolver.GetResolvedPackages())
{
TaskItem item = new TaskItem(resolvedPkg.Id);
item.SetMetadata("Version", resolvedPkg.Version.ToString());
_packagesResolved.Add(item);
}
}
}
}