Skip to content

Commit

Permalink
Support embedded READMEs (loic-sharma#670)
Browse files Browse the repository at this point in the history
NuGet's official embedded READMEs declares the README using the [`readme`](https://docs.microsoft.com/en-us/nuget/reference/nuspec#readme) metadata in the package's .nuspec.
  • Loading branch information
loic-sharma authored Aug 13, 2021
1 parent d5f7101 commit e60e916
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 24 deletions.
26 changes: 5 additions & 21 deletions src/BaGet.Core/Extensions/PackageArchiveReaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,8 @@ namespace BaGet.Core

public static class PackageArchiveReaderExtensions
{
private static readonly string[] OrderedReadmeFileNames = new[]
{
"readme.md",
"readme.txt",
};

private static readonly HashSet<string> ReadmeFileNames = new HashSet<string>(
OrderedReadmeFileNames,
StringComparer.OrdinalIgnoreCase);

public static bool HasReadme(this PackageArchiveReader package)
=> package.GetFiles().Any(ReadmeFileNames.Contains);
=> !string.IsNullOrEmpty(package.NuspecReader.GetReadme());

public static bool HasEmbeddedIcon(this PackageArchiveReader package)
=> !string.IsNullOrEmpty(package.NuspecReader.GetIcon());
Expand All @@ -33,19 +23,13 @@ public async static Task<Stream> GetReadmeAsync(
this PackageArchiveReader package,
CancellationToken cancellationToken)
{
var packageFiles = package.GetFiles();

foreach (var readmeFileName in OrderedReadmeFileNames)
var readmePath = package.NuspecReader.GetReadme();
if (readmePath == null)
{
var readmePath = packageFiles.FirstOrDefault(f => f.Equals(readmeFileName, StringComparison.OrdinalIgnoreCase));

if (readmePath != null)
{
return await package.GetStreamAsync(readmePath, cancellationToken);
}
throw new InvalidOperationException("Package does not have a readme!");
}

throw new InvalidOperationException("Package does not have a readme!");
return await package.GetStreamAsync(readmePath, cancellationToken);
}

public async static Task<Stream> GetIconAsync(
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<MicrosoftAspNetCorePackageVersion>3.1.1</MicrosoftAspNetCorePackageVersion>
<MicrosoftEntityFrameworkCorePackageVersion>3.1.1</MicrosoftEntityFrameworkCorePackageVersion>
<MicrosoftExtensionsPackageVersion>3.1.1</MicrosoftExtensionsPackageVersion>
<NuGetPackageVersion>5.4.0</NuGetPackageVersion>
<NuGetPackageVersion>5.10.0</NuGetPackageVersion>
</PropertyGroup>

<ItemGroup Condition="'$(DOTNET_RUNNING_IN_CONTAINER)' == ''">
Expand Down
2 changes: 1 addition & 1 deletion tests/BaGet.Tests/ApiIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public async Task PackageMetadataReturnsOk()
""authors"": ""Test author"",
""dependencyGroups"": [
{
""targetFramework"": ""net50"",
""targetFramework"": ""net5.0"",
""dependencies"": []
}
],
Expand Down
2 changes: 1 addition & 1 deletion tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PropertyGroup>
<MicrosoftAspNetCorePackageVersion>3.1.1</MicrosoftAspNetCorePackageVersion>
<NewtonsoftJsonPackageVersion>12.0.2</NewtonsoftJsonPackageVersion>
<NuGetPackageVersion>5.4.0</NuGetPackageVersion>
<NuGetPackageVersion>5.10.0</NuGetPackageVersion>
<XUnitPackageVersion>2.4.1</XUnitPackageVersion>
</PropertyGroup>

Expand Down

0 comments on commit e60e916

Please sign in to comment.