|
29 | 29 | using NuGet.Common; |
30 | 30 | using NuGet.Configuration; |
31 | 31 | using NuGet.Packaging; |
| 32 | +using NuGet.Packaging.Core; |
32 | 33 | using NuGet.Packaging.Licenses; |
33 | 34 | using NuGet.Protocol; |
34 | 35 | using NuGet.Protocol.Core.Types; |
@@ -77,25 +78,28 @@ bool disableHashComputation |
77 | 78 | } |
78 | 79 |
|
79 | 80 | internal string GetCachedNuspecFilename(string name, string version) |
| 81 | + { |
| 82 | + return GetCachedNupkgFilename(name, version, name.ToLowerInvariant() + _nuspecExtension); |
| 83 | + } |
| 84 | + |
| 85 | + internal string GetCachedNupkgFilename(string name, string version, string fileName) |
80 | 86 | { |
81 | 87 | if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(version)) { return null; } |
82 | 88 |
|
83 | 89 | var lowerName = name.ToLowerInvariant(); |
84 | 90 | var lowerVersion = version.ToLowerInvariant(); |
85 | | - string nuspecFilename = null; |
86 | 91 |
|
87 | 92 | foreach (var packageCachePath in _packageCachePaths) |
88 | 93 | { |
89 | 94 | var currentDirectory = _fileSystem.Path.Combine(packageCachePath, lowerName, NormalizeVersion(lowerVersion)); |
90 | | - var currentFilename = _fileSystem.Path.Combine(currentDirectory, lowerName + _nuspecExtension); |
| 95 | + var currentFilename = _fileSystem.Path.Combine(currentDirectory, fileName); |
91 | 96 | if (_fileSystem.File.Exists(currentFilename)) |
92 | 97 | { |
93 | | - nuspecFilename = currentFilename; |
94 | | - break; |
| 98 | + return currentFilename; |
95 | 99 | } |
96 | 100 | } |
97 | 101 |
|
98 | | - return nuspecFilename; |
| 102 | + return null; |
99 | 103 | } |
100 | 104 |
|
101 | 105 | /// <summary> |
@@ -272,9 +276,19 @@ public async Task<Component> GetComponentAsync(string name, string version, Comp |
272 | 276 | } |
273 | 277 | else if (_githubService == null) |
274 | 278 | { |
275 | | - var licenseUrl = nuspecModel.nuspecReader.GetLicenseUrl(); |
276 | | - var license = new License { Name = "Unknown - See URL", Url = licenseUrl?.Trim() }; |
277 | | - component.Licenses = new List<LicenseChoice> { new LicenseChoice { License = license } }; |
| 279 | + License license = await TryGetLicenseFileAsync(licenseMetadata, name, version).ConfigureAwait(false); |
| 280 | + |
| 281 | + if (license == null) |
| 282 | + { |
| 283 | + var licenseUrl = nuspecModel.nuspecReader.GetLicenseUrl(); |
| 284 | + license = new License { Name = "Unknown - See URL", Url = licenseUrl?.Trim() }; |
| 285 | + } |
| 286 | + |
| 287 | + if (license != null) |
| 288 | + { |
| 289 | + component.Licenses ??= new List<LicenseChoice>(); |
| 290 | + component.Licenses.Add(new LicenseChoice { License = license }); |
| 291 | + } |
278 | 292 | } |
279 | 293 | else |
280 | 294 | { |
@@ -310,6 +324,11 @@ public async Task<Component> GetComponentAsync(string name, string version, Comp |
310 | 324 | } |
311 | 325 | } |
312 | 326 |
|
| 327 | + if (license == null) |
| 328 | + { |
| 329 | + license = await TryGetLicenseFileAsync(licenseMetadata, name, version).ConfigureAwait(false); |
| 330 | + } |
| 331 | + |
313 | 332 | if (license != null) |
314 | 333 | { |
315 | 334 | component.Licenses = new List<LicenseChoice> { new LicenseChoice { License = license } }; |
@@ -350,6 +369,48 @@ public async Task<Component> GetComponentAsync(string name, string version, Comp |
350 | 369 | return component; |
351 | 370 | } |
352 | 371 |
|
| 372 | + private async Task<License> TryGetLicenseFileAsync(LicenseMetadata licenseMetadata, string name, string version) |
| 373 | + { |
| 374 | + if (licenseMetadata == null || licenseMetadata.Type != LicenseType.File || string.IsNullOrEmpty(licenseMetadata.License)) |
| 375 | + { |
| 376 | + return null; |
| 377 | + } |
| 378 | + |
| 379 | + string licensePath = GetCachedNupkgFilename(name, version, licenseMetadata.License); |
| 380 | + |
| 381 | + if (licensePath == null) |
| 382 | + { |
| 383 | + return null; |
| 384 | + } |
| 385 | + |
| 386 | + if (!_fileSystem.File.Exists(licensePath)) |
| 387 | + { |
| 388 | + return null; |
| 389 | + } |
| 390 | + |
| 391 | + string extension = _fileSystem.Path.GetExtension(licensePath)?.ToLowerInvariant(); |
| 392 | + |
| 393 | + string contentType = extension switch |
| 394 | + { |
| 395 | + ".md" => "text/markdown", |
| 396 | + ".txt" or "" or null => "text/plain", |
| 397 | + _ => "appliation/octet-stream", |
| 398 | + }; |
| 399 | + |
| 400 | + byte[] licenseContent = await _fileSystem.File.ReadAllBytesAsync(licensePath).ConfigureAwait(false); |
| 401 | + |
| 402 | + return new License |
| 403 | + { |
| 404 | + Name = $"{name} License", |
| 405 | + Text = new AttachedText |
| 406 | + { |
| 407 | + ContentType = contentType, |
| 408 | + Encoding = "base64", |
| 409 | + Content = Convert.ToBase64String(licenseContent), |
| 410 | + } |
| 411 | + }; |
| 412 | + } |
| 413 | + |
353 | 414 | private static Component SetupComponentProperties(Component component, NuspecModel nuspecModel) |
354 | 415 | { |
355 | 416 | component.Authors = new List<OrganizationalContact> { new OrganizationalContact { Name = nuspecModel.nuspecReader.GetAuthors() } }; |
|
0 commit comments