forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNuGetPackageResolver.cs
63 lines (56 loc) · 2.46 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
// 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 class NuGetPackageResolver : IPackageResolver
{
private readonly FallbackPackagePathResolver _packagePathResolver;
public NuGetPackageResolver(INuGetPathContext pathContext)
{
_packagePathResolver = new FallbackPackagePathResolver(pathContext);
}
public NuGetPackageResolver(string userPackageFolder, IEnumerable<string> fallbackPackageFolders)
{
_packagePathResolver = new FallbackPackagePathResolver(userPackageFolder, fallbackPackageFolders);
}
public string GetPackageDirectory(string packageId, NuGetVersion version)
{
string packageRoot = null;
return GetPackageDirectory(packageId, version, out packageRoot);
}
public string GetPackageDirectory(string packageId, NuGetVersion version, out string packageRoot)
{
packageRoot = null;
var pkginfo = _packagePathResolver.GetPackageInfo(packageId,version);
if (pkginfo != null)
{
packageRoot = pkginfo.PathResolver.GetVersionListPath(""); //TODO Remove Once Nuget is updated to use FallbackPackagePathInfo.PathResolver.RootPath
}
return _packagePathResolver.GetPackageDirectory(packageId, version);
}
public static NuGetPackageResolver CreateResolver(LockFile lockFile, string projectPath)
{
NuGetPackageResolver packageResolver;
string userPackageFolder = lockFile.PackageFolders.FirstOrDefault()?.Path;
if (userPackageFolder != null)
{
var fallBackFolders = lockFile.PackageFolders.Skip(1).Select(f => f.Path);
packageResolver = new NuGetPackageResolver(userPackageFolder, fallBackFolders);
}
else
{
NuGetPathContext nugetPathContext = NuGetPathContext.Create(Path.GetDirectoryName(projectPath));
packageResolver = new NuGetPackageResolver(nugetPathContext);
}
return packageResolver;
}
}
}