private async Task IndexPackageAsync( PackageDetailsCatalogLeaf catalogLeaf, CancellationToken cancellationToken) { var packageId = catalogLeaf.PackageId; var packageVersion = catalogLeaf.ParsePackageVersion(); Package package; Stream packageStream = null; Stream readmeStream = null; try { var contentClient = _clientFactory.CreatePackageContentClient(); using (var stream = await contentClient.GetPackageContentStreamOrNullAsync(packageId, packageVersion, cancellationToken)) { packageStream = await stream.AsTemporaryFileStreamAsync(cancellationToken); } _logger.LogInformation( "Downloaded package {PackageId} {PackageVersion}, building metadata...", packageId, packageVersion); using (var reader = new PackageArchiveReader(packageStream, leaveStreamOpen: true)) { package = reader.GetPackageMetadata(); package.Listed = catalogLeaf.IsListed(); package.Published = catalogLeaf.Published.UtcDateTime; if (package.HasReadme) { using (var stream = await reader.GetReadmeAsync(cancellationToken)) { readmeStream = await stream.AsTemporaryFileStreamAsync(cancellationToken); } } await IndexPackageAsync(package, readmeStream, cancellationToken); } } catch (Exception e) { _logger.LogError( e, "Failed to process package {PackageId} {PackageVersion}", packageId, packageVersion); throw; } finally { packageStream?.Dispose(); readmeStream?.Dispose(); } }
private string VerifyConsistencyAndNormalizeVersion( Context context, PackageDetailsCatalogLeaf leaf) { if (!StringComparer.OrdinalIgnoreCase.Equals(context.PackageId, leaf.PackageId)) { const string message = "The package ID found in the catalog package does not match the catalog leaf."; _logger.LogError( message + " Page ID: {PagePackageId}, leaf ID: {LeafPackageId}, leaf URL: {Url}", context.PackageId, leaf.PackageId, leaf.Url); throw new InvalidOperationException(message); } var parsedPackageVersion = leaf.ParsePackageVersion(); var normalizedVersion = parsedPackageVersion.ToNormalizedString(); if (leaf.VerbatimVersion != null) { var parsedVerbatimVersion = NuGetVersion.Parse(leaf.VerbatimVersion); if (normalizedVersion != parsedVerbatimVersion.ToNormalizedString()) { const string message = "The normalized versions from the package version and the verbatim version do not match."; _logger.LogError( message + " ID: {PackageId}, version: {PackageVersion}, verbatim: {VerbatimVersion}, leaf URL: {Url}", leaf.PackageId, leaf.PackageVersion, leaf.VerbatimVersion, leaf.Url); throw new InvalidOperationException(message); } } if (parsedPackageVersion.IsPrerelease != leaf.IsPrerelease) { var message = $"The {nameof(PackageDetailsCatalogLeaf.IsPrerelease)} from the leaf does not match the version. " + $"Using the value from the parsed version. "; _logger.LogWarning( message + " ID: {PackageId}, version: {PackageVersion}, leaf is prerelease: {LeafIsPrerelease}, " + "parsed is prerelease: {ParsedIsPrerelease}, leaf URL: {Url}", leaf.PackageId, leaf.PackageVersion, leaf.IsPrerelease, parsedPackageVersion.IsPrerelease, leaf.Url); leaf.IsPrerelease = parsedPackageVersion.IsPrerelease; } return(normalizedVersion); }
public void UpdateLeafItem(RegistrationLeafItem leafItem, HiveType hive, string id, PackageDetailsCatalogLeaf packageDetails) { var parsedVersion = packageDetails.ParsePackageVersion(); leafItem.Url = _urlBuilder.GetLeafUrl(hive, id, parsedVersion); leafItem.Type = JsonLdConstants.RegistrationLeafItemType; leafItem.PackageContent = GetPackageContentUrl(id, packageDetails); leafItem.Registration = _urlBuilder.GetIndexUrl(hive, id); if (leafItem.CatalogEntry == null) { leafItem.CatalogEntry = new Protocol.Registration.RegistrationCatalogEntry(); } UpdateCatalogEntry(hive, id, leafItem.CatalogEntry, packageDetails, parsedVersion); }
/// <summary> /// Determines if the provied package details leaf represents a SemVer 2.0.0 package. A package is considered /// SemVer 2.0.0 if it's version is SemVer 2.0.0 or one of its dependency version ranges is SemVer 2.0.0. /// </summary> /// <param name="leaf">The catalog leaf.</param> /// <returns>True if the package is SemVer 2.0.0.</returns> public static bool IsSemVer2(this PackageDetailsCatalogLeaf leaf) { var parsedPackageVersion = leaf.ParsePackageVersion(); if (parsedPackageVersion.IsSemVer2) { return(true); } if (leaf.VerbatimVersion != null) { var parsedVerbatimVersion = NuGetVersion.Parse(leaf.VerbatimVersion); if (parsedVerbatimVersion.IsSemVer2) { return(true); } } if (leaf.DependencyGroups != null) { foreach (var dependencyGroup in leaf.DependencyGroups) { // Example: https://api.nuget.org/v3/catalog0/data/2018.10.28.07.42.42/mvcsitemapprovider.3.3.0-pre1.json if (dependencyGroup.Dependencies == null) { continue; } foreach (var dependency in dependencyGroup.Dependencies) { var versionRange = dependency.ParseRange(); if ((versionRange.MaxVersion != null && versionRange.MaxVersion.IsSemVer2) || (versionRange.MinVersion != null && versionRange.MinVersion.IsSemVer2)) { return(true); } } } } return(false); }