forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalPackageRepository.cs
More file actions
379 lines (321 loc) · 16.3 KB
/
Copy pathLocalPackageRepository.cs
File metadata and controls
379 lines (321 loc) · 16.3 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
namespace Microsoft.PackageManagement.NuGetProvider
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.PackageManagement.Provider.Utility;
/// <summary>
/// Package repository for downloading data from file repositories
/// </summary>
internal class LocalPackageRepository : IPackageRepository {
private readonly string _path;
/// <summary>
/// Ctor's
/// </summary>
/// <param name="request"></param>
/// <param name="physicalPath"></param>
public LocalPackageRepository(string physicalPath, NuGetRequest request) {
_path = physicalPath;
}
/// <summary>
/// Package source location
/// </summary>
public string Source {
get {
return _path;
}
}
/// <summary>
/// Finding the packages in the file repository
/// </summary>
/// <param name="openPackage">Delegate function which is actually finding a package</param>
/// <param name="packageId">Package Id</param>
/// <param name="packagePaths">File repository path</param>
/// <param name="request"></param>
/// <returns></returns>
private static IEnumerable<IPackage> GetPackages(Func<string, Request, IPackage> openPackage,
string packageId,
IEnumerable<string> packagePaths,
Request request)
{
request.Debug(Resources.Messages.DebugInfoCallMethod3, "LocalPackageRepository", "GetPackages", packageId);
foreach (var path in packagePaths)
{
IPackage package = null;
try {
package = GetPackage(openPackage, path, request);
} catch (InvalidOperationException ex) {
// ignore error for unzipped packages (nuspec files).
if (!string.Equals(NuGetConstant.ManifestExtension, Path.GetExtension(path), StringComparison.OrdinalIgnoreCase)) {
request.Verbose(ex.Message);
throw;
}
}
if (package != null && package.Id.Equals(packageId, StringComparison.OrdinalIgnoreCase)) {
yield return package;
}
}
}
/// <summary>
/// Finding the package in the file repository
/// </summary>
/// <param name="openPackage">Delegate function which is actually finding a package</param>
/// <param name="path">File repository path</param>
/// <param name="request"></param>
/// <returns></returns>
private static IPackage GetPackage(Func<string, Request, IPackage> openPackage, string path, Request request)
{
//Create the package
IPackage package = openPackage(path, request);
return package;
}
/// <summary>
/// A delegate used for finding the package.
/// </summary>
/// <param name="path">File repository path</param>
/// <param name="request"></param>
/// <returns></returns>
private static IPackage OpenPackage(string path, Request request) {
request.Debug(Resources.Messages.DebugInfoCallMethod3, "LocalPackageRepository", "OpenPackage", path);
if (!File.Exists(path)) {
request.Warning(Resources.Messages.FileNotFound, path,"LocalPackageRepository::OpenPackage");
return null;
}
//deal with .nupkg
if (string.Equals(Path.GetExtension(path), NuGetConstant.PackageExtension, StringComparison.OrdinalIgnoreCase)) {
PackageBase package;
try {
package = ProcessZipPackage(path);
} catch (Exception ex) {
request.Verbose(ex.Message);
throw;
}
// Set the last modified date on the package
package.Published = FileUtility.GetLastModified(path);
// We assume local files in the local repository are all latest version
package.IsAbsoluteLatestVersion = true;
package.IsLatestVersion = true;
package.FullFilePath = path;
return package;
}
return null;
}
/// <summary>
/// Unzip the package and create PackageImpl object
/// </summary>
/// <param name="nupkgPath">The .nupkg file path</param>
/// <returns></returns>
private static PackageBase ProcessZipPackage(string nupkgPath) {
string packageName = Path.GetFileNameWithoutExtension(nupkgPath);
packageName = PackageUtility.GetPackageNameWithoutVersionInfo(packageName);
PackageBase package = PackageUtility.DecompressFile(nupkgPath, packageName);
return package;
}
/// <summary>
/// Get the .nupkg files
/// </summary>
/// <param name="filter">The file filter to be applied while finding the .nupkg files</param>
/// <returns></returns>
private IEnumerable<string> GetPackageFiles(string filter = null) {
filter = filter ?? "*" + NuGetConstant.PackageExtension;
// Check for package files one level deep. We use this at package install time
// to determine the set of installed packages. Installed packages are copied to
// {id}.{version}\{packagefile}.{extension}.
foreach (var dir in FileUtility.GetDirectories(_path))
//foreach (var dir in FileSystem.GetDirectories(String.Empty))
{
foreach (var path in FileUtility.GetFiles(dir, filter, recursive: false)) {
yield return path;
}
}
// Check top level directory
foreach (var path in FileUtility.GetFiles(_path, filter, recursive: false)) {
yield return path;
}
}
/// <summary>
/// Find-Package
/// </summary>
/// <param name="packageId">Package id</param>
/// <param name="version">Package Name</param>
/// <param name="request"></param>
/// <returns></returns>
public virtual IPackage FindPackage(string packageId, SemanticVersion version, NuGetRequest request)
{
return FindPackage(OpenPackage, packageId, version, request);
}
/// <summary>
/// Find-Package
/// </summary>
/// <param name="openPackage">Delegate function which is actually finding a package</param>
/// <param name="packageId">Package Id</param>
/// <param name="version">Package version</param>
/// <param name="nugetRequest"></param>
/// <returns></returns>
private IPackage FindPackage(Func<string, Request, IPackage> openPackage, string packageId, SemanticVersion version, NuGetRequest nugetRequest)
{
if (nugetRequest == null) {
return null;
}
nugetRequest.Debug(Resources.Messages.SearchingRepository, "FindPackage", packageId);
var lookupPackageName = new PackageName(packageId, version);
//handle file cache here if we want to support local package cache later
// Lookup files which start with the name "<Id>." and attempt to match it with all possible version string combinations (e.g. 1.2.0, 1.2.0.0)
// before opening the package. To avoid creating file name strings, we attempt to specifically match everything after the last path separator
// which would be the file name and extension.
return (from path in GetPackageLookupPaths(packageId, version)
let package = GetPackage(openPackage, path, nugetRequest)
where lookupPackageName.Equals(new PackageName(package.Id, package.Version))
select package).FirstOrDefault();
}
/// <summary>
/// Find the package files (.nupkg or .nuspec).
/// </summary>
/// <param name="packageId"></param>
/// <param name="version"></param>
/// <returns></returns>
private IEnumerable<string> GetPackageLookupPaths(string packageId, SemanticVersion version) {
// Files created by the path resolver. This would take into account the non-side-by-side scenario
// and we do not need to match this for id and version.
//var packageFileName = packageId + version + Constant.PackageExtension;
var packageFileName = FileUtility.MakePackageFileName(false, packageId, version.ToString(), NuGetConstant.PackageExtension);
// var packageFileName = PathResolver.GetPackageFileName(packageId, version);
var manifestFileName = Path.ChangeExtension(packageFileName, NuGetConstant.ManifestExtension);
var filesMatchingFullName = GetPackageFiles(packageFileName).Concat(GetPackageFiles(manifestFileName));
if (version.Version.Revision < 1) {
// If the build or revision number is not set, we need to look for combinations of the format
// * Foo.1.2.nupkg
// * Foo.1.2.3.nupkg
// * Foo.1.2.0.nupkg
// * Foo.1.2.0.0.nupkg
// To achieve this, we would look for files named 1.2*.nupkg if both build and revision are 0 and
// 1.2.3*.nupkg if only the revision is set to 0.
string partialName = version.Version.Build < 1 ?
String.Join(".", packageId, version.Version.Major, version.Version.Minor) :
String.Join(".", packageId, version.Version.Major, version.Version.Minor, version.Version.Build);
string partialManifestName = partialName + "*" + NuGetConstant.ManifestExtension;
partialName += "*" + NuGetConstant.PackageExtension;
// Partial names would result is gathering package with matching major and minor but different build and revision.
// Attempt to match the version in the path to the version we're interested in.
var partialNameMatches = GetPackageFiles(partialName).Where(path => FileNameMatchesPattern(packageId, version, path));
var partialManifestNameMatches = GetPackageFiles(partialManifestName).Where(
path => FileNameMatchesPattern(packageId, version, path));
filesMatchingFullName = filesMatchingFullName.Concat(partialNameMatches).Concat(partialManifestNameMatches);
}
// cannot find matching files, we should try to search for just packageid.nupkg
if (filesMatchingFullName.Count() == 0)
{
// exclude version
var packageWithoutVersionName = FileUtility.MakePackageFileName(true, packageId, null, NuGetConstant.PackageExtension);
var packageWithoutVersionManifest = Path.ChangeExtension(packageWithoutVersionName, NuGetConstant.ManifestExtension);
return GetPackageFiles(packageWithoutVersionName).Concat(GetPackageFiles(packageWithoutVersionManifest));
}
return filesMatchingFullName;
}
/// <summary>
/// True if the the file contains the right id and version.
/// </summary>
/// <param name="packageId">package id</param>
/// <param name="version">package version</param>
/// <param name="path">File path</param>
/// <returns></returns>
private static bool FileNameMatchesPattern(string packageId, SemanticVersion version, string path)
{
var name = Path.GetFileNameWithoutExtension(path);
SemanticVersion parsedVersion;
if (string.IsNullOrWhiteSpace(name))
{
return false;
}
// When matching by pattern, we will always have a version token. Packages without versions would be matched early on by the version-less path resolver
// when doing an exact match.
return name.Length > packageId.Length &&
SemanticVersion.TryParse(name.Substring(packageId.Length + 1), out parsedVersion) &&
parsedVersion == version;
}
/// <summary>
/// Find-Package based the given Id
/// </summary>
/// <param name="packageId">Package Id</param>
/// <param name="request"></param>
/// <returns></returns>
public IEnumerable<IPackage> FindPackagesById(string packageId, NuGetRequest request)
{
return FindPackagesById(OpenPackage, packageId, request);
}
/// <summary>
/// Find-Package based the given Id
/// </summary>
/// <param name="openPackage">Delegate function which is actually finding a package</param>
/// <param name="packageId">Package version</param>
/// <param name="request"></param>
/// <returns></returns>
private IEnumerable<IPackage> FindPackagesById(Func<string, Request, IPackage> openPackage, string packageId, Request request)
{
request.Debug(Resources.Messages.DebugInfoCallMethod3, "LocalPackageRepository", "FindPackagesById", packageId);
// get packages in .nupkg or .nuspec files
// add "*" to avoid parsing the version (id: packageName+Version, e.g. jQuery.1.10.0)
return GetPackages(
openPackage,
packageId,
GetPackageFiles(packageId + "*" + NuGetConstant.PackageExtension),
request).Union(
GetPackages(
openPackage,
packageId,
GetPackageFiles(packageId + "*" + NuGetConstant.ManifestExtension),
request));
}
/// <summary>
/// True if the packagesource is a file repository
/// </summary>
public bool IsFile {
get {
//true because this is not a local file repository
return true;
}
}
/// <summary>
/// Search the entire repository for the case when a user does not provider package name or uses wildcards in the name.
/// </summary>
/// <param name="searchTerm">The Searchterm</param>
/// <param name="nugetRequest"></param>
/// <returns></returns>
public IEnumerable<IPackage> Search(string searchTerm, NuGetRequest nugetRequest)
{
var packages = SearchImpl(searchTerm, nugetRequest);
if (packages == null) {
return Enumerable.Empty<IPackage>();
}
if (nugetRequest != null && nugetRequest.AllVersions.Value) {
//return whatever we can find
return packages;
}
//return the latest version
return packages.GroupBy(p => p.Id).Select(each => each.OrderByDescending(pp => pp.Version).FirstOrDefault());
}
private IEnumerable<IPackage> SearchImpl(string searchTerm, NuGetRequest nugetRequest)
{
if (nugetRequest == null) {
yield break;
}
nugetRequest.Debug(Resources.Messages.SearchingRepository, "LocalPackageRepository", Source);
var files = Directory.GetFiles(Source);
foreach (var package in nugetRequest.FilterOnTags(files.Select(nugetRequest.GetPackageByFilePath).Where(pkgItem => pkgItem != null).Select(pkg => pkg.Package)))
{
yield return package;
}
// look in the package source location for directories that contain nupkg files.
var subdirs = Directory.EnumerateDirectories(Source, "*", SearchOption.AllDirectories);
foreach (var subdir in subdirs) {
var nupkgs = Directory.EnumerateFileSystemEntries(subdir, "*.nupkg", SearchOption.TopDirectoryOnly);
foreach (var package in nugetRequest.FilterOnTags(nupkgs.Select(nugetRequest.GetPackageByFilePath).Where(pkgItem => pkgItem != null).Select(pkg => pkg.Package)))
{
yield return package;
}
}
}
}
}