forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetPackageDirectory.cs
72 lines (57 loc) · 2.7 KB
/
GetPackageDirectory.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
// 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.Versioning;
namespace Microsoft.NET.Build.Tasks
{
// Locates the root NuGet package directory for each of the input items that has PackageName and PackageVersion,
// but not PackageDirectory metadata specified
public class GetPackageDirectory : TaskBase
{
public ITaskItem[] Items { get; set; } = Array.Empty<ITaskItem>();
public string[] PackageFolders { get; set; } = Array.Empty<string>();
public string AssetsFileWithAdditionalPackageFolders { get; set; }
[Output]
public ITaskItem[] Output { get; set; }
protected override void ExecuteCore()
{
if (Items.Length == 0 || (PackageFolders.Length == 0 && string.IsNullOrEmpty(AssetsFileWithAdditionalPackageFolders)))
{
Output = Items;
return;
}
if (!string.IsNullOrEmpty(AssetsFileWithAdditionalPackageFolders))
{
var lockFileCache = new LockFileCache(this);
var lockFile = lockFileCache.GetLockFile(AssetsFileWithAdditionalPackageFolders);
PackageFolders = PackageFolders.Concat(lockFile.PackageFolders.Select(p => p.Path)).ToArray();
}
var packageResolver = NuGetPackageResolver.CreateResolver(PackageFolders);
int index = 0;
var updatedItems = new ITaskItem[Items.Length];
foreach (var item in Items)
{
string packageName = item.GetMetadata(MetadataKeys.NuGetPackageId);
string packageVersion = item.GetMetadata(MetadataKeys.NuGetPackageVersion);
if (string.IsNullOrEmpty(packageName) || string.IsNullOrEmpty(packageVersion)
|| !string.IsNullOrEmpty(item.GetMetadata(MetadataKeys.PackageDirectory)))
{
updatedItems[index++] = item;
continue;
}
var parsedPackageVersion = NuGetVersion.Parse(packageVersion);
string packageDirectory = packageResolver.GetPackageDirectory(packageName, parsedPackageVersion);
if (packageDirectory == null)
{
updatedItems[index++] = item;
continue;
}
var newItem = new TaskItem(item);
newItem.SetMetadata(MetadataKeys.PackageDirectory, packageDirectory);
updatedItems[index++] = newItem;
}
Output = updatedItems;
}
}
}