From 80a84e4335cdf1dfe6f5edc3f8492d73746747f5 Mon Sep 17 00:00:00 2001 From: Joel Bennett Date: Tue, 3 Mar 2015 02:24:36 -0500 Subject: [PATCH] (GH-132) Add a list method to the GenericRunner Refactor GenericRunner.run to a common find_command and separate the run and list methods which call different methods on the underlying command (IListCommand). Previously we only supported GenericRunner.run, this also updates GetChocolatey to add a list method that calls GenericRunner.list --- src/chocolatey/GetChocolatey.cs | 11 ++++- .../runners/GenericRunner.cs | 40 +++++++++++++++---- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/chocolatey/GetChocolatey.cs b/src/chocolatey/GetChocolatey.cs index e6487625e5..b7c82b4bec 100644 --- a/src/chocolatey/GetChocolatey.cs +++ b/src/chocolatey/GetChocolatey.cs @@ -213,7 +213,16 @@ private void extract_resources() AssemblyFileExtractor.extract_all_resources_to_relative_directory(_container.GetInstance(), Assembly.GetAssembly(typeof(ChocolateyResourcesAssembly)), ApplicationParameters.InstallLocation, folders, ApplicationParameters.ChocolateyFileResources); } + + public IEnumerable List() + { + extract_resources(); + var configuration = create_configuration(new List()); + configuration.RegularOuptut = true; + var runner = new GenericRunner(); + return runner.list(configuration, _container, isConsole: false, parseArgs: null); + } } // ReSharper restore InconsistentNaming -} \ No newline at end of file +} diff --git a/src/chocolatey/infrastructure.app/runners/GenericRunner.cs b/src/chocolatey/infrastructure.app/runners/GenericRunner.cs index 99b14f1ace..f75e16ebbb 100644 --- a/src/chocolatey/infrastructure.app/runners/GenericRunner.cs +++ b/src/chocolatey/infrastructure.app/runners/GenericRunner.cs @@ -13,10 +13,12 @@ // See the License for the specific language governing permissions and // limitations under the License. + namespace chocolatey.infrastructure.app.runners { using System; using System.Linq; + using System.Collections.Generic; using SimpleInjector; using adapters; using attributes; @@ -28,7 +30,7 @@ namespace chocolatey.infrastructure.app.runners public sealed class GenericRunner { - public void run(ChocolateyConfiguration config, Container container, bool isConsole, Action parseArgs) + private ICommand find_command(ChocolateyConfiguration config, Container container, bool isConsole, Action parseArgs) { var commands = container.GetAllInstances(); var command = commands.Where((c) => @@ -83,7 +85,7 @@ Chocolatey is not an official build (bypassed with --allow-unofficial). If you are seeing this message and it is not expected, your system may now be in a bad state. Only official builds are to be trusted. " - ); + ); } } @@ -96,14 +98,38 @@ now be in a bad state. Only official builds are to be trusted. } command.noop(config); + return null; } - else + } + return command; + } + + public void run(ChocolateyConfiguration config, Container container, bool isConsole, Action parseArgs) + { + var command = find_command(config, container, isConsole, parseArgs); + if(command != null) + { + this.Log().Debug("_ {0}:{1} - Normal Run Mode _".format_with(ApplicationParameters.Name, command.GetType().Name)); + command.run(config); + } + } + + public IEnumerable list(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)) { - this.Log().Debug("_ {0}:{1} - Normal Run Mode _".format_with(ApplicationParameters.Name, command.GetType().Name)); - command.run(config); + throw new Exception("The implementation of '{0}' does not support listing '{1}'".format_with(config.CommandName, typeof(T).Name)); } + return new List(); + } + else + { + this.Log().Debug("_ {0}:{1} - Normal List Mode _".format_with(ApplicationParameters.Name, command.GetType().Name)); + return command.list(config); } } } - -} \ No newline at end of file +}