This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
PackageDependencyProvider.cs
200 lines (167 loc) · 7.57 KB
/
PackageDependencyProvider.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// 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;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using Microsoft.DotNet.ProjectModel.Graph;
using NuGet.Frameworks;
using NuGet.Packaging;
namespace Microsoft.DotNet.ProjectModel.Resolution
{
public class PackageDependencyProvider
{
private readonly string _packagesPath;
private readonly VersionFolderPathResolver _packagePathResolver;
private readonly FrameworkReferenceResolver _frameworkReferenceResolver;
public PackageDependencyProvider(string packagesPath, FrameworkReferenceResolver frameworkReferenceResolver)
{
_packagesPath = packagesPath;
_packagePathResolver = new VersionFolderPathResolver(packagesPath);
_frameworkReferenceResolver = frameworkReferenceResolver;
}
public PackageDescription GetDescription(NuGetFramework targetFramework, LockFilePackageLibrary package, LockFileTargetLibrary targetLibrary)
{
// If a NuGet dependency is supposed to provide assemblies but there is no assembly compatible with
// current target framework, we should mark this dependency as unresolved
var containsAssembly = package.Files
.Any(x => x.StartsWith($"ref{Path.DirectorySeparatorChar}") ||
x.StartsWith($"lib{Path.DirectorySeparatorChar}"));
var compatible = targetLibrary.FrameworkAssemblies.Any() ||
targetLibrary.CompileTimeAssemblies.Any() ||
targetLibrary.RuntimeAssemblies.Any() ||
!containsAssembly;
var dependencies = new List<LibraryRange>(targetLibrary.Dependencies.Count + targetLibrary.FrameworkAssemblies.Count);
PopulateDependencies(dependencies, targetLibrary, targetFramework);
string path;
if (package.Path != null)
{
path = Path.Combine(_packagesPath, package.Path);
}
else
{
path = _packagePathResolver.GetInstallPath(package.Name, package.Version);
}
var exists = Directory.Exists(path);
if (exists)
{
// If the package's compile time assemblies is for a portable profile then, read the assembly metadata
// and turn System.* references into reference assembly dependencies
PopulateLegacyPortableDependencies(targetFramework, dependencies, path, targetLibrary);
}
var packageDescription = new PackageDescription(
path,
package,
targetLibrary,
dependencies,
compatible,
resolved: compatible && exists);
return packageDescription;
}
private void PopulateLegacyPortableDependencies(NuGetFramework targetFramework, List<LibraryRange> dependencies, string packagePath, LockFileTargetLibrary targetLibrary)
{
var seen = new HashSet<string>();
foreach (var assembly in targetLibrary.CompileTimeAssemblies)
{
if (IsPlaceholderFile(assembly))
{
continue;
}
// (ref/lib)/{tfm}/{assembly}
var pathParts = assembly.Path.Split(Path.DirectorySeparatorChar);
if (pathParts.Length != 3)
{
continue;
}
var assemblyTargetFramework = NuGetFramework.Parse(pathParts[1]);
if (!assemblyTargetFramework.IsPCL)
{
continue;
}
var assemblyPath = Path.Combine(packagePath, assembly.Path);
foreach (var dependency in GetDependencies(assemblyPath))
{
if (seen.Add(dependency))
{
string path;
Version version;
// If there exists a reference assembly on the requested framework with the same name then turn this into a
// framework assembly dependency
if (_frameworkReferenceResolver.TryGetAssembly(dependency, targetFramework, out path, out version))
{
dependencies.Add(new LibraryRange(dependency,
LibraryType.ReferenceAssembly,
LibraryDependencyType.Build));
}
}
}
}
}
private static IEnumerable<string> GetDependencies(string path)
{
using (var peReader = new PEReader(File.OpenRead(path)))
{
var metadataReader = peReader.GetMetadataReader();
foreach (var assemblyReferenceHandle in metadataReader.AssemblyReferences)
{
var assemblyReference = metadataReader.GetAssemblyReference(assemblyReferenceHandle);
yield return metadataReader.GetString(assemblyReference.Name);
}
}
}
private void PopulateDependencies(
List<LibraryRange> dependencies,
LockFileTargetLibrary targetLibrary,
NuGetFramework targetFramework)
{
foreach (var dependency in targetLibrary.Dependencies)
{
dependencies.Add(new LibraryRange(
dependency.Id,
dependency.VersionRange,
LibraryType.Unspecified,
LibraryDependencyType.Default));
}
if (!targetFramework.IsPackageBased)
{
// Only add framework assemblies for non-package based frameworks.
foreach (var frameworkAssembly in targetLibrary.FrameworkAssemblies)
{
dependencies.Add(new LibraryRange(
frameworkAssembly,
LibraryType.ReferenceAssembly,
LibraryDependencyType.Default));
}
}
}
public static bool IsPlaceholderFile(string path)
{
return string.Equals(Path.GetFileName(path), "_._", StringComparison.Ordinal);
}
public static string ResolvePackagesPath(string rootDirectory, GlobalSettings settings)
{
// Order
// 1. global.json { "packages": "..." }
// 2. EnvironmentNames.PackagesStore environment variable
// 3. NuGet.config repositoryPath (maybe)?
// 4. {DefaultLocalRuntimeHomeDir}\packages
if (!string.IsNullOrEmpty(settings?.PackagesPath))
{
return Path.Combine(rootDirectory, settings.PackagesPath);
}
var runtimePackages = Environment.GetEnvironmentVariable(EnvironmentNames.PackagesStore);
if (!string.IsNullOrEmpty(runtimePackages))
{
return runtimePackages;
}
var profileDirectory = Environment.GetEnvironmentVariable("USERPROFILE");
if (string.IsNullOrEmpty(profileDirectory))
{
profileDirectory = Environment.GetEnvironmentVariable("HOME");
}
return Path.Combine(profileDirectory, ".nuget", "packages");
}
}
}