Skip to content

Commit

Permalink
#51 - Add checkbox on Browser tab to include prerelease packages in s…
Browse files Browse the repository at this point in the history
…earch results. Refactor package content download into separate service.
  • Loading branch information
maraf committed Dec 18, 2018
1 parent 7c91b59 commit 9df3eab
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 66 deletions.
7 changes: 4 additions & 3 deletions src/PackageManager.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ private UpdatesViewModel CreateUpdatesViewModel()
{
var log = new DefaultLog();

var contentService = new NuGetPackageContentService(log);
var packageSources = new NuGetPackageSourceCollection(new PackageSourceProvider(new Settings(Environment.CurrentDirectory)));
var packageSourceSelector = new AllPackageSourceSelector(packageSources);
var versionService = new NuGetPackageVersionService(log);
var versionService = new NuGetPackageVersionService(contentService, log);

var repositoryFactory = new NuGetSourceRepositoryFactory();
var installService = new NuGetInstallService(repositoryFactory, log, Args.Path, versionService);
var searchService = new NuGetSearchService(repositoryFactory, log, versionService);
var installService = new NuGetInstallService(repositoryFactory, log, Args.Path, contentService, versionService);
var searchService = new NuGetSearchService(repositoryFactory, log, contentService, versionService);
var selfPackageConfiguration = new SelfPackageConfiguration(Args.SelfPackageId);
var selfUpdateService = new SelfUpdateService(this, new ProcessService(this, Array.Empty<string>()));

Expand Down
34 changes: 8 additions & 26 deletions src/PackageManager.NuGet/Models/NuGetPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ namespace PackageManager.Models
public class NuGetPackage : NuGetPackageIdentity, IPackage
{
private readonly IPackageSearchMetadata source;
private readonly ILog log;
private readonly ILogger nuGetLog;
private readonly bool isPrereleaseIncluded;
private readonly SourceRepository repository;
private readonly NuGetPackageContentService contentService;
private readonly NuGetPackageVersionService versionService;
private readonly NuGetPackageContent.IFrameworkFilter frameworkFilter;

public string Description => source.Description;

Expand All @@ -34,41 +33,24 @@ public class NuGetPackage : NuGetPackageIdentity, IPackage
public Uri ProjectUrl => source.ProjectUrl;
public Uri LicenseUrl => source.LicenseUrl;

public NuGetPackage(IPackageSearchMetadata source, SourceRepository repository, ILog log, NuGetPackageVersionService versionService, NuGetPackageContent.IFrameworkFilter frameworkFilter = null)
public NuGetPackage(IPackageSearchMetadata source, bool isPrereleaseIncluded, SourceRepository repository, NuGetPackageContentService contentService, NuGetPackageVersionService versionService)
: base(source?.Identity)
{
Ensure.NotNull(source, "source");
Ensure.NotNull(repository, "repository");
Ensure.NotNull(log, "log");
Ensure.NotNull(contentService, "contentService");
Ensure.NotNull(versionService, "versionService");
this.source = source;
this.isPrereleaseIncluded = isPrereleaseIncluded;
this.repository = repository;
this.contentService = contentService;
this.versionService = versionService;
this.log = log.Factory.Scope("Package");
this.nuGetLog = new NuGetLogger(this.log);
this.frameworkFilter = frameworkFilter;
}

public async Task<IPackageContent> GetContentAsync(CancellationToken cancellationToken)
{
DownloadResource download = await repository.GetResourceAsync<DownloadResource>();
if (download == null)
throw Ensure.Exception.InvalidOperation($"Unnable to resolve '{nameof(DownloadResource)}'.");

using (var sourceCacheContext = new SourceCacheContext())
{
var context = new PackageDownloadContext(sourceCacheContext, Path.GetTempPath(), true);
var result = await download.GetDownloadResourceResultAsync(source.Identity, context, String.Empty, nuGetLog, cancellationToken);
if (result.Status == DownloadResourceResultStatus.Cancelled)
throw new OperationCanceledException();
else if (result.Status == DownloadResourceResultStatus.NotFound)
throw Ensure.Exception.InvalidOperation($"Package '{source.Identity.Id}-v{source.Identity.Version}' not found");

return new NuGetPackageContent(new PackageArchiveReader(result.PackageStream), log, frameworkFilter);
}
}
=> await contentService.DownloadAsync(repository, source, cancellationToken);

public async Task<IEnumerable<IPackage>> GetVersionsAsync(CancellationToken cancellationToken)
=> await versionService.GetListAsync(Int32.MaxValue, source, repository, cancellationToken: cancellationToken);
=> await versionService.GetListAsync(Int32.MaxValue, source, repository, isPrereleaseIncluded: isPrereleaseIncluded, cancellationToken: cancellationToken);
}
}
7 changes: 5 additions & 2 deletions src/PackageManager.NuGet/Services/NuGetInstallService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class NuGetInstallService : IInstallService
{
private readonly IFactory<SourceRepository, IPackageSource> repositoryFactory;
private readonly ILog log;
private readonly NuGetPackageContentService contentService;
private readonly NuGetPackageVersionService versionService;
private readonly ILogger nuGetLog;
private readonly INuGetPackageFilter packageFilter;
Expand All @@ -31,16 +32,18 @@ public class NuGetInstallService : IInstallService
public string Path { get; }
public string ConfigFilePath => System.IO.Path.Combine(Path, "packages.config");

public NuGetInstallService(IFactory<SourceRepository, IPackageSource> repositoryFactory, ILog log, string path, NuGetPackageVersionService versionService, INuGetPackageFilter packageFilter = null, NuGetPackageContent.IFrameworkFilter frameworkFilter = null)
public NuGetInstallService(IFactory<SourceRepository, IPackageSource> repositoryFactory, ILog log, string path, NuGetPackageContentService contentService, NuGetPackageVersionService versionService, INuGetPackageFilter packageFilter = null, NuGetPackageContent.IFrameworkFilter frameworkFilter = null)
{
Ensure.NotNull(repositoryFactory, "repositoryFactory");
Ensure.NotNull(log, "log");
Ensure.NotNull(path, "path");
Ensure.NotNull(contentService, "contentService");
Ensure.NotNull(versionService, "versionService");
this.repositoryFactory = repositoryFactory;
this.log = log;
this.nuGetLog = new NuGetLogger(log);
Path = path;
this.contentService = contentService;
this.versionService = versionService;
this.frameworkFilter = frameworkFilter;

Expand Down Expand Up @@ -161,7 +164,7 @@ await ReadPackageConfig(

NuGetPackageFilterResult filterResult = packageFilter.IsPassed(metadata);
result.Add(new NuGetInstalledPackage(
new NuGetPackage(metadata, repository, log, versionService, frameworkFilter),
new NuGetPackage(metadata, false, repository, contentService, versionService),
filterResult == NuGetPackageFilterResult.Ok
));
break;
Expand Down
54 changes: 54 additions & 0 deletions src/PackageManager.NuGet/Services/NuGetPackageContentService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Neptuo;
using Neptuo.Logging;
using NuGet.Common;
using NuGet.Packaging;
using NuGet.Protocol.Core.Types;
using PackageManager.Logging;
using PackageManager.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace PackageManager.Services
{
public class NuGetPackageContentService
{
private readonly ILog log;
private readonly ILogger nuGetLog;
private readonly NuGetPackageContent.IFrameworkFilter frameworkFilter;

public NuGetPackageContentService(ILog log, NuGetPackageContent.IFrameworkFilter frameworkFilter = null)
{
Ensure.NotNull(log, "log");
this.log = log.Factory.Scope("Package");
this.nuGetLog = new NuGetLogger(this.log);
this.frameworkFilter = frameworkFilter;
}

public async Task<IPackageContent> DownloadAsync(SourceRepository repository, IPackageSearchMetadata package, CancellationToken cancellationToken)
{
Ensure.NotNull(repository, "repository");
Ensure.NotNull(package, "package");

DownloadResource download = await repository.GetResourceAsync<DownloadResource>();
if (download == null)
throw Ensure.Exception.InvalidOperation($"Unnable to resolve '{nameof(DownloadResource)}'.");

using (var sourceCacheContext = new SourceCacheContext())
{
var context = new PackageDownloadContext(sourceCacheContext, Path.GetTempPath(), true);
var result = await download.GetDownloadResourceResultAsync(package.Identity, context, String.Empty, nuGetLog, cancellationToken);
if (result.Status == DownloadResourceResultStatus.Cancelled)
throw new OperationCanceledException();
else if (result.Status == DownloadResourceResultStatus.NotFound)
throw Ensure.Exception.InvalidOperation($"Package '{package.Identity.Id}-v{package.Identity.Version}' not found");

return new NuGetPackageContent(new PackageArchiveReader(result.PackageStream), log, frameworkFilter);
}
}
}
}
26 changes: 15 additions & 11 deletions src/PackageManager.NuGet/Services/NuGetPackageVersionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ public class NuGetPackageVersionService
{
private readonly INuGetPackageFilter filter;
private readonly NuGetPackageContent.IFrameworkFilter frameworkFilter;
private readonly NuGetPackageContentService contentService;
private readonly ILog log;
private readonly ILogger nuGetLog;

public NuGetPackageVersionService(ILog log, INuGetPackageFilter filter = null, NuGetPackageContent.IFrameworkFilter frameworkFilter = null)
public NuGetPackageVersionService(NuGetPackageContentService contentService, ILog log, INuGetPackageFilter filter = null, NuGetPackageContent.IFrameworkFilter frameworkFilter = null)
{
Ensure.NotNull(contentService, "contentService");
Ensure.NotNull(log, "log");
this.contentService = contentService;
this.log = log;
this.nuGetLog = new NuGetLogger(log);

Expand All @@ -34,18 +37,18 @@ public NuGetPackageVersionService(ILog log, INuGetPackageFilter filter = null, N
this.frameworkFilter = frameworkFilter;
}

public async Task<IReadOnlyList<IPackage>> GetListAsync(int resultCount, IPackageSearchMetadata package, SourceRepository repository, Func<IPackageSearchMetadata, IPackageSearchMetadata, bool> versionFilter = null, CancellationToken cancellationToken = default)
public async Task<IReadOnlyList<IPackage>> GetListAsync(int resultCount, IPackageSearchMetadata package, SourceRepository repository, Func<IPackageSearchMetadata, IPackageSearchMetadata, bool> versionFilter = null, bool isPrereleaseIncluded = false, CancellationToken cancellationToken = default)
{
if (versionFilter == null)
versionFilter = (source, target) => true;

try
{
List<IPackage> result = new List<IPackage>();
if (await SearchOlderVersionsDirectly(result, resultCount, package, repository, versionFilter))
if (await SearchOlderVersionsDirectly(result, resultCount, package, repository, versionFilter, isPrereleaseIncluded))
return result;

if (await SearchOlderVersionsUsingMetadataResource(result, resultCount, package, repository, versionFilter, cancellationToken))
if (await SearchOlderVersionsUsingMetadataResource(result, resultCount, package, repository, versionFilter, isPrereleaseIncluded, cancellationToken))
return result;

return new List<IPackage>();
Expand All @@ -57,15 +60,16 @@ public async Task<IReadOnlyList<IPackage>> GetListAsync(int resultCount, IPackag
}
}

private async Task<bool> SearchOlderVersionsDirectly(List<IPackage> result, int resultCount, IPackageSearchMetadata package, SourceRepository repository, Func<IPackageSearchMetadata, IPackageSearchMetadata, bool> versionFilter)
private async Task<bool> SearchOlderVersionsDirectly(List<IPackage> result, int resultCount, IPackageSearchMetadata package, SourceRepository repository, Func<IPackageSearchMetadata, IPackageSearchMetadata, bool> versionFilter, bool isPrereleaseIncluded)
{
bool isSuccess = false;
IEnumerable<VersionInfo> versions = await package.GetVersionsAsync();
foreach (VersionInfo version in versions)
{
// TODO: Filter prelease on V2 feed.
if (version.PackageSearchMetadata != null && versionFilter(package, version.PackageSearchMetadata))
{
IPackage item = ProcessOlderVersion(repository, version.PackageSearchMetadata);
IPackage item = ProcessOlderVersion(repository, version.PackageSearchMetadata, isPrereleaseIncluded);
if (item != null)
{
result.Add(item);
Expand All @@ -80,7 +84,7 @@ private async Task<bool> SearchOlderVersionsDirectly(List<IPackage> result, int
return isSuccess;
}

private async Task<bool> SearchOlderVersionsUsingMetadataResource(List<IPackage> result, int resultCount, IPackageSearchMetadata package, SourceRepository repository, Func<IPackageSearchMetadata, IPackageSearchMetadata, bool> versionFilter, CancellationToken cancellationToken)
private async Task<bool> SearchOlderVersionsUsingMetadataResource(List<IPackage> result, int resultCount, IPackageSearchMetadata package, SourceRepository repository, Func<IPackageSearchMetadata, IPackageSearchMetadata, bool> versionFilter, bool isPrereleaseIncluded, CancellationToken cancellationToken)
{
PackageMetadataResource metadataResource = await repository.GetResourceAsync<PackageMetadataResource>(cancellationToken);
if (metadataResource == null)
Expand All @@ -90,7 +94,7 @@ private async Task<bool> SearchOlderVersionsUsingMetadataResource(List<IPackage>
{
IEnumerable<IPackageSearchMetadata> versions = await metadataResource?.GetMetadataAsync(
package.Identity.Id,
false,
isPrereleaseIncluded,
false,
sourceCacheContext,
nuGetLog,
Expand All @@ -102,7 +106,7 @@ private async Task<bool> SearchOlderVersionsUsingMetadataResource(List<IPackage>
{
if (versionFilter(package, version))
{
IPackage item = ProcessOlderVersion(repository, version);
IPackage item = ProcessOlderVersion(repository, version, isPrereleaseIncluded);
if (item != null)
{
result.Add(item);
Expand All @@ -116,7 +120,7 @@ private async Task<bool> SearchOlderVersionsUsingMetadataResource(List<IPackage>
return true;
}

private IPackage ProcessOlderVersion(SourceRepository repository, IPackageSearchMetadata version)
private IPackage ProcessOlderVersion(SourceRepository repository, IPackageSearchMetadata version, bool isPrereleaseIncluded)
{
log.Debug($"Found '{version.Identity}'.");

Expand All @@ -125,7 +129,7 @@ private IPackage ProcessOlderVersion(SourceRepository repository, IPackageSearch
{
case NuGetPackageFilterResult.Ok:
log.Debug("Package added.");
return new NuGetPackage(version, repository, log, this, frameworkFilter);
return new NuGetPackage(version, isPrereleaseIncluded, repository, contentService, this);

default:
log.Debug("Package skipped.");
Expand Down
Loading

0 comments on commit 9df3eab

Please sign in to comment.