Skip to content

Commit

Permalink
Start on getting list working
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCakeIsNaOH committed Jul 1, 2022
1 parent 3f8bf90 commit c11032b
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 49 deletions.
109 changes: 75 additions & 34 deletions src/chocolatey/infrastructure.app/nuget/NugetList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,30 @@ namespace chocolatey.infrastructure.app.nuget

public static class NugetList
{
public static IEnumerable<IPackage> GetPackages(ChocolateyConfiguration configuration, ILogger nugetLogger)
public static IEnumerable<IPackageSearchMetadata> GetPackages(ChocolateyConfiguration configuration, ILogger nugetLogger)
{
return execute_package_search(configuration, nugetLogger);
}

public static int GetCount(ChocolateyConfiguration configuration, ILogger nugetLogger)
{
return execute_package_search(configuration, nugetLogger).Count();
return 0;
//return execute_package_search(configuration, nugetLogger).Count();
}

private static IQueryable<IPackage> execute_package_search(ChocolateyConfiguration configuration, ILogger nugetLogger)
{
var packageRepository = NugetCommon.GetRemoteRepository(configuration, nugetLogger, new PackageDownloader());
var searchTermLower = configuration.Input.to_lower();

//private static void execute_package_search(ChocolateyConfiguration configuration, ILogger nugetLogger)
private static IQueryable<IPackageSearchMetadata> execute_package_search(ChocolateyConfiguration configuration, ILogger nugetLogger)
{
var packageRepositories = NugetCommon.GetRemoteRepository(configuration, nugetLogger);
var packageRepositoriesResources = NugetCommon.GetRepositoryResources(packageRepositories);
//var searchResources = NugetCommon.GetRepositoryResource<PackageSearchResource>(packageRepositories);
//var findResources = NugetCommon.GetRepositoryResource<FindPackageByIdResource>(packageRepositories);
string searchTermLower = configuration.Input.to_lower();
SearchFilter searchFilter = new SearchFilter(configuration.Prerelease);
searchFilter.IncludeDelisted = configuration.ListCommand.LocalOnly;

/*
// Whether or not the package is remote determines two things:
// 1. Does the repository have a notion of "listed"?
// 2. Does it support prerelease in a straight-forward way?
Expand All @@ -67,27 +76,68 @@ private static IQueryable<IPackage> execute_package_search(ChocolateyConfigurati
{
isServiceBased = packageRepository is IServiceBasedRepository;
}
*/

SemanticVersion version = !string.IsNullOrWhiteSpace(configuration.Version) ? new SemanticVersion(SemanticVersion.Parse(configuration.Version)) : null;
IQueryable<IPackage> results;
NuGetVersion version = !string.IsNullOrWhiteSpace(configuration.Version) ? NuGetVersion.Parse(configuration.Version) : null;
//SemanticVersion version = !string.IsNullOrWhiteSpace(configuration.Version) ? new SemanticVersion(SemanticVersion.Parse(configuration.Version)) : null;
List <IPackageSearchMetadata> results = new List<IPackageSearchMetadata>();

if (!configuration.ListCommand.Exact)
{
results = packageRepository.Search(searchTermLower, configuration.Prerelease);
foreach (var repositoryResources in packageRepositoriesResources)
{
if (repositoryResources.listResource != null)
{
var tempResults = repositoryResources.listResource.ListAsync(searchTermLower, configuration.Prerelease, true, false, nugetLogger, CancellationToken.None).GetAwaiter().GetResult();
var enumerator = tempResults.GetEnumeratorAsync();

results.AddRange(Task.Run(async () =>
{
var loopResults = new List<IPackageSearchMetadata>();
while (await enumerator.MoveNextAsync())
{
loopResults.Add(enumerator.Current);
}
return loopResults;
}).GetAwaiter().GetResult());
}
else
{
var skipNumber = 0;
var takeNumber = configuration.ListCommand.PageSize;
searchTermLower = string.IsNullOrWhiteSpace(searchTermLower) ? "*" : searchTermLower;
IEnumerable<IPackageSearchMetadata> partResults;
do
{
partResults = repositoryResources.searchResource.SearchAsync(searchTermLower, searchFilter, skipNumber, takeNumber, nugetLogger, CancellationToken.None).GetAwaiter().GetResult();
skipNumber += takeNumber;
results.AddRange(partResults);
} while (partResults.Any());
}
}


//TODO - deduplicate package ids
//TODO - skip page numbers

//results = packageRepository.Search(searchTermLower, configuration.Prerelease);
}
else
{
if (configuration.AllVersions)
{
/*
// convert from a search to getting packages by id.
// search based on lower case id - similar to PackageRepositoryExtensions.FindPackagesByIdCore()
results = packageRepository.GetPackages().Where(p => p.Id.ToLower() == searchTermLower)
results = packageRepository.GetPackages().Where(p => p.Identity.Id.ToLower() == searchTermLower)
.AsEnumerable()
.Where(p => configuration.Prerelease || p.IsReleaseVersion())
.AsQueryable();
*/
}
else
{
/*
var exactPackage = find_package(searchTermLower, version, configuration, packageRepository);
if (exactPackage == null) return new List<IPackage>().AsQueryable();
Expand All @@ -96,50 +146,47 @@ private static IQueryable<IPackage> execute_package_search(ChocolateyConfigurati
{
exactPackage
}.AsQueryable();
*/
}
}

/*
if (configuration.ListCommand.Page.HasValue)
{
results = results.Skip(configuration.ListCommand.PageSize * configuration.ListCommand.Page.Value).Take(configuration.ListCommand.PageSize);
}
*/

if (configuration.ListCommand.ByIdOnly)
{
results = isServiceBased ?
results.Where(p => p.Id.ToLower().Contains(searchTermLower))
: results.Where(p => p.Id.contains(searchTermLower, StringComparison.OrdinalIgnoreCase));
results = results.Where(p => p.Identity.Id.ToLower().Contains(searchTermLower)).ToList();
}

if (configuration.ListCommand.ByTagOnly)
{
results = isServiceBased
? results.Where(p => p.Tags.Contains(searchTermLower))
: results.Where(p => p.Tags.contains(searchTermLower, StringComparison.InvariantCultureIgnoreCase));
results = results.Where(p => p.Tags.contains(searchTermLower, StringComparison.InvariantCultureIgnoreCase)).ToList();
}

if (configuration.ListCommand.IdStartsWith)
{
results = isServiceBased ?
results.Where(p => p.Id.ToLower().StartsWith(searchTermLower))
: results.Where(p => p.Id.StartsWith(searchTermLower, StringComparison.OrdinalIgnoreCase));
results = results.Where(p => p.Identity.Id.ToLower().StartsWith(searchTermLower)).ToList();
}

if (configuration.ListCommand.ApprovedOnly)
{
results = results.Where(p => p.IsApproved);
results = results.Where(p => p.IsApproved).ToList();
}

if (configuration.ListCommand.DownloadCacheAvailable)
{
results = results.Where(p => p.IsDownloadCacheAvailable);
results = results.Where(p => p.IsDownloadCacheAvailable).ToList();
}

if (configuration.ListCommand.NotBroken)
{
results = results.Where(p => (p.IsDownloadCacheAvailable && configuration.Information.IsLicensedVersion) || p.PackageTestResultStatus != "Failing");
results = results.Where(p => (p.IsDownloadCacheAvailable && configuration.Information.IsLicensedVersion) || p.PackageTestResultStatus != "Failing").ToList();
}

/*
if (configuration.AllVersions || !string.IsNullOrWhiteSpace(configuration.Version))
{
if (isServiceBased)
Expand All @@ -152,15 +199,6 @@ private static IQueryable<IPackage> execute_package_search(ChocolateyConfigurati
}
}
if (configuration.Prerelease && packageRepository.SupportsPrereleasePackages)
{
results = results.Where(p => p.IsAbsoluteLatestVersion);
}
else
{
results = results.Where(p => p.IsLatestVersion);
}

if (!isServiceBased)
{
results =
Expand All @@ -170,14 +208,16 @@ private static IQueryable<IPackage> execute_package_search(ChocolateyConfigurati
.distinct_last(PackageEqualityComparer.Id, PackageComparer.Version)
.AsQueryable();
}
*/

results = configuration.ListCommand.OrderByPopularity ?
results.OrderByDescending(p => p.DownloadCount).ThenBy(p => p.Id)
results.OrderByDescending(p => p.DownloadCount).ThenBy(p => p.Identity.Id).ToList()
: results;

return results;
return results.AsQueryable();
}

/*
/// <summary>
/// Searches for packages that are available based on name and other options
/// </summary>
Expand Down Expand Up @@ -272,6 +312,7 @@ public static IPackage find_package(string packageName, SemanticVersion version,
return results.ToList().OrderByDescending(x => x.Version).FirstOrDefault();
}
*/
}

// ReSharper restore InconsistentNaming
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,20 @@ public virtual IEnumerable<PackageResult> list_run(ChocolateyConfiguration confi

var packages = new List<IPackageSearchMetadata>();

foreach (var package in perform_source_runner_function(config, r => r.list_run(config)))
foreach (PackageResult package in perform_source_runner_function(config, r => r.list_run(config)))
{
if (config.SourceType == SourceType.normal)
{
yield return package;

if (!config.ListCommand.IncludeRegistryPrograms)
{
yield return package;
//yield return package;
}

if (config.ListCommand.LocalOnly && config.ListCommand.IncludeRegistryPrograms && package.Package != null)
if (config.ListCommand.LocalOnly && config.ListCommand.IncludeRegistryPrograms && package.SearchMetadata != null)
{
packages.Add(package.Package);
//packages.Add(package.Package);
}
}
}
Expand Down
36 changes: 25 additions & 11 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public virtual void list_noop(ChocolateyConfiguration config)

public virtual IEnumerable<PackageResult> list_run(ChocolateyConfiguration config)
{
//return new PackageResult[0];
int count = 0;

var sources = config.Sources;
Expand All @@ -137,9 +138,16 @@ public virtual IEnumerable<PackageResult> list_run(ChocolateyConfiguration confi
{
var package = pkg; // for lamda access

ManifestMetadata localPackage;
var localSearchMetadata = package as LocalPackageSearchMetadata;
if (localSearchMetadata != null)
{
//new ManifestMetadata() localSearchMetadata.PackagePath
}

if (!string.IsNullOrWhiteSpace(config.Version))
{
if (!pkg.Version.to_string().is_equal_to(config.Version)) continue;
if (!pkg.Identity.Version.to_string().is_equal_to(config.Version)) continue;
}

if (config.ListCommand.LocalOnly)
Expand All @@ -162,8 +170,8 @@ public virtual IEnumerable<PackageResult> list_run(ChocolateyConfiguration confi

if (config.RegularOutput)
{
this.Log().Info(logger, () => "{0}{1}".format_with(package.Id, config.ListCommand.IdOnly ? string.Empty : " {0}{1}{2}{3}".format_with(
package.Version.to_string(),
this.Log().Info(logger, () => "{0}{1}".format_with(package.Identity.Id, config.ListCommand.IdOnly ? string.Empty : " {0}{1}{2}{3}".format_with(
package.Identity.Version.to_string(),
package.IsApproved ? " [Approved]" : string.Empty,
package.IsDownloadCacheAvailable ? " Downloads cached for licensed users" : string.Empty,
package.PackageTestResultStatus == "Failing" && package.IsDownloadCacheAvailable ? " - Possibly broken for FOSS users (due to original download location changes by vendor)" : package.PackageTestResultStatus == "Failing" ? " - Possibly broken" : string.Empty
Expand Down Expand Up @@ -194,7 +202,8 @@ Package url
),
package.DownloadCount <= 0 ? "n/a" : package.DownloadCount.to_string(),
package.VersionDownloadCount <= 0 ? "n/a" : package.VersionDownloadCount.to_string(),
package.PackageSourceUrl != null && !string.IsNullOrWhiteSpace(package.PackageSourceUrl.to_string()) ? package.PackageSourceUrl.to_string() : "n/a",
//package.PackageSourceUrl != null && !string.IsNullOrWhiteSpace(package.PackageSourceUrl.to_string()) ? package.PackageSourceUrl.to_string() : "n/a",
"N/A",
string.IsNullOrWhiteSpace(package.PackageHash) ? string.Empty : "{0} Package Checksum: '{1}' ({2})".format_with(
Environment.NewLine,
package.PackageHash,
Expand All @@ -203,23 +212,28 @@ Package url
package.Tags.trim_safe().escape_curly_braces(),
package.ProjectUrl != null ? package.ProjectUrl.to_string() : "n/a",
package.LicenseUrl != null && !string.IsNullOrWhiteSpace(package.LicenseUrl.to_string()) ? package.LicenseUrl.to_string() : "n/a",
package.ProjectSourceUrl != null && !string.IsNullOrWhiteSpace(package.ProjectSourceUrl.to_string()) ? "{0} Software Source: {1}".format_with(Environment.NewLine, package.ProjectSourceUrl.to_string()) : string.Empty,
package.DocsUrl != null && !string.IsNullOrWhiteSpace(package.DocsUrl.to_string()) ? "{0} Documentation: {1}".format_with(Environment.NewLine, package.DocsUrl.to_string()) : string.Empty,
package.MailingListUrl != null && !string.IsNullOrWhiteSpace(package.MailingListUrl.to_string()) ? "{0} Mailing List: {1}".format_with(Environment.NewLine, package.MailingListUrl.to_string()) : string.Empty,
package.BugTrackerUrl != null && !string.IsNullOrWhiteSpace(package.BugTrackerUrl.to_string()) ? "{0} Issues: {1}".format_with(Environment.NewLine, package.BugTrackerUrl.to_string()) : string.Empty,
//package.ProjectSourceUrl != null && !string.IsNullOrWhiteSpace(package.ProjectSourceUrl.to_string()) ? "{0} Software Source: {1}".format_with(Environment.NewLine, package.ProjectSourceUrl.to_string()) : string.Empty,
string.Empty,
//package.DocsUrl != null && !string.IsNullOrWhiteSpace(package.DocsUrl.to_string()) ? "{0} Documentation: {1}".format_with(Environment.NewLine, package.DocsUrl.to_string()) : string.Empty,
string.Empty,
//package.MailingListUrl != null && !string.IsNullOrWhiteSpace(package.MailingListUrl.to_string()) ? "{0} Mailing List: {1}".format_with(Environment.NewLine, package.MailingListUrl.to_string()) : string.Empty,
string.Empty,
//package.BugTrackerUrl != null && !string.IsNullOrWhiteSpace(package.BugTrackerUrl.to_string()) ? "{0} Issues: {1}".format_with(Environment.NewLine, package.BugTrackerUrl.to_string()) : string.Empty,
string.Empty,
package.Summary != null && !string.IsNullOrWhiteSpace(package.Summary.to_string()) ? "{0} Summary: {1}".format_with(Environment.NewLine, package.Summary.escape_curly_braces().to_string()) : string.Empty,
package.Description.escape_curly_braces().Replace("\n ", "\n").Replace("\n", "\n "),
package.ReleaseNotes != null && !string.IsNullOrWhiteSpace(package.ReleaseNotes.to_string()) ? "{0} Release Notes: {1}".format_with(Environment.NewLine, package.ReleaseNotes.escape_curly_braces().Replace("\n ", "\n").Replace("\n", "\n ")) : string.Empty
//package.ReleaseNotes != null && !string.IsNullOrWhiteSpace(package.ReleaseNotes.to_string()) ? "{0} Release Notes: {1}".format_with(Environment.NewLine, package.ReleaseNotes.escape_curly_braces().Replace("\n ", "\n").Replace("\n", "\n ")) : string.Empty
string.Empty
));
}
else
{
this.Log().Info(logger, () => "{0}{1}".format_with(package.Id, config.ListCommand.IdOnly ? string.Empty : "|{0}".format_with(package.Version.to_string())));
this.Log().Info(logger, () => "{0}{1}".format_with(package.Identity.Id, config.ListCommand.IdOnly ? string.Empty : "|{0}".format_with(package.Identity.Version.to_string())));
}
}
else
{
this.Log().Debug(() => "{0}{1}".format_with(package.Id, config.ListCommand.IdOnly ? string.Empty : " {0}".format_with(package.Version.to_string())));
this.Log().Debug(() => "{0}{1}".format_with(package.Identity.Id, config.ListCommand.IdOnly ? string.Empty : " {0}".format_with(package.Identity.Version.to_string())));
}
count++;

Expand Down

0 comments on commit c11032b

Please sign in to comment.