From 3042c74ca9e1603181a58604ce578447ab5e6af8 Mon Sep 17 00:00:00 2001 From: Richard Simpson Date: Fri, 25 Sep 2015 13:50:55 -0500 Subject: [PATCH] Add Get Count API --- src/chocolatey/GetChocolatey.cs | 9 +++++++ .../commands/ChocolateyListCommand.cs | 6 +++++ .../commands/ChocolateySourceCommand.cs | 5 ++++ .../infrastructure.app/nuget/NugetList.cs | 27 +++++++++++++++++++ .../runners/GenericRunner.cs | 18 +++++++++++++ .../services/ChocolateyPackageService.cs | 5 ++++ .../services/CygwinService.cs | 5 ++++ .../services/IChocolateyPackageService.cs | 7 +++++ .../services/ISourceRunner.cs | 7 +++++ .../services/NugetService.cs | 13 +++++++++ .../services/PythonService.cs | 5 ++++ .../services/RubyGemsService.cs | 5 ++++ .../services/WebPiService.cs | 5 ++++ .../services/WindowsFeatureService.cs | 5 ++++ .../infrastructure/commands/IListCommand.cs | 7 ++++- 15 files changed, 128 insertions(+), 1 deletion(-) diff --git a/src/chocolatey/GetChocolatey.cs b/src/chocolatey/GetChocolatey.cs index c8883f2192..7c58d2fd99 100644 --- a/src/chocolatey/GetChocolatey.cs +++ b/src/chocolatey/GetChocolatey.cs @@ -222,6 +222,15 @@ public IEnumerable List() var runner = new GenericRunner(); return runner.list(configuration, _container, isConsole: false, parseArgs: null); } + + public int Count() + { + extract_resources(); + var configuration = create_configuration(new List()); + configuration.RegularOutput = true; + var runner = new GenericRunner(); + return runner.count(configuration, _container, isConsole: false, parseArgs: null); + } } // ReSharper restore InconsistentNaming diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs index 1855be78ec..32cce2b02b 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs @@ -140,6 +140,12 @@ public IEnumerable list(ChocolateyConfiguration configuration) return _packageService.list_run(configuration); } + public int count(ChocolateyConfiguration config) + { + config.QuietOutput = true; + return _packageService.count_run(config); + } + public bool may_require_admin_access() { return false; diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateySourceCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateySourceCommand.cs index d82876812a..c13331b055 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateySourceCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateySourceCommand.cs @@ -148,6 +148,11 @@ public IEnumerable list(ChocolateyConfiguration configuration) return _configSettingsService.source_list(configuration); } + public int count(ChocolateyConfiguration config) + { + return list(config).Count(); + } + public bool may_require_admin_access() { return true; diff --git a/src/chocolatey/infrastructure.app/nuget/NugetList.cs b/src/chocolatey/infrastructure.app/nuget/NugetList.cs index 88c573311e..07bcb2f6a5 100644 --- a/src/chocolatey/infrastructure.app/nuget/NugetList.cs +++ b/src/chocolatey/infrastructure.app/nuget/NugetList.cs @@ -54,6 +54,33 @@ public static IEnumerable GetPackages(ChocolateyConfiguration configur .Where(p => configuration.Prerelease || p.IsReleaseVersion()) .distinct_last(PackageEqualityComparer.Id, PackageComparer.Version); } + + public static int GetCount(ChocolateyConfiguration configuration, ILogger nugetLogger) + { + var packageRepository = NugetCommon.GetRemoteRepository(configuration, nugetLogger); + IQueryable results = packageRepository.Search(configuration.Input, configuration.Prerelease); + + if (configuration.AllVersions) + { + return results.Where(PackageExtensions.IsListed).Count(); + } + + if (configuration.Prerelease && packageRepository.SupportsPrereleasePackages) + { + results = results.Where(p => p.IsAbsoluteLatestVersion); + } + else + { + results = results.Where(p => p.IsLatestVersion); + } + + return results + .AsEnumerable() + .Where(PackageExtensions.IsListed) + .Where(p => configuration.Prerelease || p.IsReleaseVersion()) + .distinct_last(PackageEqualityComparer.Id, PackageComparer.Version) + .Count(); + } } // ReSharper restore InconsistentNaming diff --git a/src/chocolatey/infrastructure.app/runners/GenericRunner.cs b/src/chocolatey/infrastructure.app/runners/GenericRunner.cs index 5347cec8c3..43f78e6150 100644 --- a/src/chocolatey/infrastructure.app/runners/GenericRunner.cs +++ b/src/chocolatey/infrastructure.app/runners/GenericRunner.cs @@ -150,6 +150,24 @@ public IEnumerable list(ChocolateyConfiguration config, Container containe } } + public int count(ChocolateyConfiguration config, Container container, bool isConsole, Action parseArgs) + { + var command = find_command(config, container, isConsole, parseArgs) as IListCommand; + if (command == null) + { + if (!string.IsNullOrWhiteSpace(config.CommandName)) + { + throw new Exception("The implementation of '{0}' does not support listing.".format_with(config.CommandName)); + } + return 0; + } + else + { + this.Log().Debug("_ {0}:{1} - Normal Count Mode _".format_with(ApplicationParameters.Name, command.GetType().Name)); + return command.count(config); + } + } + public void warn_when_admin_needs_elevation(ChocolateyConfiguration config) { var shouldWarn = (!config.Information.IsProcessElevated && config.Information.IsUserAdministrator); diff --git a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs index 0e49b6fd98..f2646fde41 100644 --- a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs +++ b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs @@ -70,6 +70,11 @@ public void ensure_source_app_installed(ChocolateyConfiguration config) perform_source_runner_action(config, r => r.ensure_source_app_installed(config, (packageResult) => handle_package_result(packageResult, config, CommandNameType.install))); } + public int count_run(ChocolateyConfiguration config) + { + return perform_source_runner_function(config, r => r.count_run(config)); + } + private void perform_source_runner_action(ChocolateyConfiguration config, Action action) { var runner = _sourceRunners.FirstOrDefault(r => r.SourceType == config.SourceType); diff --git a/src/chocolatey/infrastructure.app/services/CygwinService.cs b/src/chocolatey/infrastructure.app/services/CygwinService.cs index c94681202b..911f87b896 100644 --- a/src/chocolatey/infrastructure.app/services/CygwinService.cs +++ b/src/chocolatey/infrastructure.app/services/CygwinService.cs @@ -156,6 +156,11 @@ public void ensure_source_app_installed(ChocolateyConfiguration config, Action

The configuration. void ensure_source_app_installed(ChocolateyConfiguration config); + ///

+ /// Retrieves the count of items that meet the search criteria. + /// + /// + /// + int count_run(ChocolateyConfiguration config); + /// /// Run list in noop mode /// diff --git a/src/chocolatey/infrastructure.app/services/ISourceRunner.cs b/src/chocolatey/infrastructure.app/services/ISourceRunner.cs index ac9826ddfd..c881f10a64 100644 --- a/src/chocolatey/infrastructure.app/services/ISourceRunner.cs +++ b/src/chocolatey/infrastructure.app/services/ISourceRunner.cs @@ -39,6 +39,13 @@ public interface ISourceRunner /// The action to continue with as part of the install void ensure_source_app_installed(ChocolateyConfiguration config, Action ensureAction); + /// + /// Retrieve the listed packages from the source feed cout + /// + /// The configuration. + /// Packages count + int count_run(ChocolateyConfiguration config); + /// /// Run list in noop mode /// diff --git a/src/chocolatey/infrastructure.app/services/NugetService.cs b/src/chocolatey/infrastructure.app/services/NugetService.cs index 64e0765d56..db6659c75a 100644 --- a/src/chocolatey/infrastructure.app/services/NugetService.cs +++ b/src/chocolatey/infrastructure.app/services/NugetService.cs @@ -75,6 +75,19 @@ public void ensure_source_app_installed(ChocolateyConfiguration config, Action

: ICommand + public interface IListCommand : ICommand + { + int count(ChocolateyConfiguration config); + } + + public interface IListCommand : IListCommand { IEnumerable list(ChocolateyConfiguration config); }