From 754cd0a4323e61a882a06d6b1fa738a48ce078a9 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Sun, 26 May 2019 17:05:52 -0500 Subject: [PATCH] (maint) move find_package to NuGetList Listing or searching using some of NuGet.Core components should be in a common place - that is NuGetList.cs currently. Move the optimized method to where it can be used everywhere. Additionally, add smarts to the method to determine if one is using the older methods of search when repository optimizations are not available. --- .../infrastructure.app/nuget/NugetList.cs | 96 ++++++++++++++++++- .../services/NugetService.cs | 96 +------------------ 2 files changed, 99 insertions(+), 93 deletions(-) diff --git a/src/chocolatey/infrastructure.app/nuget/NugetList.cs b/src/chocolatey/infrastructure.app/nuget/NugetList.cs index 946c20f4ad..b98d9f6977 100644 --- a/src/chocolatey/infrastructure.app/nuget/NugetList.cs +++ b/src/chocolatey/infrastructure.app/nuget/NugetList.cs @@ -18,6 +18,7 @@ namespace chocolatey.infrastructure.app.nuget { using System; using System.Collections.Generic; + using System.Globalization; using System.Linq; using NuGet; @@ -142,8 +143,101 @@ private static IQueryable execute_package_search(ChocolateyConfigurati : results; return results; - } + } + + /// + /// Searches for packages that are available based on name and other options + /// + /// Name of package to search for + /// Optional version to search for + /// Chocolatey configuration used to help supply the search parameters + /// Repository (aggregate for multiple) to search in + /// One result or nothing + public static IPackage find_package(string packageName, SemanticVersion version, ChocolateyConfiguration config, IPackageRepository repository) + { + // use old method when newer method causes issues + if (!config.Features.UsePackageRepositoryOptimizations) return repository.FindPackage(packageName, version, config.Prerelease, allowUnlisted: false); + + packageName = packageName.to_string().ToLower(CultureInfo.CurrentCulture); + // find the package based on version using older method + if (version != null) return repository.FindPackage(packageName, version, config.Prerelease, allowUnlisted: false); + + // we should always be using an aggregate repository + var aggregateRepository = repository as AggregateRepository; + if (aggregateRepository != null) + { + var packageResults = new List(); + + foreach (var packageRepository in aggregateRepository.Repositories.or_empty_list_if_null()) + { + try + { + "chocolatey".Log().Debug("Using '" + packageRepository.Source + "'."); + "chocolatey".Log().Debug("- Supports prereleases? '" + packageRepository.SupportsPrereleasePackages + "'."); + "chocolatey".Log().Debug("- Is ServiceBased? '" + (packageRepository is IServiceBasedRepository) + "'."); + + // search based on lower case id - similar to PackageRepositoryExtensions.FindPackagesByIdCore() + IQueryable combinedResults = packageRepository.GetPackages().Where(x => x.Id.ToLower() == packageName); + + if (config.Prerelease && packageRepository.SupportsPrereleasePackages) + { + combinedResults = combinedResults.Where(p => p.IsAbsoluteLatestVersion); + } + else + { + combinedResults = combinedResults.Where(p => p.IsLatestVersion); + } + + if (!(packageRepository is IServiceBasedRepository)) + { + combinedResults = combinedResults + .Where(PackageExtensions.IsListed) + .Where(p => config.Prerelease || p.IsReleaseVersion()) + .distinct_last(PackageEqualityComparer.Id, PackageComparer.Version) + .AsQueryable(); + } + + var packageRepositoryResults = combinedResults.ToList(); + if (packageRepositoryResults.Count() != 0) + { + "chocolatey".Log().Debug("Package '{0}' found on source '{1}'".format_with(packageName, packageRepository.Source)); + packageResults.AddRange(packageRepositoryResults); + } + } + catch (Exception e) + { + "chocolatey".Log().Warn("Error retrieving packages from source '{0}':{1} {2}".format_with(packageRepository.Source, Environment.NewLine, e.Message)); + } + } + + // get only one result, should be the latest - similar to TryFindLatestPackageById + return packageResults.OrderByDescending(x => x.Version).FirstOrDefault(); + } + + // search based on lower case id - similar to PackageRepositoryExtensions.FindPackagesByIdCore() + IQueryable results = repository.GetPackages().Where(x => x.Id.ToLower() == packageName); + if (config.Prerelease && repository.SupportsPrereleasePackages) + { + results = results.Where(p => p.IsAbsoluteLatestVersion); + } + else + { + results = results.Where(p => p.IsLatestVersion); + } + + if (!(repository is IServiceBasedRepository)) + { + results = results + .Where(PackageExtensions.IsListed) + .Where(p => config.Prerelease || p.IsReleaseVersion()) + .distinct_last(PackageEqualityComparer.Id, PackageComparer.Version) + .AsQueryable(); + } + + // get only one result, should be the latest - similar to TryFindLatestPackageById + return results.ToList().OrderByDescending(x => x.Version).FirstOrDefault(); + } } diff --git a/src/chocolatey/infrastructure.app/services/NugetService.cs b/src/chocolatey/infrastructure.app/services/NugetService.cs index f1b1493855..696a2b97d0 100644 --- a/src/chocolatey/infrastructure.app/services/NugetService.cs +++ b/src/chocolatey/infrastructure.app/services/NugetService.cs @@ -471,9 +471,7 @@ public virtual ConcurrentDictionary install_run(Chocolate continue; } - IPackage availablePackage = config.Features.UsePackageRepositoryOptimizations ? - find_package(packageName, version, config, packageManager.SourceRepository) - : packageManager.SourceRepository.FindPackage(packageName, version, config.Prerelease, allowUnlisted: false); + IPackage availablePackage = NugetList.find_package(packageName, version, config, packageManager.SourceRepository); if (availablePackage == null) { @@ -695,9 +693,8 @@ public virtual ConcurrentDictionary upgrade_run(Chocolate config.Prerelease = true; } - IPackage availablePackage = config.Features.UsePackageRepositoryOptimizations ? - find_package(packageName, version, config, packageManager.SourceRepository) - : packageManager.SourceRepository.FindPackage(packageName, version, config.Prerelease, allowUnlisted: false); + IPackage availablePackage = NugetList.find_package(packageName, version, config, packageManager.SourceRepository); + config.Prerelease = originalPrerelease; @@ -924,9 +921,7 @@ public virtual ConcurrentDictionary get_outdated(Chocolat } SemanticVersion version = null; - var latestPackage = config.Features.UsePackageRepositoryOptimizations ? - find_package(packageName, null, config, packageManager.SourceRepository) - : packageManager.SourceRepository.FindPackage(packageName, version, config.Prerelease, allowUnlisted: false); + var latestPackage = NugetList.find_package(packageName, null, config, packageManager.SourceRepository); if (latestPackage == null) { @@ -954,89 +949,6 @@ public virtual ConcurrentDictionary get_outdated(Chocolat return outdatedPackages; } - private IPackage find_package(string packageName, SemanticVersion version, ChocolateyConfiguration config, IPackageRepository repository) - { - packageName = packageName.to_string().ToLower(CultureInfo.CurrentCulture); - // find the package based on version - if (version != null) return repository.FindPackage(packageName, version, config.Prerelease, allowUnlisted: false); - - // we should always be using an aggregate repository - var aggregateRepository = repository as AggregateRepository; - if (aggregateRepository != null) - { - var packageResults = new List(); - - foreach (var packageRepository in aggregateRepository.Repositories.or_empty_list_if_null()) - { - try - { - this.Log().Debug("Using '" + packageRepository.Source + "'."); - this.Log().Debug("- Supports prereleases? '" + packageRepository.SupportsPrereleasePackages + "'."); - this.Log().Debug("- Is ServiceBased? '" + (packageRepository is IServiceBasedRepository) + "'."); - - // search based on lower case id - similar to PackageRepositoryExtensions.FindPackagesByIdCore() - IQueryable combinedResults = packageRepository.GetPackages().Where(x => x.Id.ToLower() == packageName); - - if (config.Prerelease && packageRepository.SupportsPrereleasePackages) - { - combinedResults = combinedResults.Where(p => p.IsAbsoluteLatestVersion); - } - else - { - combinedResults = combinedResults.Where(p => p.IsLatestVersion); - } - - if (!(packageRepository is IServiceBasedRepository)) - { - combinedResults = combinedResults - .Where(PackageExtensions.IsListed) - .Where(p => config.Prerelease || p.IsReleaseVersion()) - .distinct_last(PackageEqualityComparer.Id, PackageComparer.Version) - .AsQueryable(); - } - - var packageRepositoryResults = combinedResults.ToList(); - if (packageRepositoryResults.Count() != 0) - { - this.Log().Debug("Package '{0}' found on source '{1}'".format_with(packageName, packageRepository.Source)); - packageResults.AddRange(packageRepositoryResults); - } - } - catch (Exception e) - { - this.Log().Warn("Error retrieving packages from source '{0}':{1} {2}".format_with(packageRepository.Source, Environment.NewLine, e.Message)); - } - } - - // get only one result, should be the latest - similar to TryFindLatestPackageById - return packageResults.OrderByDescending(x => x.Version).FirstOrDefault(); - } - - // search based on lower case id - similar to PackageRepositoryExtensions.FindPackagesByIdCore() - IQueryable results = repository.GetPackages().Where(x => x.Id.ToLower() == packageName); - - if (config.Prerelease && repository.SupportsPrereleasePackages) - { - results = results.Where(p => p.IsAbsoluteLatestVersion); - } - else - { - results = results.Where(p => p.IsLatestVersion); - } - - if (!(repository is IServiceBasedRepository)) - { - results = results - .Where(PackageExtensions.IsListed) - .Where(p => config.Prerelease || p.IsReleaseVersion()) - .distinct_last(PackageEqualityComparer.Id, PackageComparer.Version) - .AsQueryable(); - } - - // get only one result, should be the latest - similar to TryFindLatestPackageById - return results.ToList().OrderByDescending(x => x.Version).FirstOrDefault(); - } - /// /// Sets the configuration for the package upgrade ///