Skip to content

Commit

Permalink
Merge pull request NuGet#31 from AdmiringWorm/find-packages-capabilities
Browse files Browse the repository at this point in the history
(NuGet#9) Check whether FindPackagesById is supported for a feed
  • Loading branch information
gep13 authored Mar 14, 2023
2 parents f23ed88 + a59e93e commit d79e7c5
Show file tree
Hide file tree
Showing 18 changed files with 308 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Chocolatey-NuGet.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"src\\NuGet.Core\\NuGet.ProjectModel\\NuGet.ProjectModel.csproj",
"src\\NuGet.Core\\NuGet.Resolver\\NuGet.Resolver.csproj",
"src\\NuGet.Core\\NuGet.Versioning\\NuGet.Versioning.csproj",
"src\\NuGet.Core\\NuGet.Credential\\NuGet.Credentials.csproj",
"src\\NuGet.Core\\NuGet.Credentials\\NuGet.Credentials.csproj",
"src\\NuGet.Core\\NuGet.LibraryModel\\NuGet.LibraryModel.csproj",
"test\\NuGet.Core.Tests\\NuGet.Commands.Test\\NuGet.Commands.Test.csproj",
"test\\NuGet.Core.Tests\\NuGet.Common.Test\\NuGet.Common.Test.csproj",
Expand All @@ -35,4 +35,4 @@
"test\\TestUtilities\\Test.Utility\\Test.Utility.csproj"
]
}
}
}
10 changes: 10 additions & 0 deletions src/NuGet.Core/NuGet.Protocol/ILegacyFeedCapabilityResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,15 @@ public interface ILegacyFeedCapabilityResource
{
Task<bool> SupportsIsAbsoluteLatestVersionAsync(ILogger log, CancellationToken token);
Task<bool> SupportsSearchAsync(ILogger log, CancellationToken token);

//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

Task<bool> SupportsFindPackagesByIdAsync(ILogger log, CancellationToken token);

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
}
}
33 changes: 33 additions & 0 deletions src/NuGet.Core/NuGet.Protocol/LegacyFeed/ChocolateyV2FeedParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
//////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using NuGet.Common;
using NuGet.Protocol.Core.Types;

namespace NuGet.Protocol
{
Expand Down Expand Up @@ -44,6 +49,34 @@ public sealed partial class V2FeedParser : IV2FeedParser
private static readonly XName _xnamePackageScanFlagResult = XName.Get("PackageScanFlagResult", DataServicesNS);
#pragma warning restore IDE1006 // Naming Styles

public Task<IReadOnlyList<V2FeedPackageInfo>> GetPackageVersionsAsync(string id, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
{
return GetPackageVersionsAsync(id, includeUnlisted: true, includePreRelease: true, sourceCacheContext: sourceCacheContext, log: log, token: token);
}

public async Task<IReadOnlyList<V2FeedPackageInfo>> GetPackageVersionsAsync(string id, bool includeUnlisted, bool includePreRelease, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
{
var filter = new SearchFilter(includePreRelease, null)
{
ExactPackageId = true,
IncludeDelisted = includeUnlisted,
OrderBy = SearchOrderBy.Version
};

var uri = _queryBuilder.BuildGetPackagesUri(id, filter, null, null);

var packages = await QueryV2FeedAsync(
uri,
id,
max: -1,
ignoreNotFounds: true,
sourceCacheContext: sourceCacheContext,
log: log,
token: token);

return packages.Items;
}


/// <summary>
/// Retrieve an XML <see cref="DateTime"/> value safely
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ public class DependencyInfoResourceV2Feed : DependencyInfoResource
private readonly FrameworkReducer _frameworkReducer = new FrameworkReducer();
private readonly SourceRepository _source;

public DependencyInfoResourceV2Feed(V2FeedParser feedParser, SourceRepository source)
//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

private readonly ILegacyFeedCapabilityResource _feedCapabilities;

public DependencyInfoResourceV2Feed(V2FeedParser feedParser, ILegacyFeedCapabilityResource feedCapabilities, SourceRepository source)
{
if (feedParser == null)
{
Expand All @@ -37,8 +43,13 @@ public DependencyInfoResourceV2Feed(V2FeedParser feedParser, SourceRepository so

_feedParser = feedParser;
_source = source;
_feedCapabilities = feedCapabilities;
}

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

public override async Task<SourcePackageDependencyInfo> ResolvePackage(
PackageIdentity package,
NuGetFramework projectFramework,
Expand Down Expand Up @@ -78,7 +89,13 @@ public override async Task<IEnumerable<SourcePackageDependencyInfo>> ResolvePack

try
{
var packages = await _feedParser.FindPackagesByIdAsync(packageId, sourceCacheContext, log, token);
//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
var packages = await FindPackageById(packageId, includeUnlisted: true, includePrerelease: true, sourceCacheContext, log, token);
//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

var results = new List<SourcePackageDependencyInfo>();

Expand Down Expand Up @@ -112,7 +129,7 @@ public override async Task<IEnumerable<SourcePackageDependencyInfo>> ResolvePack

try
{
var packages = await _feedParser.FindPackagesByIdAsync(packageId, true, includePrerelease, sourceCacheContext, log, token);
var packages = await FindPackageById(packageId, true, includePrerelease, sourceCacheContext, log, token);

var results = new List<SourcePackageDependencyInfo>();

Expand Down Expand Up @@ -170,5 +187,25 @@ private SourcePackageDependencyInfo CreateDependencyInfo(

return result;
}

//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

private async Task<IReadOnlyList<V2FeedPackageInfo>> FindPackageById(string packageId, bool includeUnlisted, bool includePrerelease, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
{
if (await _feedCapabilities.SupportsFindPackagesByIdAsync(log, token))
{
return await _feedParser.FindPackagesByIdAsync(packageId, includeUnlisted, includePrerelease, sourceCacheContext, log, token);
}
else
{
return await _feedParser.GetPackageVersionsAsync(packageId, includeUnlisted, includePrerelease, sourceCacheContext, log, token);
}
}

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,17 @@ public override async Task<Tuple<bool, INuGetResource>> TryCreate(SourceReposito
var httpSource = await source.GetResourceAsync<HttpSourceResource>(token);
var parser = new V2FeedParser(httpSource.HttpSource, serviceDocument.BaseAddress, source.PackageSource.Source);

resource = new DependencyInfoResourceV2Feed(parser, source);
//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

var feedCapabilityResource = new LegacyFeedCapabilityResourceV2Feed(parser,
serviceDocument.BaseAddress);
resource = new DependencyInfoResourceV2Feed(parser, feedCapabilityResource, source);

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
}

return new Tuple<bool, INuGetResource>(resource != null, resource);
Expand Down
25 changes: 25 additions & 0 deletions src/NuGet.Core/NuGet.Protocol/LegacyFeed/IV2FeedParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

using System.Collections.Generic;

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
Expand All @@ -25,5 +34,21 @@ Task<V2FeedPage> GetSearchPageAsync(
int take,
ILogger log,
CancellationToken token);

//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

Task<IReadOnlyList<V2FeedPackageInfo>> GetPackageVersionsAsync(
string id,
bool includeUnlisted,
bool includePreRelease,
SourceCacheContext sourceCacheContext,
ILogger log,
CancellationToken token);

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ public override async Task<bool> SupportsSearchAsync(ILogger log, CancellationTo
return capabilities.SupportsSearch;
}

//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

public override async Task<bool> SupportsFindPackagesByIdAsync(ILogger log, CancellationToken token)
{
var capabilities = await GetCachedCapabilitiesAsync(log, token);

return capabilities.SupportsFindPackageById;
}

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

private async Task<Capabilities> GetCachedCapabilitiesAsync(ILogger log, CancellationToken token)
{
var task = CachedCapabilities.GetOrAdd(
Expand Down Expand Up @@ -87,6 +102,22 @@ private async Task<Capabilities> GetCapabilitiesAsync(string metadataUri, ILogge
capabilities.SupportsSearch = metadata
.SupportedMethodNames
.Contains("Search");

//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

capabilities.SupportsFindPackageById = metadata
.SupportedMethodNames
.Contains("FindPackagesById");

capabilities.SupportsGetUpdates = metadata
.SupportedMethodNames
.Contains("GetUpdates");

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
}
catch
{
Expand Down Expand Up @@ -114,6 +145,18 @@ private async Task<Capabilities> GetCapabilitiesAsync(string metadataUri, ILogge
private class Capabilities
{
public string MetadataUri { get; set; }

//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

public bool SupportsFindPackageById { get; set; }
public bool SupportsGetUpdates { get; set; }

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

public bool SupportsIsAbsoluteLatestVersion { get; set; }
public bool SupportsSearch { get; set; }
}
Expand Down
41 changes: 39 additions & 2 deletions src/NuGet.Core/NuGet.Protocol/LegacyFeed/MetadataResourceV2Feed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ public class MetadataResourceV2Feed : MetadataResource
private readonly V2FeedParser _feedParser;
private readonly SourceRepository _source;

public MetadataResourceV2Feed(V2FeedParser feedParser, SourceRepository source)
//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

private readonly ILegacyFeedCapabilityResource _feedCapabilities;

public MetadataResourceV2Feed(V2FeedParser feedParser, ILegacyFeedCapabilityResource feedCapabilities, SourceRepository source)
{
if (feedParser == null)
{
Expand All @@ -28,8 +34,13 @@ public MetadataResourceV2Feed(V2FeedParser feedParser, SourceRepository source)

_feedParser = feedParser;
_source = source;
_feedCapabilities = feedCapabilities;
}

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

public override async Task<IEnumerable<KeyValuePair<string, NuGetVersion>>> GetLatestVersions(IEnumerable<string> packageIds, bool includePrerelease, bool includeUnlisted,
SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
{
Expand Down Expand Up @@ -71,7 +82,13 @@ public override async Task<IEnumerable<NuGetVersion>> GetVersions(string package

try
{
var packages = await _feedParser.FindPackagesByIdAsync(packageId, includeUnlisted, includePrerelease, sourceCacheContext, log, token);
//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
var packages = await FindPackageById(packageId, includeUnlisted, includePrerelease, sourceCacheContext, log, token);
//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

return packages.Select(p => p.Version).ToArray();
}
Expand Down Expand Up @@ -105,5 +122,25 @@ public override async Task<bool> Exists(string packageId, bool includePrerelease

return versions.Any();
}

//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

private async Task<IReadOnlyList<V2FeedPackageInfo>> FindPackageById(string packageId, bool includeUnlisted, bool includePrerelease, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
{
if (await _feedCapabilities.SupportsFindPackagesByIdAsync(log, token))
{
return await _feedParser.FindPackagesByIdAsync(packageId, includeUnlisted, includePrerelease, sourceCacheContext, log, token);
}
else
{
return await _feedParser.GetPackageVersionsAsync(packageId, includeUnlisted, includePrerelease, sourceCacheContext, log, token);
}
}

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ public override async Task<Tuple<bool, INuGetResource>> TryCreate(SourceReposito
var httpSource = await source.GetResourceAsync<HttpSourceResource>(token);
var parser = new V2FeedParser(httpSource.HttpSource, serviceDocument.BaseAddress, source.PackageSource.Source);

resource = new MetadataResourceV2Feed(parser, source);
//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

var feedCapabilityResource = new LegacyFeedCapabilityResourceV2Feed(parser, serviceDocument.BaseAddress);
resource = new MetadataResourceV2Feed(parser, feedCapabilityResource, source);

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
}

return new Tuple<bool, INuGetResource>(resource != null, resource);
Expand Down
Loading

0 comments on commit d79e7c5

Please sign in to comment.