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 4, 2015
1 parent cec6145 commit 80a84e4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
11 changes: 10 additions & 1 deletion src/chocolatey/GetChocolatey.cs
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
}
}
40 changes: 33 additions & 7 deletions src/chocolatey/infrastructure.app/runners/GenericRunner.cs
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 Down Expand Up @@ -83,7 +85,7 @@ public void run(ChocolateyConfiguration config, Container container, bool isCons
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.
"
);
);

}
}
Expand All @@ -96,14 +98,38 @@ public void run(ChocolateyConfiguration config, Container container, bool isCons
}

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 List Mode _".format_with(ApplicationParameters.Name, command.GetType().Name));
return command.list(config);
}
}
}

}
}

0 comments on commit 80a84e4

Please sign in to comment.