-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add filtering for gallery and library packages
- Loading branch information
Showing
11 changed files
with
250 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NuGet.Common; | ||
using NuGet.Packaging.Core; | ||
using NuGet.Protocol; | ||
using NuGet.Protocol.Core.Types; | ||
|
||
namespace Bonsai.NuGet.Search | ||
{ | ||
internal class LocalPackageTypeSearchResource : PackageSearchResource | ||
{ | ||
readonly FindLocalPackagesResource findLocalPackagesResource; | ||
readonly Lazy<LocalPackageSearchResource> defaultSearchResource; | ||
|
||
public LocalPackageTypeSearchResource(FindLocalPackagesResource localResource) | ||
{ | ||
findLocalPackagesResource = localResource ?? throw new ArgumentNullException(nameof(localResource)); | ||
defaultSearchResource = new Lazy<LocalPackageSearchResource>( | ||
() => new LocalPackageSearchResource(findLocalPackagesResource)); | ||
} | ||
|
||
public override async Task<IEnumerable<IPackageSearchMetadata>> SearchAsync( | ||
string searchTerm, | ||
SearchFilter filters, | ||
int skip, | ||
int take, | ||
ILogger log, | ||
CancellationToken cancellationToken) | ||
{ | ||
LocalPackageSearchResource searchResource; | ||
if (filters?.PackageTypes != null | ||
&& filters.PackageTypes.SingleOrDefault() is string packageType) | ||
{ | ||
var wrapperResource = new FindLocalPackageWrapperResource(findLocalPackagesResource, packageType); | ||
searchResource = new LocalPackageSearchResource(wrapperResource); | ||
} | ||
else searchResource = defaultSearchResource.Value; | ||
|
||
return await searchResource.SearchAsync(searchTerm, filters, skip, take, log, cancellationToken); | ||
} | ||
|
||
class FindLocalPackageWrapperResource : FindLocalPackagesResource | ||
{ | ||
readonly FindLocalPackagesResource baseLocalResource; | ||
|
||
public FindLocalPackageWrapperResource(FindLocalPackagesResource localResource, string packageType) | ||
{ | ||
baseLocalResource = localResource ?? throw new ArgumentNullException(nameof(localResource)); | ||
Root = baseLocalResource.Root; | ||
PackageTypeFilter = packageType; | ||
} | ||
|
||
public string PackageTypeFilter { get; } | ||
|
||
private bool MatchesPackageType(LocalPackageInfo package) | ||
{ | ||
var packageTypes = package.Nuspec.GetPackageTypes(); | ||
if (packageTypes.Count == 0 | ||
&& PackageType.PackageTypeNameComparer.Equals(PackageTypeFilter, PackageType.Dependency.Name)) | ||
{ | ||
return true; | ||
} | ||
|
||
return package.Nuspec | ||
.GetPackageTypes() | ||
.Any(packageType => PackageType.PackageTypeNameComparer.Equals(packageType.Name, PackageTypeFilter)); | ||
} | ||
|
||
public override IEnumerable<LocalPackageInfo> FindPackagesById(string id, ILogger logger, CancellationToken token) | ||
{ | ||
return baseLocalResource.FindPackagesById(id, logger, token); | ||
} | ||
|
||
public override LocalPackageInfo GetPackage(Uri path, ILogger logger, CancellationToken token) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
public override LocalPackageInfo GetPackage(PackageIdentity identity, ILogger logger, CancellationToken token) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
public override IEnumerable<LocalPackageInfo> GetPackages(ILogger logger, CancellationToken token) | ||
{ | ||
var result = baseLocalResource.GetPackages(logger, token); | ||
if (!string.IsNullOrEmpty(PackageTypeFilter)) | ||
{ | ||
result = result.Where(MatchesPackageType); | ||
} | ||
return result; | ||
} | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Bonsai.NuGet/Search/LocalPackageTypeSearchResourceProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NuGet.Protocol; | ||
using NuGet.Protocol.Core.Types; | ||
|
||
namespace Bonsai.NuGet.Search | ||
{ | ||
internal class LocalPackageTypeSearchResourceProvider : ResourceProvider | ||
{ | ||
public LocalPackageTypeSearchResourceProvider() | ||
: base(typeof(PackageSearchResource), nameof(LocalPackageTypeSearchResourceProvider), nameof(LocalPackageSearchResourceProvider)) | ||
{ | ||
} | ||
|
||
public override async Task<Tuple<bool, INuGetResource>> TryCreate(SourceRepository source, CancellationToken token) | ||
{ | ||
INuGetResource resource = null; | ||
var localResource = await source.GetResourceAsync<FindLocalPackagesResource>(token); | ||
if (localResource != null) | ||
{ | ||
resource = new LocalPackageTypeSearchResource(localResource); | ||
} | ||
|
||
return new Tuple<bool, INuGetResource>(resource != null, resource); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NuGet.Common; | ||
using NuGet.Protocol; | ||
using NuGet.Protocol.Core.Types; | ||
|
||
namespace Bonsai.NuGet.Search | ||
{ | ||
internal class PackageTypeSearchResourceV3 : PackageSearchResource | ||
{ | ||
readonly PackageSearchResourceV3 packageSearchResource; | ||
|
||
public PackageTypeSearchResourceV3(PackageSearchResourceV3 searchResource) | ||
{ | ||
packageSearchResource = searchResource ?? throw new ArgumentNullException(nameof(searchResource)); | ||
} | ||
|
||
public override Task<IEnumerable<IPackageSearchMetadata>> SearchAsync(string searchTerm, SearchFilter filters, int skip, int take, ILogger log, CancellationToken cancellationToken) | ||
{ | ||
if (filters?.PackageTypes != null | ||
&& filters.PackageTypes.SingleOrDefault() is string packageType) | ||
{ | ||
searchTerm += "&packageType=" + packageType; | ||
} | ||
|
||
return packageSearchResource.SearchAsync(searchTerm, filters, skip, take, log, cancellationToken); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
Bonsai.NuGet/Search/PackageTypeSearchResourceV3Provider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NuGet.Protocol; | ||
using NuGet.Protocol.Core.Types; | ||
|
||
namespace Bonsai.NuGet.Search | ||
{ | ||
internal class PackageTypeSearchResourceV3Provider : ResourceProvider | ||
{ | ||
readonly PackageSearchResourceV3Provider searchResourceV3Provider = new(); | ||
|
||
public PackageTypeSearchResourceV3Provider() | ||
: base(typeof(PackageSearchResource), nameof(PackageTypeSearchResourceV3Provider), nameof(PackageSearchResourceV3Provider)) | ||
{ | ||
} | ||
|
||
public override async Task<Tuple<bool, INuGetResource>> TryCreate(SourceRepository source, CancellationToken token) | ||
{ | ||
INuGetResource resource = null; | ||
var result = await searchResourceV3Provider.TryCreate(source, token); | ||
if (result.Item1) | ||
{ | ||
var searchResource = (PackageSearchResourceV3)result.Item2; | ||
resource = new PackageTypeSearchResourceV3(searchResource); | ||
} | ||
|
||
return new Tuple<bool, INuGetResource>(resource != null, resource); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using NuGet.Protocol.Core.Types; | ||
|
||
namespace Bonsai.NuGet.Search | ||
{ | ||
internal static class Repository | ||
{ | ||
public static ProviderFactory Provider => ProviderFactory.Instance; | ||
|
||
public class ProviderFactory : global::NuGet.Protocol.Core.Types.Repository.ProviderFactory | ||
{ | ||
internal static readonly ProviderFactory Instance = new(); | ||
|
||
public override IEnumerable<Lazy<INuGetResourceProvider>> GetCoreV3() | ||
{ | ||
yield return new Lazy<INuGetResourceProvider>(() => new PackageTypeSearchResourceV3Provider()); | ||
yield return new Lazy<INuGetResourceProvider>(() => new LocalPackageTypeSearchResourceProvider()); | ||
foreach (var provider in base.GetCoreV3()) | ||
{ | ||
yield return provider; | ||
} | ||
} | ||
} | ||
} | ||
} |