public PackageEntryInfo GetVersionInfo(PackageEntryInfo packageInfo, RequestWrapper request) { try { request.Debug(Messages.DebugInfoCallMethod, "NuGetFilesFeed3", "GetVersionInfo"); if (packageInfo == null) { throw new ArgumentNullException("packageInfo"); } if (request == null) { throw new ArgumentNullException("request"); } string query = String.Format(CultureInfo.InvariantCulture, Constants.VersionIndexTemplate, this.baseUrl, this.baseUrl.EndsWith("/") ? String.Empty : "/", packageInfo.Id.ToLowerInvariant()); Stream queryResponse = NuGetClient.DownloadDataToStream(query, request, ignoreNullResponse: true); if (queryResponse != null) { dynamic root = DynamicJsonParser.Parse(new StreamReader(queryResponse).ReadToEnd()); if (root.HasProperty("versions")) { foreach (string v in root.versions) { packageInfo.AddVersion(new SemanticVersion(v)); } } else { request.Debug(Messages.VersionIndexDownloadFailed, packageInfo.Id); } } else { request.Debug(Messages.VersionIndexDownloadFailed, packageInfo.Id); } } finally { request.Debug(Messages.DebugInfoReturnCall, "NuGetFilesFeed3", "GetVersionInfo"); } return(packageInfo); }
private IEnumerable <PackageBase> GetPackagesForPackageEntry(dynamic packageEntry, NuGetSearchContext searchContext, RequestWrapper request) { PackageEntryInfo packageEntryInfo = new PackageEntryInfo(packageEntry.id); if (!PackageFilterUtility.IsValidByName(packageEntryInfo, searchContext)) { yield break; } // This will help us take packageEntryInfo.LatestVersion and packageEntryInfo.AbsoluteLatestVersion and get the matching package easily // We're not setting isLatestVersion here so we don't have to deal with "is it latest or absolute latest" Dictionary <SemanticVersion, PackageBase> versionToPackageTable = new Dictionary <SemanticVersion, PackageBase>(); NuGetSearchContext individualPackageSearchContext = new NuGetSearchContext() { PackageInfo = packageEntryInfo, AllVersions = searchContext.AllVersions, AllowPrerelease = searchContext.AllowPrerelease, RequiredVersion = searchContext.RequiredVersion, MinimumVersion = searchContext.MinimumVersion, MaximumVersion = searchContext.MaximumVersion, EnableDeepMetadataBypass = searchContext.EnableDeepMetadataBypass }; bool latestVersionRequired = !searchContext.AllVersions && searchContext.RequiredVersion == null && searchContext.MinimumVersion == null && searchContext.MaximumVersion == null; if (searchContext.EnableDeepMetadataBypass) { if (latestVersionRequired) { // Use the search result page to get the metadata for the latest version SemanticVersion individualPackageVersion = new SemanticVersion(packageEntry.version); packageEntryInfo.AddVersion(individualPackageVersion); PackageBase pb = this.ResourcesCollection.PackageConverter.Make(packageEntry); if (pb != null) { yield return(pb); } } else { // Go to the registration index of this package first. This allows us to bypass "deep" (but required) metadata in certain cases. NuGetPackageFeed3 packageFeed3 = (NuGetPackageFeed3)this.ResourcesCollection.PackagesFeed; NuGetSearchResult result = packageFeed3.Find(individualPackageSearchContext, request); foreach (PackageBase pb in result.Result.Cast <PackageBase>()) { yield return(pb); } } } else { // Either we want a specific version or we want all metadata for any packages foreach (dynamic packageVersionEntry in packageEntry.versions) { if (packageEntry.version.Equals(packageVersionEntry.version) || searchContext.AllVersions) { if (packageVersionEntry.Metadata.HasProperty("id")) { // Collect all versions from the search results so we can manually set isLatestVersion and isAbsoluteLatestVersion later SemanticVersion individualPackageVersion = new SemanticVersion(packageVersionEntry.version); packageEntryInfo.AddVersion(individualPackageVersion); // Skip prerelease versions if AllowPrereleaseVersions is not specified if (!String.IsNullOrEmpty(individualPackageVersion.SpecialVersion) && !searchContext.AllowPrerelease) { continue; } long?versionDownloadCount = null; if (packageVersionEntry.HasProperty("downloads")) { versionDownloadCount = packageVersionEntry.downloads; } string registrationUrl = packageVersionEntry.Metadata.id; // This should be PackageFeed3 // There should be a better way to reuse this function NuGetPackageFeed3 packageFeed3 = (NuGetPackageFeed3)this.ResourcesCollection.PackagesFeed; PackageBase packageVersionPackage = packageFeed3.Find(registrationUrl, individualPackageSearchContext, request, true).FirstOrDefault(); if (packageVersionPackage != null) { if (versionDownloadCount.HasValue) { packageVersionPackage.VersionDownloadCount = versionDownloadCount.Value; } // Reset these so we haven't collected all versions yet, so this is wrong packageVersionPackage.IsLatestVersion = false; packageVersionPackage.IsAbsoluteLatestVersion = false; versionToPackageTable[individualPackageVersion] = packageVersionPackage; } } } } // Now manually set the latest versions if (packageEntryInfo.LatestVersion != null && versionToPackageTable.ContainsKey(packageEntryInfo.LatestVersion)) { versionToPackageTable[packageEntryInfo.LatestVersion].IsLatestVersion = true; } if (packageEntryInfo.AbsoluteLatestVersion != null && versionToPackageTable.ContainsKey(packageEntryInfo.AbsoluteLatestVersion)) { versionToPackageTable[packageEntryInfo.AbsoluteLatestVersion].IsAbsoluteLatestVersion = true; } // I think this is the best we can do for enumeration (reads all versions of a package before yielding anything) foreach (PackageBase package in versionToPackageTable.Values) { yield return(package); } } }