Skip to content

Commit

Permalink
feat: Add support for gzipped xrefmap that stored as local file (dotn…
Browse files Browse the repository at this point in the history
…et#9966)

feat: add gzipped xrefmap file support
  • Loading branch information
filzrev authored and p-kostov committed Jun 28, 2024
1 parent c243a1a commit d18b417
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
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

0 comments on commit d18b417

Please sign in to comment.