Skip to content

Commit

Permalink
Merge branch 'stable'
Browse files Browse the repository at this point in the history
* stable:
  (GH-223) Fix handling of LocalOnly for API
  (GH-132) Use Config.QuietOutput for List commands
  (GH-132) PackageService / ListCommand List for API

Conflicts:
	src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs
	src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs
	src/chocolatey/infrastructure.app/services/INugetService.cs
	src/chocolatey/infrastructure.app/services/NugetService.cs
	src/chocolatey/infrastructure/commands/IListCommand.cs
  • Loading branch information
ferventcoder committed Jun 29, 2015
2 parents 79ed3af + 3c12763 commit 909ab18
Show file tree
Hide file tree
Showing 17 changed files with 196 additions and 123 deletions.
Expand Up @@ -233,7 +233,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);
}
}
}
Expand Down
Expand Up @@ -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<ChocolateyConfiguration>(), true)).Returns(packageResults);
nugetService.Setup(n => n.list_run(It.IsAny<ChocolateyConfiguration>())).Returns(packageResults);
configuration.PinCommand.Command = PinCommandType.list;
}

Expand Down Expand Up @@ -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<ChocolateyConfiguration>(), true), Times.Once);
nugetService.Verify(n => n.list_run(It.IsAny<ChocolateyConfiguration>()), Times.Once);
}

[Pending("NuGet is killing me with extension methods. Need to find proper item to mock out to return the package object.")]
Expand Down
Expand Up @@ -16,17 +16,19 @@
namespace chocolatey.infrastructure.app.commands
{
using System.Collections.Generic;
using System.Linq;
using attributes;
using commandline;
using configuration;
using domain;
using infrastructure.commands;
using logging;
using results;
using services;

[CommandFor(CommandNameType.list)]
[CommandFor(CommandNameType.search)]
public sealed class ChocolateyListCommand : ICommand
public sealed class ChocolateyListCommand : IListCommand<PackageResult>
{
private readonly IChocolateyPackageService _packageService;

Expand Down Expand Up @@ -66,12 +68,6 @@ public void configure_argument_parser(OptionSet optionSet, ChocolateyConfigurati
public void handle_additional_argument_parsing(IList<string> unparsedArguments, ChocolateyConfiguration configuration)
{
configuration.Input = string.Join(" ", unparsedArguments);

if (configuration.ListCommand.LocalOnly)
{
configuration.Sources = ApplicationParameters.PackagesLocation;
configuration.Prerelease = true;
}
}

public void handle_validation(ChocolateyConfiguration configuration)
Expand Down Expand Up @@ -115,7 +111,15 @@ public void noop(ChocolateyConfiguration configuration)
public void run(ChocolateyConfiguration configuration)
{
_packageService.ensure_source_app_installed(configuration);
_packageService.list_run(configuration, logResults: true);
// note: you must leave the .ToList() here or else the method won't be evaluated!
_packageService.list_run(configuration).ToList();
}

public IEnumerable<PackageResult> list(ChocolateyConfiguration configuration)
{
configuration.QuietOutput = true;
// here it's up to the caller to enumerate the results
return _packageService.list_run(configuration);
}

public bool may_require_admin_access()
Expand Down
Expand Up @@ -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)
Expand Down
Expand Up @@ -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
{
Expand Down
Expand Up @@ -153,7 +153,22 @@ 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 look into using mutually exclusive output levels - Debug, Info (Regular), Error (Quiet)
// Verbose and Important are not part of the levels at all
/// <summary>
/// Gets or sets a value indicating whether output should be limited.
/// This supports the --limit-output parameter.
/// </summary>
/// <value><c>true</c> for regular output; <c>false</c> for limited output.</value>
public bool RegularOutput { get; set; }
/// <summary>
/// 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.
/// </summary>
/// <value><c>true</c> for no output; <c>false</c> for regular or limited output.</value>
/// <remarks>This has only been implemented for NuGet List</remarks>
public bool QuietOutput { get; set; }
public bool PromptForConfirmation { get; set; }
public bool AcceptLicense { get; set; }
public bool AllowUnofficialBuild { get; set; }
Expand Down
110 changes: 62 additions & 48 deletions src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs
Expand Up @@ -22,13 +22,14 @@ namespace chocolatey.infrastructure.app.services
using commandline;
using configuration;
using domain;
using filesystem;
using infrastructure.commands;
using infrastructure.services;
using logging;
using NuGet;
using platforms;
using results;
using tolerance;
using IFileSystem = filesystem.IFileSystem;

public class ChocolateyPackageService : IChocolateyPackageService
{
Expand All @@ -44,9 +45,9 @@ public class ChocolateyPackageService : IChocolateyPackageService
private readonly IXmlService _xmlService;
private readonly IConfigTransformService _configTransformService;

public ChocolateyPackageService(INugetService nugetService, IPowershellService powershellService,
IEnumerable<ISourceRunner> sourceRunners, IShimGenerationService shimgenService,
IFileSystem fileSystem, IRegistryService registryService,
public ChocolateyPackageService(INugetService nugetService, IPowershellService powershellService,
IEnumerable<ISourceRunner> sourceRunners, IShimGenerationService shimgenService,
IFileSystem fileSystem, IRegistryService registryService,
IChocolateyPackageInformationService packageInfoService, IFilesService filesService,
IAutomaticUninstallerService autoUninstallerService, IXmlService xmlService,
IConfigTransformService configTransformService)
Expand Down Expand Up @@ -99,55 +100,68 @@ public void list_noop(ChocolateyConfiguration config)
perform_source_runner_action(config, r => r.list_noop(config));
}

public void list_run(ChocolateyConfiguration config, bool logResults)
public IEnumerable<PackageResult> list_run(ChocolateyConfiguration config)
{
this.Log().Debug(() => "Searching for package information");

var list = perform_source_runner_function(config, r => r.list_run(config, logResults));
var packages = new List<IPackage>();

if (config.SourceType == SourceType.normal)
foreach (var package in perform_source_runner_function(config, r => r.list_run(config)))
{
if (config.RegularOutput)
if (config.SourceType == SourceType.normal)
{
this.Log().Warn(() => @"{0} packages {1}.".format_with(list.Count(), config.ListCommand.LocalOnly ? "installed" : "found"));
if (!config.ListCommand.IncludeRegistryPrograms)
{
yield return package;
}

if (config.ListCommand.LocalOnly && config.ListCommand.IncludeRegistryPrograms)
if (config.ListCommand.LocalOnly && config.ListCommand.IncludeRegistryPrograms && package.Package != null)
{
report_registry_programs(config, list);
packages.Add(package.Package);
}
}
}
}

private void report_registry_programs(ChocolateyConfiguration config, IEnumerable<PackageResult> list)
{
var itemsToRemoveFromMachine = new List<string>();
foreach (var packageResult in list)
if (config.RegularOutput)
{
if (packageResult != null && packageResult.Package != null)
if (config.ListCommand.LocalOnly && config.ListCommand.IncludeRegistryPrograms)
{
var pkginfo = _packageInfoService.get_package_information(packageResult.Package);
if (pkginfo.RegistrySnapshot == null)
{
continue;
}
var key = pkginfo.RegistrySnapshot.RegistryKeys.FirstOrDefault();
if (key != null)
foreach (var installed in report_registry_programs(config, packages))
{
itemsToRemoveFromMachine.Add(key.DisplayName);
yield return installed;
}
}
}
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)
}

private IEnumerable<PackageResult> report_registry_programs(ChocolateyConfiguration config, IEnumerable<IPackage> 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)
{
this.Log().Info(() => "");
foreach (var key in machineInstalled.or_empty_list_if_null())
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()));
}
this.Log().Warn(() => @"{0} applications not managed with Chocolatey.".format_with(machineInstalled.Count));
count++;

yield return new PackageResult(key.DisplayName, key.DisplayName, key.InstallLocation);
}

if (config.RegularOutput)
{
this.Log().Warn(() => @"{0} applications not managed with Chocolatey.".format_with(count));
}
}

Expand Down Expand Up @@ -227,8 +241,8 @@ public void handle_package_result(PackageResult packageResult, ChocolateyConfigu
var powerShellRan = _powershellService.install(config, packageResult);
if (powerShellRan)
{
// we don't care about the exit code
if (config.Information.PlatformType == PlatformType.Windows) CommandExecutor.execute_static("shutdown", "/a", config.CommandExecutionTimeoutSeconds, _fileSystem.get_current_directory(), (s, e) => { }, (s, e) => { }, false, false);
// we don't care about the exit code
if (config.Information.PlatformType == PlatformType.Windows) CommandExecutor.execute_static("shutdown", "/a", config.CommandExecutionTimeoutSeconds, _fileSystem.get_current_directory(), (s, e) => { }, (s, e) => { }, false, false);
}

var difference = _registryService.get_differences(before, _registryService.get_installer_keys());
Expand Down Expand Up @@ -263,7 +277,7 @@ public void handle_package_result(PackageResult packageResult, ChocolateyConfigu
{
handle_extension_packages(config, packageResult);
}

_packageInfoService.save_package_information(pkgInfo);
ensure_bad_package_path_is_clean(config, packageResult);

Expand All @@ -277,7 +291,7 @@ public void handle_package_result(PackageResult packageResult, ChocolateyConfigu

remove_rollback_if_exists(packageResult);

this.Log().Info(ChocolateyLoggers.Important, " The {0} of {1} was successful.".format_with( commandName.to_string(), packageResult.Name));
this.Log().Info(ChocolateyLoggers.Important, " The {0} of {1} was successful.".format_with(commandName.to_string(), packageResult.Name));
}

public ConcurrentDictionary<string, PackageResult> install_run(ChocolateyConfiguration config)
Expand All @@ -296,7 +310,7 @@ public void handle_package_result(PackageResult packageResult, ChocolateyConfigu
action = (packageResult) => handle_package_result(packageResult, packageConfig, CommandNameType.install);
}
var results = perform_source_runner_function(packageConfig, r => r.install_run(packageConfig, action));

foreach (var result in results)
{
packageInstalls.GetOrAdd(result.Key, result.Value);
Expand All @@ -312,7 +326,7 @@ public void handle_package_result(PackageResult packageResult, ChocolateyConfigu
packageInstalls.Count,
installFailures,
installWarnings == 0 ? string.Empty : "{0} {1} package(s) had warnings.".format_with(Environment.NewLine, installWarnings),
_fileSystem.combine_paths(ApplicationParameters.LoggingLocation,ApplicationParameters.LoggingFile)
_fileSystem.combine_paths(ApplicationParameters.LoggingLocation, ApplicationParameters.LoggingFile)
));

if (installWarnings != 0)
Expand Down Expand Up @@ -367,7 +381,7 @@ public void outdated_run(ChocolateyConfiguration config)
config.RegularOutput = false;
var oudatedPackages = _nugetService.upgrade_noop(config, null);
config.RegularOutput = output;

if (config.RegularOutput)
{
var upgradeWarnings = oudatedPackages.Count(p => p.Value.Warning);
Expand Down Expand Up @@ -546,16 +560,16 @@ public void uninstall_noop(ChocolateyConfiguration config)
this.Log().Info(@"Uninstalling the following packages:");
this.Log().Info(ChocolateyLoggers.Important, @"{0}".format_with(config.PackageNames));

foreach (var packageConfigFile in config.PackageNames.Split(new[] { ApplicationParameters.PackageNamesSeparator }, StringSplitOptions.RemoveEmptyEntries).or_empty_list_if_null().Where(p => p.EndsWith(".config")).ToList())
if (config.PackageNames.Split(new[] { ApplicationParameters.PackageNamesSeparator }, StringSplitOptions.RemoveEmptyEntries).or_empty_list_if_null().Any(p => p.EndsWith(".config")))
{
throw new ApplicationException("A packages.config file is only used with installs.");
}


Action<PackageResult> action = null;
if (config.SourceType == SourceType.normal)
{
action = (packageResult) => handle_package_uninstall(packageResult, config);
}
}

var packageUninstalls = perform_source_runner_function(config, r => r.uninstall_run(config, action));

Expand Down Expand Up @@ -600,11 +614,11 @@ public void handle_package_uninstall(PackageResult packageResult, ChocolateyConf
_powershellService.uninstall(config, packageResult);
}

if (packageResult.Success)
{
if (packageResult.Success)
{
_autoUninstallerService.run(packageResult, config);
}
}

// we don't care about the exit code
if (config.Information.PlatformType == PlatformType.Windows) CommandExecutor.execute_static("shutdown", "/a", config.CommandExecutionTimeoutSeconds, _fileSystem.get_current_directory(), (s, e) => { }, (s, e) => { }, false, false);

Expand Down Expand Up @@ -632,7 +646,7 @@ private void uninstall_cleanup(ChocolateyConfiguration config, PackageResult pac
ensure_bad_package_path_is_clean(config, packageResult);
remove_rollback_if_exists(packageResult);
handle_extension_packages(config, packageResult);

if (config.Force)
{
var packageDirectory = _fileSystem.combine_paths(packageResult.InstallLocation);
Expand Down Expand Up @@ -741,14 +755,14 @@ private void move_bad_package_to_failure_location(PackageResult packageResult)
{
FaultTolerance.try_catch_with_logging_exception(
() => _fileSystem.move_directory(packageResult.InstallLocation, packageResult.InstallLocation.Replace(ApplicationParameters.PackagesLocation, ApplicationParameters.PackageFailuresLocation)),
"Could not move bad package to failure directory It will show as installed.{0} {1}{0} The error".format_with(Environment.NewLine,packageResult.InstallLocation));
"Could not move bad package to failure directory It will show as installed.{0} {1}{0} The error".format_with(Environment.NewLine, packageResult.InstallLocation));
}
}

private void rollback_previous_version(ChocolateyConfiguration config, PackageResult packageResult)
{
if (packageResult.InstallLocation == null) return;

var rollbackDirectory = packageResult.InstallLocation.Replace(ApplicationParameters.PackagesLocation, ApplicationParameters.PackageBackupLocation);
if (!_fileSystem.directory_exists(rollbackDirectory))
{
Expand Down Expand Up @@ -784,4 +798,4 @@ private void remove_rollback_if_exists(PackageResult packageResult)
_nugetService.remove_rollback_directory_if_exists(packageResult.Name);
}
}
}
}

0 comments on commit 909ab18

Please sign in to comment.