From a097c353ec799bb9a9be32ae1ecb7c609229f2b3 Mon Sep 17 00:00:00 2001 From: filzrev <103790468+filzrev@users.noreply.github.com> Date: Thu, 30 May 2024 16:04:15 +0900 Subject: [PATCH] feat: add gzipped xrefmap file support --- src/Docfx.Build/XRefMaps/XRefMapDownloader.cs | 19 ++++++++++++ .../TestData/xrefmap.json.gz | Bin 0 -> 156 bytes .../Docfx.Build.Tests/TestData/xrefmap.yml.gz | Bin 0 -> 132 bytes .../XRefMapDownloaderTest.cs | 28 ++++++++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 test/Docfx.Build.Tests/TestData/xrefmap.json.gz create mode 100644 test/Docfx.Build.Tests/TestData/xrefmap.yml.gz diff --git a/src/Docfx.Build/XRefMaps/XRefMapDownloader.cs b/src/Docfx.Build/XRefMaps/XRefMapDownloader.cs index 10e7423c28b..4e9222570ea 100644 --- a/src/Docfx.Build/XRefMaps/XRefMapDownloader.cs +++ b/src/Docfx.Build/XRefMaps/XRefMapDownloader.cs @@ -3,6 +3,7 @@ using System; using System.Diagnostics; +using System.IO.Compression; using System.Net; using Docfx.Common; @@ -119,6 +120,24 @@ private static async ValueTask 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(stream, token); + case ".yml": + default: + { + using var reader = new StreamReader(stream); + return YamlUtility.Deserialize(reader); + }; + } + } + case ".json": { using var stream = File.OpenRead(filePath); diff --git a/test/Docfx.Build.Tests/TestData/xrefmap.json.gz b/test/Docfx.Build.Tests/TestData/xrefmap.json.gz new file mode 100644 index 0000000000000000000000000000000000000000..b7d70eee95723bc1a29cace9cf4aa61849855017 GIT binary patch literal 156 zcmV;N0Av3jiwFp*FIHv(0C;j`W^G|`E^2dcZUB3~dw(@Amx6*)QEFOhQEFatYO#`) zLNrha2tYDmpp;gcljE0|n+lXuDlREf(t*ol05yU{GfGMdimmkZQ}UCG^$IFWGV=5E z@{7{-jrC0Rb25{P5{oMJi%U{UDhpDJ^)gCwbCgld$-`=TX=Vyc5F%C!qGEZufHVNp Kra?Em0001-ph4^a literal 0 HcmV?d00001 diff --git a/test/Docfx.Build.Tests/TestData/xrefmap.yml.gz b/test/Docfx.Build.Tests/TestData/xrefmap.yml.gz new file mode 100644 index 0000000000000000000000000000000000000000..f0adc272cca9ab3b73046a31a7ff1bd2237046b5 GIT binary patch literal 132 zcmV-~0DJ!*iwFqlP*r9E0C;j`W^G|`E_rQi0DHfCe^F{$YEf!la%!;^FPE-DT4_#> zUt(^ml|pez5igg5LIzNQl|n{INkOrdzJ5x6aS*4jNU^W27IpFbk00017+cbIr literal 0 HcmV?d00001 diff --git a/test/Docfx.Build.Tests/XRefMapDownloaderTest.cs b/test/Docfx.Build.Tests/XRefMapDownloaderTest.cs index 50b389263f4..3e4a6ebf16b 100644 --- a/test/Docfx.Build.Tests/XRefMapDownloaderTest.cs +++ b/test/Docfx.Build.Tests/XRefMapDownloaderTest.cs @@ -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); + } + /// /// XrefmapDownloader test for xrefmap that has no baseUrl and href is defined by relative path. ///