From 9968418b6861ec8320c78a60448d8253d725ad51 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Thu, 22 Jan 2015 00:23:04 -0600 Subject: [PATCH] (GH-14) IChocolateyPackageService ensure app installed ChocolateyPackageService will determine based on the ISourceRunners it has in the list on ensuring that an app/tool is installed. --- .../services/ChocolateyPackageService.cs | 57 ++++++++++++++----- .../services/IChocolateyPackageService.cs | 7 +++ 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs index d4197e6514..fe35ad23df 100644 --- a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs +++ b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs @@ -31,6 +31,7 @@ namespace chocolatey.infrastructure.app.services public class ChocolateyPackageService : IChocolateyPackageService { private readonly INugetService _nugetService; + private readonly IEnumerable _sourceRunners; private readonly IPowershellService _powershellService; private readonly IShimGenerationService _shimgenService; private readonly IFileSystem _fileSystem; @@ -38,9 +39,10 @@ public class ChocolateyPackageService : IChocolateyPackageService private readonly IChocolateyPackageInformationService _packageInfoService; private readonly IAutomaticUninstallerService _autoUninstallerService; - public ChocolateyPackageService(INugetService nugetService, IPowershellService powershellService, IShimGenerationService shimgenService, IFileSystem fileSystem, IRegistryService registryService, IChocolateyPackageInformationService packageInfoService, IAutomaticUninstallerService autoUninstallerService) + public ChocolateyPackageService(INugetService nugetService, IEnumerable sourceRunners, IPowershellService powershellService, IShimGenerationService shimgenService, IFileSystem fileSystem, IRegistryService registryService, IChocolateyPackageInformationService packageInfoService, IAutomaticUninstallerService autoUninstallerService) { _nugetService = nugetService; + _sourceRunners = sourceRunners; _powershellService = powershellService; _shimgenService = shimgenService; _fileSystem = fileSystem; @@ -49,32 +51,59 @@ public ChocolateyPackageService(INugetService nugetService, IPowershellService p _autoUninstallerService = autoUninstallerService; } - public void list_noop(ChocolateyConfiguration config) + 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))); + } + + private void perform_source_runner_action(ChocolateyConfiguration config, Action action) { - if (config.Sources.is_equal_to(SpecialSourceType.webpi.to_string())) + var runner = _sourceRunners.FirstOrDefault(r => r.SourceType == config.SourceType); + if (runner != null && action != null) { - //todo: webpi + action.Invoke(runner); } else { - _nugetService.list_noop(config); + this.Log().Warn("No runner was found that implements source type '{0}' or action was missing".format_with(config.SourceType.to_string())); } + } + + private T perform_source_runner_function(ChocolateyConfiguration config, Func function) + { + var runner = _sourceRunners.FirstOrDefault(r => r.SourceType == config.SourceType); + if (runner != null && function != null) + { + return function.Invoke(runner); + } + + this.Log().Warn("No runner was found that implements source type '{0}' or function was missing.".format_with(config.SourceType.to_string())); + return default(T); + } + + public void list_noop(ChocolateyConfiguration config) + { + perform_source_runner_action(config, r => r.list_noop(config)); + + //switch (config.SourceType) + //{ + // case SourceType.normal: + // _nugetService.list_noop(config); + // break; + // default: + // perform_source_runner_action(config,r => r.list_noop(config)); + // break; + //} } public void list_run(ChocolateyConfiguration config, bool logResults) { this.Log().Debug(() => "Searching for package information"); - if (config.Sources.is_equal_to(SpecialSourceType.webpi.to_string())) - { - //todo: webpi - //install webpi if not installed - //run the webpi command - this.Log().Warn("Command not yet functional, stay tuned..."); - } - else + var list = perform_source_runner_function(config, r => r.list_run(config, logResults)); + + if (config.SourceType == SourceType.normal) { - var list = _nugetService.list_run(config, logResults: true); if (config.RegularOuptut) { this.Log().Warn(() => @"{0} packages {1}.".format_with(list.Count, config.ListCommand.LocalOnly ? "installed" : "found")); diff --git a/src/chocolatey/infrastructure.app/services/IChocolateyPackageService.cs b/src/chocolatey/infrastructure.app/services/IChocolateyPackageService.cs index 7e468977a5..aae99907ef 100644 --- a/src/chocolatey/infrastructure.app/services/IChocolateyPackageService.cs +++ b/src/chocolatey/infrastructure.app/services/IChocolateyPackageService.cs @@ -24,6 +24,13 @@ namespace chocolatey.infrastructure.app.services /// public interface IChocolateyPackageService { + + /// + /// Ensures the application that controls a source is installed + /// + /// The configuration. + void ensure_source_app_installed(ChocolateyConfiguration config); + /// /// Run list in noop mode ///