From 5742f16e20669699663bd275c42e70754e6f4fef Mon Sep 17 00:00:00 2001 From: Joel Bennett Date: Mon, 16 Mar 2015 00:27:48 -0400 Subject: [PATCH] (GH-132) Move "LogOutput" to the configuration as "Quiet" so it works the way others do... --- .../commands/ChocolateyListCommandSpecs.cs | 2 +- .../commands/ChocolateyPinCommandSpecs.cs | 4 +- .../commands/ChocolateyListCommand.cs | 8 +- .../commands/ChocolateyPinCommand.cs | 2 +- .../commands/ChocolateyVersionCommand.cs | 2 +- .../configuration/ChocolateyConfiguration.cs | 12 +++ .../services/ChocolateyPackageService.cs | 93 +++++++++---------- .../services/IChocolateyPackageService.cs | 3 +- .../services/INugetService.cs | 2 +- .../services/NugetService.cs | 36 +++---- 10 files changed, 86 insertions(+), 78 deletions(-) diff --git a/src/chocolatey.tests/infrastructure.app/commands/ChocolateyListCommandSpecs.cs b/src/chocolatey.tests/infrastructure.app/commands/ChocolateyListCommandSpecs.cs index 36a8096b96..99d2154d4b 100644 --- a/src/chocolatey.tests/infrastructure.app/commands/ChocolateyListCommandSpecs.cs +++ b/src/chocolatey.tests/infrastructure.app/commands/ChocolateyListCommandSpecs.cs @@ -208,7 +208,7 @@ public override void Because() [Fact] public void should_call_service_list_run() { - packageService.Verify(c => c.list_run(configuration, true), Times.Once); + packageService.Verify(c => c.list_run(configuration), Times.Once); } } } diff --git a/src/chocolatey.tests/infrastructure.app/commands/ChocolateyPinCommandSpecs.cs b/src/chocolatey.tests/infrastructure.app/commands/ChocolateyPinCommandSpecs.cs index 95ae4330cd..e9248924b2 100644 --- a/src/chocolatey.tests/infrastructure.app/commands/ChocolateyPinCommandSpecs.cs +++ b/src/chocolatey.tests/infrastructure.app/commands/ChocolateyPinCommandSpecs.cs @@ -342,7 +342,7 @@ public override void Context() new PackageResult(package.Object, null), new PackageResult(pinnedPackage.Object, null) }; - nugetService.Setup(n => n.list_run(It.IsAny(), true)).Returns(packageResults); + nugetService.Setup(n => n.list_run(It.IsAny())).Returns(packageResults); configuration.PinCommand.Command = PinCommandType.list; } @@ -414,7 +414,7 @@ public void should_call_nuget_service_list_run_when_command_is_list() configuration.PinCommand.Command = PinCommandType.list; command.run(configuration); - nugetService.Verify(n => n.list_run(It.IsAny(), true), Times.Once); + nugetService.Verify(n => n.list_run(It.IsAny()), Times.Once); } [Pending("NuGet is killing me with extension methods. Need to find proper item to mock out to return the package object.")] diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs index 7dd8c2d0c1..77aa762809 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs @@ -16,6 +16,7 @@ namespace chocolatey.infrastructure.app.commands { using System.Collections.Generic; + using System.Linq; using attributes; using commandline; using configuration; @@ -108,12 +109,15 @@ public void noop(ChocolateyConfiguration configuration) public void run(ChocolateyConfiguration configuration) { - _packageService.list_run(configuration, logResults: true); + // you must leave the .ToList() here or else the method won't be evaluated! + _packageService.list_run(configuration).ToList(); } public IEnumerable list(ChocolateyConfiguration configuration) { - return _packageService.list_run(configuration, logResults: false); + configuration.Quiet = true; + // here it's up to the caller to enumerate the results + return _packageService.list_run(configuration); } } } diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyPinCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyPinCommand.cs index 73fa6769cb..57ea3ee3e8 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyPinCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyPinCommand.cs @@ -139,7 +139,7 @@ public void run(ChocolateyConfiguration configuration) public void list_pins(IPackageManager packageManager, ChocolateyConfiguration config) { - foreach (var pkg in _nugetService.list_run(config, logResults: true)) + foreach (var pkg in _nugetService.list_run(config)) { var pkgInfo = _packageInfoService.get_package_information(pkg.Package); if (pkgInfo != null && pkgInfo.IsPinned) diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyVersionCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyVersionCommand.cs index 02f285fccc..a3fd4de673 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyVersionCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyVersionCommand.cs @@ -115,7 +115,7 @@ public override void run(ChocolateyConfiguration configuration) { if (configuration.ListCommand.LocalOnly) { - _packageService.list_run(configuration,logResults:true); + _packageService.list_run(configuration); } else { diff --git a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs index f8a163b778..a5298e8f91 100644 --- a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs +++ b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs @@ -150,7 +150,19 @@ private void append_output(StringBuilder propertyValues, string append) public bool Force { get; set; } public bool Noop { get; set; } public bool HelpRequested { get; set; } + // TODO: Should just use output levels: Debug, Verbose, Info (Regular), Important, Error (Quiet) + /// + /// Gets or sets a value indicating whether output should be limited. + /// This supports the --limit-output parameter. + /// + /// true for regular output; false for limited output. public bool RegularOutput { get; set; } + /// + /// Gets or sets a value indicating whether console logging should be supressed. + /// This is for use by API calls which surface results in alternate forms. + /// + /// true for no output; false for regular or limited output. + public bool Quiet { get; set; } public bool PromptForConfirmation { get; set; } public bool AcceptLicense { get; set; } public bool AllowUnofficialBuild { get; set; } diff --git a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs index addd0363cc..521bc6698b 100644 --- a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs +++ b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs @@ -22,14 +22,15 @@ namespace chocolatey.infrastructure.app.services using commandline; using configuration; using domain; - using filesystem; using infrastructure.services; using logging; + using NuGet; using platforms; using results; using tolerance; + using IFileSystem = filesystem.IFileSystem; - public class ChocolateyPackageService : IChocolateyPackageService + public class ChocolateyPackageService : IChocolateyPackageService { private readonly INugetService _nugetService; private readonly IPowershellService _powershellService; @@ -64,7 +65,7 @@ public void list_noop(ChocolateyConfiguration config) } } - public IEnumerable list_run(ChocolateyConfiguration config, bool logResults) + public IEnumerable list_run(ChocolateyConfiguration config) { this.Log().Debug(() => "Searching for package information"); @@ -74,71 +75,63 @@ public IEnumerable list_run(ChocolateyConfiguration config, bool //install webpi if not installed //run the webpi command this.Log().Warn("Command not yet functional, stay tuned..."); - return new PackageResult[]{}; + yield break; } else { - var list = _nugetService.list_run(config, logResults: logResults).ToList(); - if (config.RegularOutput) - { - this.Log().Warn(() => @"{0} packages {1}.".format_with(list.Count, config.ListCommand.LocalOnly ? "installed" : "found")); - } - if (!config.ListCommand.LocalOnly && !config.ListCommand.IncludeRegistryPrograms) + var packages = new List(); + + foreach (var package in _nugetService.list_run(config)) { - return list; - } + if (!config.ListCommand.LocalOnly && !config.ListCommand.IncludeRegistryPrograms) + { + yield return package; + } - // in this case, we need to a list we can enumerate multiple times and append to + if (config.ListCommand.LocalOnly && config.ListCommand.IncludeRegistryPrograms && package.Package != null) + { + packages.Add(package.Package); + } + } if (config.ListCommand.LocalOnly && config.ListCommand.IncludeRegistryPrograms) { - report_registry_programs(config, list); + foreach (var installed in report_registry_programs(config, packages)) + { + yield return installed; + } } - - return list; } } - private void report_registry_programs(ChocolateyConfiguration config, List list) + private IEnumerable report_registry_programs(ChocolateyConfiguration config, IEnumerable list) { - var itemsToRemoveFromMachine = new List(); - foreach (var packageResult in list) + var itemsToRemoveFromMachine = list.Select(package => _packageInfoService.get_package_information(package)). + Where(p => p.RegistrySnapshot != null). + Select(p => p.RegistrySnapshot.RegistryKeys.FirstOrDefault()). + Where(p => p != null). + Select(p => p.DisplayName).ToList(); + + var count = 0; + var machineInstalled = _registryService.get_installer_keys().RegistryKeys. + Where((p) => p.is_in_programs_and_features() && !itemsToRemoveFromMachine.Contains(p.DisplayName)). + OrderBy((p) => p.DisplayName).Distinct(); + this.Log().Info(() => ""); + foreach (var key in machineInstalled) { - if (packageResult != null && packageResult.Package != null) - { - var pkginfo = _packageInfoService.get_package_information(packageResult.Package); - if (pkginfo.RegistrySnapshot == null) - { - continue; - } + if (config.RegularOutput) + { + this.Log().Info("{0}|{1}".format_with(key.DisplayName, key.DisplayVersion)); + if (config.Verbose) this.Log().Info(" InstallLocation: {0}{1} Uninstall:{2}".format_with(key.InstallLocation.escape_curly_braces(), Environment.NewLine, key.UninstallString.escape_curly_braces())); + } + count++; - var key = pkginfo.RegistrySnapshot.RegistryKeys.FirstOrDefault(); - if (key != null) - { - itemsToRemoveFromMachine.Add(key.DisplayName); - } - } + yield return new PackageResult(key.DisplayName, key.DisplayName, key.InstallLocation); } - var machineInstalled = _registryService.get_installer_keys().RegistryKeys.Where((p) => p.is_in_programs_and_features() && !itemsToRemoveFromMachine.Contains(p.DisplayName)).OrderBy((p) => p.DisplayName).Distinct().ToList(); - if (machineInstalled.Count != 0) + if (config.RegularOutput) { - this.Log().Info(() => ""); - foreach (var key in machineInstalled) - { - if (config.RegularOutput) - { - this.Log().Info("{0}|{1}".format_with(key.DisplayName, key.DisplayVersion)); - if (config.Verbose) this.Log().Info(" InstallLocation: {0}{1} Uninstall:{2}".format_with(key.InstallLocation.escape_curly_braces(), Environment.NewLine, key.UninstallString.escape_curly_braces())); - } - - list.Add( new PackageResult(key.DisplayName, key.DisplayName, key.InstallLocation) ); - } - - if (config.RegularOutput) - { - this.Log().Warn(() => @"{0} applications not managed with Chocolatey.".format_with(machineInstalled.Count)); - } + this.Log().Warn(() => @"{0} applications not managed with Chocolatey.".format_with(count)); } } diff --git a/src/chocolatey/infrastructure.app/services/IChocolateyPackageService.cs b/src/chocolatey/infrastructure.app/services/IChocolateyPackageService.cs index 701908ede6..8ddfde5395 100644 --- a/src/chocolatey/infrastructure.app/services/IChocolateyPackageService.cs +++ b/src/chocolatey/infrastructure.app/services/IChocolateyPackageService.cs @@ -35,9 +35,8 @@ public interface IChocolateyPackageService /// Lists/searches for packages that meet a search criteria /// /// The configuration. - /// Should results be logged? /// - IEnumerable list_run(ChocolateyConfiguration config, bool logResults); + IEnumerable list_run(ChocolateyConfiguration config); /// /// Run pack in noop mode diff --git a/src/chocolatey/infrastructure.app/services/INugetService.cs b/src/chocolatey/infrastructure.app/services/INugetService.cs index 29371dbc57..ee7b77b125 100644 --- a/src/chocolatey/infrastructure.app/services/INugetService.cs +++ b/src/chocolatey/infrastructure.app/services/INugetService.cs @@ -35,7 +35,7 @@ public interface INugetService /// The configuration. /// Should results be logged? /// - IEnumerable list_run(ChocolateyConfiguration config, bool logResults); + IEnumerable list_run(ChocolateyConfiguration config); /// /// Run pack in noop mode. diff --git a/src/chocolatey/infrastructure.app/services/NugetService.cs b/src/chocolatey/infrastructure.app/services/NugetService.cs index 5a150681d6..e43685091a 100644 --- a/src/chocolatey/infrastructure.app/services/NugetService.cs +++ b/src/chocolatey/infrastructure.app/services/NugetService.cs @@ -71,30 +71,29 @@ public void list_noop(ChocolateyConfiguration config) )); } - public IEnumerable list_run(ChocolateyConfiguration config, bool logResults) + public IEnumerable list_run(ChocolateyConfiguration config) { - foreach (var package in NugetList.GetPackages(config, _nugetLogger)) + int count = 0; + foreach (var pkg in NugetList.GetPackages(config, _nugetLogger)) { - var pkg = package; // for lamda access - if (logResults) + var package = pkg; // for lamda access + if (!config.Quiet) { - if (config.RegularOutput) - { - this.Log().Info(config.Verbose ? ChocolateyLoggers.Important : ChocolateyLoggers.Normal, () => "{0} {1}".format_with(pkg.Id, pkg.Version.to_string())); - if (config.Verbose) this.Log().Info(() => " {0}{1} Description: {2}{1} Tags: {3}{1} Number of Downloads: {4}{1}".format_with(pkg.Title.escape_curly_braces(), Environment.NewLine, pkg.Description.escape_curly_braces(), pkg.Tags.escape_curly_braces(), pkg.DownloadCount <= 0 ? "n/a" : pkg.DownloadCount.to_string())); - // Maintainer(s):{3}{1} | pkg.Owners.join(", ") - null at the moment - } - else - { - this.Log().Info(config.Verbose ? ChocolateyLoggers.Important : ChocolateyLoggers.Normal, () => "{0}|{1}".format_with(pkg.Id, pkg.Version.to_string())); - } + this.Log().Info(config.Verbose ? ChocolateyLoggers.Important : ChocolateyLoggers.Normal, () => "{0} {1}".format_with(package.Id, package.Version.to_string())); + if (config.RegularOutput && config.Verbose) this.Log().Info(() => " {0}{1} Description: {2}{1} Tags: {3}{1} Number of Downloads: {4}{1}".format_with(package.Title.escape_curly_braces(), Environment.NewLine, package.Description.escape_curly_braces(), package.Tags.escape_curly_braces(), package.DownloadCount <= 0 ? "n/a" : package.DownloadCount.to_string())); } else { - this.Log().Debug(() => "{0} {1}".format_with(pkg.Id, pkg.Version.to_string())); + this.Log().Debug(() => "{0} {1}".format_with(package.Id, package.Version.to_string())); } + count++; - yield return new PackageResult(pkg, null, config.Sources); + yield return new PackageResult(package, null, config.Sources); + } + + if (config.RegularOutput) + { + this.Log().Warn(() => @"{0} packages {1}.".format_with(count, config.ListCommand.LocalOnly ? "installed" : "found")); } } @@ -890,8 +889,9 @@ private void set_package_names_if_all_is_specified(ChocolateyConfiguration confi config.PackageNames = string.Empty; var input = config.Input; config.Input = string.Empty; - - config.PackageNames = list_run(config, false).Select(p => p.Name).@join(ApplicationParameters.PackageNamesSeparator); + config.Quiet = true; + + config.PackageNames = list_run(config).Select(p => p.Name).@join(ApplicationParameters.PackageNamesSeparator); config.Input = input; config.Noop = noop; config.Prerelease = pre;