forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NuGetPackageResolver.cs
78 lines (62 loc) · 2.82 KB
/
NuGetPackageResolver.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
// 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 NuGet.Common;
using NuGet.Configuration;
using NuGet.Packaging;
using NuGet.ProjectModel;
using NuGet.Versioning;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Microsoft.NET.Build.Tasks
{
internal sealed class NuGetPackageResolver : IPackageResolver
{
private readonly FallbackPackagePathResolver _packagePathResolver;
// Used when no package folders are provided, finds no packages.
private static readonly NuGetPackageResolver s_noPackageFolderResolver = new NuGetPackageResolver();
private NuGetPackageResolver()
{
}
private NuGetPackageResolver(string userPackageFolder, IEnumerable<string> fallbackPackageFolders)
{
_packagePathResolver = new FallbackPackagePathResolver(userPackageFolder, fallbackPackageFolders);
}
public string GetPackageDirectory(string packageId, NuGetVersion version)
=> _packagePathResolver?.GetPackageDirectory(packageId, version);
public string GetPackageDirectory(string packageId, NuGetVersion version, out string packageRoot)
{
var packageInfo = _packagePathResolver?.GetPackageInfo(packageId, version);
if (packageInfo == null)
{
packageRoot = null;
return null;
}
packageRoot = packageInfo.PathResolver.RootPath;
return packageInfo.PathResolver.GetInstallPath(packageId, version);
}
public string ResolvePackageAssetPath(LockFileTargetLibrary package, string relativePath)
{
string packagePath = GetPackageDirectory(package.Name, package.Version);
if (packagePath == null)
{
throw new BuildErrorException(
string.Format(Strings.PackageNotFound, package.Name, package.Version));
}
return Path.Combine(packagePath, NormalizeRelativePath(relativePath));
}
public static string NormalizeRelativePath(string relativePath)
=> relativePath.Replace('/', Path.DirectorySeparatorChar);
public static NuGetPackageResolver CreateResolver(LockFile lockFile)
=> CreateResolver(lockFile.PackageFolders.Select(f => f.Path));
public static NuGetPackageResolver CreateResolver(IEnumerable<string> packageFolders)
{
string userPackageFolder = packageFolders.FirstOrDefault();
if (userPackageFolder == null)
{
return s_noPackageFolderResolver;
}
return new NuGetPackageResolver(userPackageFolder, packageFolders.Skip(1));
}
}
}