Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for gzipped xrefmap that stored as local file #9966

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Docfx.Build/XRefMaps/XRefMapDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics;
using System.IO.Compression;
using System.Net;
using Docfx.Common;

Expand Down Expand Up @@ -119,6 +120,24 @@ private static async ValueTask<IXRefContainer> ReadLocalFileAsync(string filePat
case ".zip":
return XRefArchive.Open(filePath, XRefArchiveMode.Read);

case ".gz":
{
using var fileStream = File.OpenRead(filePath);
using var stream = new GZipStream(fileStream, CompressionMode.Decompress);

switch (Path.GetExtension(Path.GetFileNameWithoutExtension(filePath)).ToLowerInvariant())
{
case ".json":
return await SystemTextJsonUtility.DeserializeAsync<XRefMap>(stream, token);
case ".yml":
default:
{
using var reader = new StreamReader(stream);
return YamlUtility.Deserialize<XRefMap>(reader);
};
}
}

case ".json":
{
using var stream = File.OpenRead(filePath);
Expand Down
Binary file added test/Docfx.Build.Tests/TestData/xrefmap.json.gz
Binary file not shown.
Binary file added test/Docfx.Build.Tests/TestData/xrefmap.yml.gz
Binary file not shown.
28 changes: 28 additions & 0 deletions test/Docfx.Build.Tests/XRefMapDownloaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,34 @@ public async Task ReadLocalXRefMapJsonFileTest()
xrefMap.References.Should().HaveCount(1);
}

[Fact]
public async Task ReadLocalXRefMapGZippedJsonFileTest()
{
// Arrange
var path = Path.Combine(Directory.GetCurrentDirectory(), "TestData", "xrefmap.json.gz");

XRefMapDownloader downloader = new XRefMapDownloader();
var xrefMap = await downloader.DownloadAsync(new Uri(path)) as XRefMap;

// Assert
xrefMap.Should().NotBeNull();
xrefMap.References.Should().HaveCount(1);
}

[Fact]
public async Task ReadLocalXRefMapGZippedYamlFileTest()
{
// Arrange
var path = Path.Combine(Directory.GetCurrentDirectory(), "TestData", "xrefmap.yml.gz");

XRefMapDownloader downloader = new XRefMapDownloader();
var xrefMap = await downloader.DownloadAsync(new Uri(path)) as XRefMap;

// Assert
xrefMap.Should().NotBeNull();
xrefMap.References.Should().HaveCount(1);
}

/// <summary>
/// XrefmapDownloader test for xrefmap that has no baseUrl and href is defined by relative path.
/// </summary>
Expand Down