-
Notifications
You must be signed in to change notification settings - Fork 694
/
VersionFolderPathResolver.cs
211 lines (189 loc) · 7.56 KB
/
VersionFolderPathResolver.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
201
202
203
204
205
206
207
208
209
210
211
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using NuGet.Packaging.Core;
using NuGet.Versioning;
namespace NuGet.Packaging
{
/// <summary>
/// A V3 path resolver.
/// </summary>
public class VersionFolderPathResolver
{
/// <summary>
/// Gets the packages directory root folder.
/// </summary>
public string RootPath { get; }
/// <summary>
/// Gets a flag indicating whether or not package ID's and versions are made lowercase.
/// </summary>
public bool IsLowerCase { get; }
/// <summary>
/// Initializes a new <see cref="VersionFolderPathResolver" /> class.
/// </summary>
/// <param name="rootPath">The packages directory root folder.</param>
public VersionFolderPathResolver(string rootPath) : this(rootPath, isLowercase: true)
{
}
/// <summary>
/// Initializes a new <see cref="VersionFolderPathResolver" /> class.
/// </summary>
/// <param name="rootPath">The packages directory root folder.</param>
/// <param name="isLowercase"><c>true</c> if package ID's and versions are made lowercase;
/// otherwise <c>false</c>.</param>
public VersionFolderPathResolver(string rootPath, bool isLowercase)
{
RootPath = rootPath;
IsLowerCase = isLowercase;
}
/// <summary>
/// Gets the package install path.
/// </summary>
/// <param name="packageId">The package ID.</param>
/// <param name="version">The package version.</param>
/// <returns>The package install path.</returns>
public virtual string GetInstallPath(string packageId, NuGetVersion version)
{
return Path.Combine(
RootPath,
GetPackageDirectory(packageId, version));
}
/// <summary>
/// Gets the package version list path.
/// </summary>
/// <param name="packageId">The package ID.</param>
/// <returns>The package version list path.</returns>
public string GetVersionListPath(string packageId)
{
return Path.Combine(
RootPath,
GetVersionListDirectory(packageId));
}
/// <summary>
/// Gets the package file path.
/// </summary>
/// <param name="packageId">The package ID.</param>
/// <param name="version">The package version.</param>
/// <returns>The package file path.</returns>
public string GetPackageFilePath(string packageId, NuGetVersion version)
{
return Path.Combine(
GetInstallPath(packageId, version),
GetPackageFileName(packageId, version));
}
/// <summary>
/// Gets the manifest file path.
/// </summary>
/// <param name="packageId">The package ID.</param>
/// <param name="version">The package version.</param>
/// <returns>The manifest file path.</returns>
public string GetManifestFilePath(string packageId, NuGetVersion version)
{
packageId = Normalize(packageId);
return Path.Combine(
GetInstallPath(packageId, version),
GetManifestFileName(packageId, version));
}
/// <summary>
/// Gets the hash file path.
/// </summary>
/// <param name="packageId">The package ID.</param>
/// <param name="version">The package version.</param>
/// <returns>The hash file path.</returns>
public string GetHashPath(string packageId, NuGetVersion version)
{
return Path.Combine(
GetInstallPath(packageId, version),
GetHashFileName(packageId, version));
}
/// <summary>
/// Gets the hash file name.
/// </summary>
/// <param name="packageId">The package ID.</param>
/// <param name="version">The package version.</param>
/// <returns>The hash file name.</returns>
public string GetHashFileName(string packageId, NuGetVersion version)
{
return $"{Normalize(packageId)}.{Normalize(version)}{PackagingCoreConstants.HashFileExtension}";
}
/// <summary>
/// Gets the new hash file path which represents the original hash of the package.
/// </summary>
/// <param name="packageId">The package ID.</param>
/// <param name="version">The package version.</param>
/// <returns>The hash file path.</returns>
public string GetNupkgMetadataPath(string packageId, NuGetVersion version)
{
return Path.Combine(
GetInstallPath(packageId, version),
PackagingCoreConstants.NupkgMetadataFileExtension);
}
/// <summary>
/// Gets the version list directory.
/// </summary>
/// <param name="packageId">The package ID.</param>
/// <returns>The version list directory.</returns>
public virtual string GetVersionListDirectory(string packageId)
{
return Normalize(packageId);
}
/// <summary>
/// Gets the package directory.
/// </summary>
/// <param name="packageId">The package ID.</param>
/// <param name="version">The package version.</param>
/// <returns>The package directory.</returns>
public virtual string GetPackageDirectory(string packageId, NuGetVersion version)
{
return Path.Combine(
GetVersionListDirectory(packageId),
Normalize(version));
}
/// <summary>
/// Gets the package file name.
/// </summary>
/// <param name="packageId">The package ID.</param>
/// <param name="version">The package version.</param>
/// <returns>The package file name.</returns>
public virtual string GetPackageFileName(string packageId, NuGetVersion version)
{
return $"{Normalize(packageId)}.{Normalize(version)}{PackagingCoreConstants.NupkgExtension}";
}
/// <summary>
/// Gets the package download marker file name.
/// </summary>
/// <param name="packageId">The package ID.</param>
/// <returns>The package download marker file name.</returns>
public string GetPackageDownloadMarkerFileName(string packageId)
{
return $"{Normalize(packageId)}{PackagingCoreConstants.PackageDownloadMarkerFileExtension}";
}
/// <summary>
/// Gets the manifest file name.
/// </summary>
/// <param name="packageId">The package ID.</param>
/// <param name="version">The package version.</param>
/// <returns>The manifest file name.</returns>
public virtual string GetManifestFileName(string packageId, NuGetVersion version)
{
return $"{Normalize(packageId)}{PackagingCoreConstants.NuspecExtension}";
}
private string Normalize(NuGetVersion version)
{
var versionString = version.ToNormalizedString();
if (IsLowerCase)
{
versionString = versionString.ToLowerInvariant();
}
return versionString;
}
private string Normalize(string packageId)
{
if (IsLowerCase)
{
packageId = packageId.ToLowerInvariant();
}
return packageId;
}
}
}