Skip to content

Commit

Permalink
(GH-132) PackageService / ListCommand List for API
Browse files Browse the repository at this point in the history
Ensure that PackageService and List command are able to list output for API
purposes.
  • Loading branch information
Jaykul authored and ferventcoder committed Jun 29, 2015
1 parent 73ba21f commit ff4a1f0
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
<Compile Include="infrastructure\commandline\ExitScenarioHandler.cs" />
<Compile Include="infrastructure\commandline\InteractivePrompt.cs" />
<Compile Include="infrastructure\commands\ICommandExecutor.cs" />
<Compile Include="infrastructure\commands\IListCommand.cs" />
<Compile Include="infrastructure\commands\PowershellExecutor.cs" />
<Compile Include="infrastructure\cryptography\CryptoHashProviderType.cs" />
<Compile Include="infrastructure\adapters\HashAlgorithm.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ namespace chocolatey.infrastructure.app.commands
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 @@ -117,6 +118,11 @@ public void run(ChocolateyConfiguration configuration)
_packageService.list_run(configuration, logResults: true);
}

public IEnumerable<PackageResult> list(ChocolateyConfiguration configuration)
{
return _packageService.list_run(configuration, logResults: false);
}

public bool may_require_admin_access()
{
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void list_noop(ChocolateyConfiguration config)
}
}

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

Expand All @@ -83,23 +83,32 @@ public void list_run(ChocolateyConfiguration config, bool logResults)
//install webpi if not installed
//run the webpi command
this.Log().Warn("Command not yet functional, stay tuned...");
return new PackageResult[]{};
}
else
{
var list = _nugetService.list_run(config, logResults: true);
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"));
this.Log().Warn(() => @"{0} packages {1}.".format_with(list.Count, config.ListCommand.LocalOnly ? "installed" : "found"));
}
if (!config.ListCommand.LocalOnly && !config.ListCommand.IncludeRegistryPrograms)
{
return list;
}

if (config.ListCommand.LocalOnly && config.ListCommand.IncludeRegistryPrograms)
{
report_registry_programs(config, list);
}
// in this case, we need to a list we can enumerate multiple times and append to

if (config.ListCommand.LocalOnly && config.ListCommand.IncludeRegistryPrograms)
{
report_registry_programs(config, list);
}

return list;
}
}

private void report_registry_programs(ChocolateyConfiguration config, IEnumerable<PackageResult> list)
private void report_registry_programs(ChocolateyConfiguration config, List<PackageResult> list)
{
var itemsToRemoveFromMachine = new List<string>();
foreach (var packageResult in list)
Expand All @@ -111,23 +120,34 @@ private void report_registry_programs(ChocolateyConfiguration config, IEnumerabl
{
continue;
}

var key = pkginfo.RegistrySnapshot.RegistryKeys.FirstOrDefault();
if (key != null)
{
itemsToRemoveFromMachine.Add(key.DisplayName);
}
}
}

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)
{
this.Log().Info(() => "");
foreach (var key in machineInstalled.or_empty_list_if_null())
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().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));
}
this.Log().Warn(() => @"{0} applications not managed with Chocolatey.".format_with(machineInstalled.Count));
}
}

Expand Down Expand Up @@ -708,4 +728,4 @@ private void remove_rollback_if_exists(PackageResult packageResult)
_nugetService.remove_rollback_directory_if_exists(packageResult.Name);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
namespace chocolatey.infrastructure.app.services
{
using System.Collections.Concurrent;
using System.Collections.Generic;
using configuration;
using results;

Expand All @@ -36,7 +37,7 @@ public interface IChocolateyPackageService
/// <param name="config">The configuration.</param>
/// <param name="logResults">Should results be logged?</param>
/// <returns></returns>
void list_run(ChocolateyConfiguration config, bool logResults);
IEnumerable<PackageResult> list_run(ChocolateyConfiguration config, bool logResults);

/// <summary>
/// Run pack in noop mode
Expand Down
2 changes: 1 addition & 1 deletion src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public IEnumerable<PackageResult> list_run(ChocolateyConfiguration config, bool
this.Log().Debug(() => "{0} {1}".format_with(pkg.Id, pkg.Version.to_string()));
}

yield return new PackageResult(pkg, null);
yield return new PackageResult(pkg, null, config.Sources);
}
if (config.RegularOutput) this.Log().Debug(() => "--- End of List ---");
}
Expand Down
25 changes: 25 additions & 0 deletions src/chocolatey/infrastructure/commands/IListCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright © 2011 - Present RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace chocolatey.infrastructure.commands
{
using System.Collections.Generic;
using app.configuration;

public interface IListCommand<T> : ICommand
{
IEnumerable<T> list(ChocolateyConfiguration configuration);
}
}
32 changes: 29 additions & 3 deletions src/chocolatey/infrastructure/results/PackageResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

namespace chocolatey.infrastructure.results
{
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using NuGet;

/// <summary>
Expand All @@ -37,17 +39,41 @@ public bool Warning
public string Version { get; private set; }
public IPackage Package { get; private set; }
public string InstallLocation { get; set; }
public string Source { get; set; }
public string SourceUri { get; set; }

public PackageResult(IPackage package, string installLocation) : this(package.Id.to_lower(), package.Version.to_string(), installLocation)
public PackageResult(IPackage package, string installLocation, string source = null) : this(package.Id.to_lower(), package.Version.to_string(), installLocation)
{
Package = package;
Source = source;
var sources = new List<Uri>();
if (!string.IsNullOrEmpty(source))
{
sources.AddRange(source.Split(new[] {";", ","}, StringSplitOptions.RemoveEmptyEntries).Select(s => new Uri(s)));
}

var rp = Package as DataServicePackage;
if (rp != null)
{
SourceUri = rp.DownloadUrl.ToString();
Source = sources.FirstOrDefault(uri => uri.IsBaseOf(rp.DownloadUrl)).to_string();
if (string.IsNullOrEmpty(Source))
{
Source = sources.FirstOrDefault(uri => uri.DnsSafeHost == rp.DownloadUrl.DnsSafeHost).to_string();
}
}
else
{
Source = sources.FirstOrDefault(uri => uri.IsFile || uri.IsUnc).to_string();
}
}

public PackageResult(string name, string version, string installLocation)
public PackageResult(string name, string version, string installLocation, string source = null)
{
Name = name;
Version = version;
InstallLocation = installLocation;
Source = source;
}
}
}

0 comments on commit ff4a1f0

Please sign in to comment.