-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ReadmeUriTemplateResource for download README from remote sources. (
#6136) * Added resource type for loading from server * Add loading message * Add unit tests * Fix comment, add check for null * remove call to GetReadmeUrl * remove unused using * update test to reflect new resource provider count * add nullable enabled * Added comments * Fixes from PR * Switch load * Pr comments * Ensure error loading is false when starting to load readme * Rename ErrorLoadingReadme to ErrorWithReadme
- Loading branch information
Showing
15 changed files
with
323 additions
and
42 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
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
39 changes: 39 additions & 0 deletions
39
src/NuGet.Core/NuGet.Protocol/Providers/ReadmeUriTemplateResourceProvider.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,39 @@ | ||
// 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. | ||
#nullable enable | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NuGet.Protocol.Core.Types; | ||
|
||
namespace NuGet.Protocol | ||
{ | ||
/// <summary>NuGet.Protocol resource provider for <see cref="ReadmeUriTemplateResource"/>.</summary> | ||
/// <remarks>When successful, returns an instance of <see cref="ReadmeUriTemplateResource"/>.</remarks> | ||
internal class ReadmeUriTemplateResourceProvider : ResourceProvider | ||
{ | ||
public ReadmeUriTemplateResourceProvider() | ||
: base(typeof(ReadmeUriTemplateResource), | ||
nameof(ReadmeUriTemplateResource), | ||
NuGetResourceProviderPositions.Last) | ||
{ | ||
} | ||
|
||
/// <inheritdoc cref="ResourceProvider.TryCreate(SourceRepository, CancellationToken)"/> | ||
public override async Task<Tuple<bool, INuGetResource?>> TryCreate(SourceRepository source, CancellationToken token) | ||
{ | ||
ReadmeUriTemplateResource? resource = null; | ||
var serviceIndex = await source.GetResourceAsync<ServiceIndexResourceV3>(token); | ||
if (serviceIndex != null) | ||
{ | ||
var uriTemplate = serviceIndex.GetServiceEntryUri(ServiceTypes.ReadmeFileUrl)?.OriginalString; | ||
|
||
// construct a new resource | ||
resource = string.IsNullOrWhiteSpace(uriTemplate) ? null : new ReadmeUriTemplateResource(uriTemplate); | ||
} | ||
|
||
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
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
52 changes: 52 additions & 0 deletions
52
src/NuGet.Core/NuGet.Protocol/Resources/ReadmeUriTemplateResource.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,52 @@ | ||
// 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. | ||
|
||
using NuGet.Protocol.Core.Types; | ||
using NuGet.Versioning; | ||
|
||
#if NETCOREAPP | ||
using System; | ||
#endif | ||
|
||
namespace NuGet.Protocol | ||
{ | ||
/// <summary> | ||
/// A resource that provides the URI for downloading a README file based on a template. | ||
/// </summary> | ||
internal class ReadmeUriTemplateResource : INuGetResource | ||
{ | ||
private readonly string _uriTemplate; | ||
private const string LowerId = "{lower_id}"; | ||
private const string LowerVersion = "{lower_version}"; | ||
|
||
public ReadmeUriTemplateResource(string uriTemplate) | ||
{ | ||
_uriTemplate = uriTemplate; | ||
} | ||
|
||
/// <summary> | ||
/// Get the URL for downloading the readme file. | ||
/// </summary> | ||
/// <param name="id">The package id</param> | ||
/// <param name="version">The package version</param> | ||
/// <returns>URL to download README, built using the URI template.</returns> | ||
public string GetReadmeUrl(string id, NuGetVersion version) | ||
{ | ||
if (_uriTemplate == null) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
var uriString = _uriTemplate | ||
#if NETCOREAPP | ||
.Replace(LowerId, id.ToLowerInvariant(), StringComparison.OrdinalIgnoreCase) | ||
.Replace(LowerVersion, version.ToNormalizedString().ToLowerInvariant(), StringComparison.OrdinalIgnoreCase); | ||
#else | ||
.Replace(LowerId, id.ToLowerInvariant()) | ||
.Replace(LowerVersion, version.ToNormalizedString().ToLowerInvariant()); | ||
#endif | ||
|
||
return uriString; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.