forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResolveCopyLocalAssets.cs
96 lines (74 loc) · 3.85 KB
/
ResolveCopyLocalAssets.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
// 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.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using NuGet.Packaging.Core;
using NuGet.ProjectModel;
using NuGet.Versioning;
using System.Collections.Generic;
namespace Microsoft.NET.Build.Tasks
{
/// <summary>
/// Resolves the assets from the package dependencies that should be copied to output/publish directories.
/// </summary>
public class ResolveCopyLocalAssets : TaskBase
{
private readonly List<ITaskItem> _resolvedAssets = new List<ITaskItem>();
public string AssetsFilePath { get; set; }
[Required]
public string TargetFramework { get; set; }
public string RuntimeIdentifier { get; set; }
public string PlatformLibraryName { get; set; }
public ITaskItem[] RuntimeFrameworks { get; set; }
public ITaskItem[] ExcludedPackageReferences { get; set; }
public bool PreserveStoreLayout { get; set; }
public ITaskItem[] RuntimeStorePackages { get; set; }
public bool IsSelfContained { get; set; }
public bool ResolveRuntimeTargets { get; set; }
[Output]
public ITaskItem[] ResolvedAssets => _resolvedAssets.ToArray();
protected override void ExecuteCore()
{
var lockFileCache = new LockFileCache(this);
LockFile lockFile = lockFileCache.GetLockFile(AssetsFilePath);
HashSet<PackageIdentity> packagestoBeFiltered = null;
if (RuntimeStorePackages != null && RuntimeStorePackages.Length > 0)
{
packagestoBeFiltered = new HashSet<PackageIdentity>();
foreach (var package in RuntimeStorePackages)
{
packagestoBeFiltered.Add(ItemUtilities.GetPackageIdentity(package));
}
}
ProjectContext projectContext = lockFile.CreateProjectContext(
TargetFramework,
RuntimeIdentifier,
PlatformLibraryName,
RuntimeFrameworks,
IsSelfContained);
projectContext.PackagesToBeFiltered = packagestoBeFiltered;
var assetsFileResolver =
new AssetsFileResolver(NuGetPackageResolver.CreateResolver(lockFile))
.WithExcludedPackages(PackageReferenceConverter.GetPackageIds(ExcludedPackageReferences))
.WithPreserveStoreLayout(PreserveStoreLayout);
foreach (var resolvedFile in assetsFileResolver.Resolve(projectContext, resolveRuntimeTargets: ResolveRuntimeTargets))
{
TaskItem item = new TaskItem(resolvedFile.SourcePath);
item.SetMetadata(MetadataKeys.DestinationSubPath, resolvedFile.DestinationSubPath);
item.SetMetadata(MetadataKeys.DestinationSubDirectory, resolvedFile.DestinationSubDirectory);
item.SetMetadata(MetadataKeys.AssetType, resolvedFile.Asset.ToString().ToLowerInvariant());
item.SetMetadata(MetadataKeys.NuGetPackageId, resolvedFile.PackageName);
item.SetMetadata(MetadataKeys.NuGetPackageVersion, resolvedFile.PackageVersion.ToLowerInvariant());
if (resolvedFile.Asset == AssetType.Resources)
{
// For resources, the DestinationSubDirectory is set to the locale. Set the Culture
// metadata on the generated item to this value so that the satellite assemblies can
// be filtered by culture.
item.SetMetadata(MetadataKeys.Culture, resolvedFile.DestinationSubDirectory.TrimEnd(Path.DirectorySeparatorChar));
}
_resolvedAssets.Add(item);
}
}
}
}