Skip to content

Commit

Permalink
(chocolateyGH-132) Add a list method to the GenericRunner
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Jaykul committed Mar 3, 2015
1 parent a61a38b commit 90eb8d8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
11 changes: 10 additions & 1 deletion src/chocolatey/GetChocolatey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,16 @@ private void extract_resources()

AssemblyFileExtractor.extract_all_resources_to_relative_directory(_container.GetInstance<IFileSystem>(), Assembly.GetAssembly(typeof(ChocolateyResourcesAssembly)), ApplicationParameters.InstallLocation, folders, ApplicationParameters.ChocolateyFileResources);
}

public IEnumerable<T> List<T>()
{
extract_resources();
var configuration = create_configuration(new List<string>());
configuration.RegularOuptut = true;
var runner = new GenericRunner();
return runner.list<T>(configuration, _container, isConsole: false, parseArgs: null);
}
}

// ReSharper restore InconsistentNaming
}
}
43 changes: 32 additions & 11 deletions src/chocolatey/infrastructure.app/runners/GenericRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +30,7 @@ namespace chocolatey.infrastructure.app.runners

public sealed class GenericRunner
{
public void run(ChocolateyConfiguration config, Container container, bool isConsole, Action<ICommand> parseArgs)
private ICommand find_command(ChocolateyConfiguration config, Container container, bool isConsole, Action<ICommand> parseArgs)
{
var commands = container.GetAllInstances<ICommand>();
var command = commands.Where((c) =>
Expand All @@ -53,7 +55,6 @@ public void run(ChocolateyConfiguration config, Container container, bool isCons
{
parseArgs.Invoke(command);
}

this.Log().Debug(() => "Configuration: {0}".format_with(config.ToString()));


Expand Down Expand Up @@ -82,27 +83,47 @@ Custom unofficial builds are not allowed by default.
choco.exe 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.
"
);

");
}
}

if (config.Noop)
{
if (config.RegularOuptut)
{
this.Log().Info("_ {0}:{1} - Noop Mode _".format_with(ApplicationParameters.Name, command.GetType().Name));
}

command.noop(config);
return null;
}
else
}
return command;
}

public void run(ChocolateyConfiguration config, Container container, bool isConsole, Action<ICommand> 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<T> list<T>(ChocolateyConfiguration config, Container container, bool isConsole, Action<ICommand> parseArgs)
{
var command = find_command(config, container, isConsole, parseArgs) as IListCommand<T>;
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<T>();
}
else
{
this.Log().Debug("_ {0}:{1} - Normal Run Mode _".format_with(ApplicationParameters.Name, command.GetType().Name));
return command.list(config);
}
}
}
}
}

0 comments on commit 90eb8d8

Please sign in to comment.