Skip to content

Commit

Permalink
Merge branch 'stable'
Browse files Browse the repository at this point in the history
* stable:
  (GH-1831) search/list/info - use package search optimizations
  (maint) move find_package to NuGetList
  (log) add separators for package execution output
  (maint) reorder outdated in interface
  • Loading branch information
ferventcoder committed May 26, 2019
2 parents e9e5bc9 + 97a171f commit f461180
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 104 deletions.
3 changes: 3 additions & 0 deletions src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ if ($env:ChocolateyEnvironmentDebug -eq 'true') { $global:DebugPreference = "Con
$global:VerbosePreference = "SilentlyContinue"
if ($env:ChocolateyEnvironmentVerbose -eq 'true') { $global:VerbosePreference = "Continue"; $verbosity = $true }

Write-Debug '---------------------------Script Execution---------------------------'
Write-Debug "Running 'ChocolateyScriptRunner' for $($env:packageName) v$($env:packageVersion) with packageScript `'$packageScript`', packageFolder:`'$($env:packageFolder)`', installArguments: `'$installArguments`', packageParameters: `'$packageParameters`',"

## Set the culture to invariant
Expand Down Expand Up @@ -74,4 +75,6 @@ if ($exitCode -ne $null -and $exitCode -ne '' -and $exitCode -ne 0) {
Set-PowerShellExitCode $exitCode
}

Write-Debug '----------------------------------------------------------------------'

Exit $exitCode
103 changes: 101 additions & 2 deletions src/chocolatey/infrastructure.app/nuget/NugetList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace chocolatey.infrastructure.app.nuget
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

using NuGet;
Expand Down Expand Up @@ -60,9 +61,14 @@ private static IQueryable<IPackage> execute_package_search(ChocolateyConfigurati

IQueryable<IPackage> results = packageRepository.Search(searchTermLower, configuration.Prerelease);

SemanticVersion version = !string.IsNullOrWhiteSpace(configuration.Version) ? new SemanticVersion(configuration.Version) : null;

if (configuration.ListCommand.Exact)
{
results = packageRepository.FindPackagesById(searchTermLower).AsQueryable();
return new List<IPackage>()
{
find_package(searchTermLower, version, configuration, packageRepository)
}.AsQueryable();
}

if (configuration.ListCommand.Page.HasValue)
Expand Down Expand Up @@ -142,8 +148,101 @@ private static IQueryable<IPackage> execute_package_search(ChocolateyConfigurati
: results;

return results;
}
}

/// <summary>
/// Searches for packages that are available based on name and other options
/// </summary>
/// <param name="packageName">Name of package to search for</param>
/// <param name="version">Optional version to search for</param>
/// <param name="config">Chocolatey configuration used to help supply the search parameters</param>
/// <param name="repository">Repository (aggregate for multiple) to search in</param>
/// <returns>One result or nothing</returns>
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<IPackage>();

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<IPackage> 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<IPackage> 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();
}

}

Expand Down
21 changes: 11 additions & 10 deletions src/chocolatey/infrastructure.app/services/INugetService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2017 - 2018 Chocolatey Software, Inc
// Copyright © 2017 - 2019 Chocolatey Software, Inc
// Copyright © 2011 - 2017 RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -16,12 +16,18 @@

namespace chocolatey.infrastructure.app.services
{
using chocolatey.infrastructure.results;
using configuration;
using System.Collections.Concurrent;
using System.Collections.Concurrent;
using configuration;
using results;

public interface INugetService : ISourceRunner
public interface INugetService : ISourceRunner
{
/// <summary>
/// Get outdated packages
/// </summary>
/// <param name="config">The configuration.</param>
ConcurrentDictionary<string, PackageResult> get_outdated(ChocolateyConfiguration config);

/// <summary>
/// Run pack in noop mode.
/// </summary>
Expand Down Expand Up @@ -52,10 +58,5 @@ public interface INugetService : ISourceRunner
/// <param name="packageName">Name of the package.</param>
void remove_rollback_directory_if_exists(string packageName);

/// <summary>
/// Get outdated packages
/// </summary>
/// <param name="config">The configuration.</param>
ConcurrentDictionary<string, PackageResult> get_outdated(ChocolateyConfiguration config);
}
}
96 changes: 4 additions & 92 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,7 @@ public virtual ConcurrentDictionary<string, PackageResult> 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)
{
Expand Down Expand Up @@ -695,9 +693,8 @@ public virtual ConcurrentDictionary<string, PackageResult> 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;

Expand Down Expand Up @@ -924,9 +921,7 @@ public virtual ConcurrentDictionary<string, PackageResult> 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)
{
Expand Down Expand Up @@ -954,89 +949,6 @@ public virtual ConcurrentDictionary<string, PackageResult> 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<IPackage>();

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<IPackage> 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<IPackage> 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();
}

/// <summary>
/// Sets the configuration for the package upgrade
/// </summary>
Expand Down

0 comments on commit f461180

Please sign in to comment.