Skip to content

Commit

Permalink
(GH-143) Make NuGet calls yield return, so we stream output as it comes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaykul committed Apr 5, 2015
1 parent 9adaa02 commit f78c5f7
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,12 @@ public override void Context()
configuration.Sources = ApplicationParameters.PackagesLocation;
configuration.ListCommand.LocalOnly = true;
configuration.AllVersions = true;
var packageResults = new ConcurrentDictionary<string, PackageResult>();
packageResults.GetOrAdd("regular", new PackageResult(package.Object, null));
packageResults.GetOrAdd("pinned", new PackageResult(pinnedPackage.Object, null));
nugetService.Setup(n => n.list_run(It.IsAny<ChocolateyConfiguration>(), false)).Returns(packageResults);
var packageResults = new []
{
new PackageResult(package.Object, null),
new PackageResult(pinnedPackage.Object, null)
};
nugetService.Setup(n => n.list_run(It.IsAny<ChocolateyConfiguration>(), true)).Returns(packageResults);
configuration.PinCommand.Command = PinCommandType.list;
}

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

This comment has been minimized.

Copy link
@Jaykul

Jaykul Apr 5, 2015

Author Contributor

This boolean had to be changed to force enumeration so the method would actually be called.

}

[Pending("NuGet is killing me with extension methods. Need to find proper item to mock out to return the package object.")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,9 @@ public void run(ChocolateyConfiguration configuration)

public void list_pins(IPackageManager packageManager, ChocolateyConfiguration config)
{
var localPackages = _nugetService.list_run(config, logResults: false);
foreach (var pkg in localPackages.or_empty_list_if_null())
foreach (var pkg in _nugetService.list_run(config, logResults: true))
{
var pkgInfo = _packageInfoService.get_package_information(pkg.Value.Package);
var pkgInfo = _packageInfoService.get_package_information(pkg.Package);
if (pkgInfo != null && pkgInfo.IsPinned)
{
this.Log().Info(() => "{0}|{1}".format_with(pkgInfo.Package.Id,pkgInfo.Package.Version));
Expand Down
16 changes: 9 additions & 7 deletions src/chocolatey/infrastructure.app/nuget/NugetList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace chocolatey.infrastructure.app.nuget

// ReSharper disable InconsistentNaming

public sealed class NugetList
public static class NugetList
{
public static IEnumerable<IPackage> GetPackages(ChocolateyConfiguration configuration, ILogger nugetLogger)
{
Expand All @@ -31,7 +31,8 @@ public static IEnumerable<IPackage> GetPackages(ChocolateyConfiguration configur

if (configuration.AllVersions)
{
return results.Where(PackageExtensions.IsListed).OrderBy(p => p.Id).ToList();
// just trust the server order, don't sort because that's a blocking operation
return results.Where(PackageExtensions.IsListed);
}

if (configuration.Prerelease && packageRepository.SupportsPrereleasePackages)
Expand All @@ -43,11 +44,12 @@ public static IEnumerable<IPackage> GetPackages(ChocolateyConfiguration configur
results = results.Where(p => p.IsLatestVersion);
}

return results.OrderBy(p => p.Id)
.AsEnumerable()
.Where(PackageExtensions.IsListed)
.Where(p => configuration.Prerelease || p.IsReleaseVersion())
.distinct_last(PackageEqualityComparer.Id, PackageComparer.Version).ToList();
// just trust the server order, don't sort because that's a blocking operation
// also don't worry about multiple versions, considering Is*LatestVersion only applies to one
return results.Where(PackageExtensions.IsListed)
.Where(p => configuration.Prerelease || p.IsReleaseVersion());
// .OrderBy(p => p.Id).ThenByDescending(p => p.Version)
// .GroupBy(p => p.Id).Select(g => g.First());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void list_run(ChocolateyConfiguration config, bool logResults)
var list = _nugetService.list_run(config, logResults: true);
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)
{
Expand All @@ -90,14 +90,14 @@ public void list_run(ChocolateyConfiguration config, bool logResults)
}
}

private void report_registry_programs(ChocolateyConfiguration config, ConcurrentDictionary<string, PackageResult> list)
private void report_registry_programs(ChocolateyConfiguration config, IEnumerable<PackageResult> list)
{
var itemsToRemoveFromMachine = new List<string>();
foreach (var packageResult in list)
{
if (packageResult.Value != null && packageResult.Value.Package != null)
if (packageResult != null && packageResult.Package != null)
{
var pkginfo = _packageInfoService.get_package_information(packageResult.Value.Package);
var pkginfo = _packageInfoService.get_package_information(packageResult.Package);
if (pkginfo.RegistrySnapshot == null)
{
continue;
Expand Down
3 changes: 2 additions & 1 deletion src/chocolatey/infrastructure.app/services/INugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace chocolatey.infrastructure.app.services
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using configuration;
using results;

Expand All @@ -34,7 +35,7 @@ public interface INugetService
/// <param name="config">The configuration.</param>
/// <param name="logResults">Should results be logged?</param>
/// <returns></returns>
ConcurrentDictionary<string, PackageResult> list_run(ChocolateyConfiguration config, bool logResults);
IEnumerable<PackageResult> list_run(ChocolateyConfiguration config, bool logResults);

/// <summary>
/// Run pack in noop mode.
Expand Down
27 changes: 10 additions & 17 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,36 +71,31 @@ public void list_noop(ChocolateyConfiguration config)
));
}

public ConcurrentDictionary<string, PackageResult> list_run(ChocolateyConfiguration config, bool logResults = true)
public IEnumerable<PackageResult> list_run(ChocolateyConfiguration config, bool logResults)
{
var packageResults = new ConcurrentDictionary<string, PackageResult>();

var packages = NugetList.GetPackages(config, _nugetLogger).ToList();

foreach (var package in packages.or_empty_list_if_null())
foreach (var package in NugetList.GetPackages(config, _nugetLogger))
{
var pkg = package; // for lamda access
if (logResults)
{
if (config.RegularOutput)
{
this.Log().Info(config.Verbose ? ChocolateyLoggers.Important : ChocolateyLoggers.Normal, () => "{0} {1}".format_with(package.Id, package.Version.to_string()));
if (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()));
// Maintainer(s):{3}{1} | package.Owners.join(", ") - null at the moment
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(package.Id, package.Version.to_string()));
this.Log().Info(config.Verbose ? ChocolateyLoggers.Important : ChocolateyLoggers.Normal, () => "{0}|{1}".format_with(pkg.Id, pkg.Version.to_string()));
}
}
else
{
this.Log().Debug(() => "{0} {1}".format_with(package.Id, package.Version.to_string()));
this.Log().Debug(() => "{0} {1}".format_with(pkg.Id, pkg.Version.to_string()));
}

packageResults.GetOrAdd(package.Id, new PackageResult(package, null));
yield return new PackageResult(pkg, null);
}

return packageResults;
}

public void pack_noop(ChocolateyConfiguration config)
Expand Down Expand Up @@ -896,13 +891,11 @@ private void set_package_names_if_all_is_specified(ChocolateyConfiguration confi
var input = config.Input;
config.Input = string.Empty;

var localPackages = list_run(config, logResults: false);

config.PackageNames = list_run(config, false).Select(p => p.Name).@join(ApplicationParameters.PackageNamesSeparator);
config.Input = input;
config.Noop = noop;
config.Prerelease = pre;
config.Sources = sources;
config.PackageNames = string.Join(ApplicationParameters.PackageNamesSeparator, localPackages.Select((p) => p.Key).or_empty_list_if_null());

if (customAction != null) customAction.Invoke();
}
Expand Down

0 comments on commit f78c5f7

Please sign in to comment.