Skip to content

Commit

Permalink
fix: globalMetadataFiles and fileMetadataFiles not working (#8828)
Browse files Browse the repository at this point in the history
  • Loading branch information
yufeih authored Jun 5, 2023
1 parent f28165a commit d5a3d28
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 124 deletions.
102 changes: 64 additions & 38 deletions src/Microsoft.DocAsCode.App/Helpers/DocumentBuilderWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,10 @@ private static List<DocumentBuildParameters> ConfigToParameter(BuildJsonConfig c
TagParameters = config.TagParameters,
ConfigureMarkdig = options.ConfigureMarkdig,
};
if (config.GlobalMetadata != null)
{
parameters.Metadata = config.GlobalMetadata.ToImmutableDictionary();
}
if (config.FileMetadata != null)
{
parameters.FileMetadata = ConvertToFileMetadataItem(baseDirectory, config.FileMetadata);
}

parameters.Metadata = GetGlobalMetadata(config);
parameters.FileMetadata = GetFileMetadata(baseDirectory, config);

if (config.PostProcessors != null)
{
parameters.PostProcessors = config.PostProcessors.ToImmutableArray();
Expand Down Expand Up @@ -254,6 +250,66 @@ private static List<DocumentBuildParameters> ConfigToParameter(BuildJsonConfig c
}
}

private static ImmutableDictionary<string, object> GetGlobalMetadata(BuildJsonConfig config)
{
var result = new Dictionary<string, object>();

if (config.GlobalMetadata != null)
{
foreach (var (key, value) in config.GlobalMetadata)
{
result[key] = value;
}
}

if (config.GlobalMetadataFilePaths != null)
{
foreach (var path in config.GlobalMetadataFilePaths)
{
foreach (var (key, value) in JsonUtility.Deserialize<Dictionary<string, object>>(path))
{
result[key] = value;
}
}
}

return result.ToImmutableDictionary();
}

private static FileMetadata GetFileMetadata(string baseDirectory, BuildJsonConfig config)
{
var result = new Dictionary<string, List<FileMetadataItem>>();

if (config.FileMetadata != null)
{
foreach (var (key, value) in config.FileMetadata)
{
var list = result.TryGetValue(key, out var items) ? items : result[key] = new();
foreach (var pair in value.Items)
{
list.Add(new FileMetadataItem(pair.Glob, key, pair.Value));
}
}
}

if (config.FileMetadataFilePaths != null)
{
foreach (var path in config.FileMetadataFilePaths)
{
foreach (var (key, value) in JsonUtility.Deserialize<Dictionary<string, FileMetadataPairs>>(path))
{
var list = result.TryGetValue(key, out var items) ? items : result[key] = new();
foreach (var pair in value.Items)
{
list.Add(new FileMetadataItem(pair.Glob, key, pair.Value));
}
}
}
}

return new FileMetadata(baseDirectory, result.ToDictionary(p => p.Key, p => p.Value.ToImmutableArray()));
}

/// <summary>
/// Group FileMappings to a dictionary using VersionName as the key.
/// As default version has no VersionName, using empty string as the key.
Expand Down Expand Up @@ -303,36 +359,6 @@ private static void AddFileMappingTypeGroup(
}
}

private static FileMetadata ConvertToFileMetadataItem(string baseDirectory, Dictionary<string, FileMetadataPairs> fileMetadata)
{
var result = new Dictionary<string, ImmutableArray<FileMetadataItem>>();
foreach (var item in fileMetadata)
{
var list = new List<FileMetadataItem>();
foreach (var pair in item.Value.Items)
{
list.Add(new FileMetadataItem(pair.Glob, item.Key, pair.Value));
}
result.Add(item.Key, list.ToImmutableArray());
}

return new FileMetadata(baseDirectory, result);
}

private static IEnumerable<string> GetFilesFromFileMapping(FileMapping mapping)
{
if (mapping != null)
{
foreach (var file in mapping.Items)
{
foreach (string item in file.Files)
{
yield return Path.Combine(file.SourceFolder ?? Directory.GetCurrentDirectory(), item);
}
}
}
}

private static FileCollection GetFileCollectionFromFileMapping(
string baseDirectory,
FileMapping articles,
Expand Down
70 changes: 0 additions & 70 deletions src/docfx/Models/OptionMerger.cs

This file was deleted.

122 changes: 106 additions & 16 deletions test/docfx.Tests/DocsetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Runtime.CompilerServices;

using System.Text.Json;
using Microsoft.DocAsCode.Tests.Common;

using Xunit;
Expand Down Expand Up @@ -41,14 +41,14 @@ public static async Task CustomLogo_Override_LogoFromTemplate()
{
["docfx.json"] =
"""
{
"build": {
"resource": [{ "files": [ "logo.svg" ] }],
"template": ["default"],
"dest": "_site"
}
{
"build": {
"resource": [{ "files": [ "logo.svg" ] }],
"template": ["default"],
"dest": "_site"
}
""",
}
""",
["logo.svg"] = "<svg>my svg</svg>"
});

Expand All @@ -62,18 +62,108 @@ public static async Task Load_Custom_Plugin_From_Template()
{
["docfx.json"] =
"""
{
"build": {
"content": [{ "files": [ "*.md" ] }],
"template": ["default", "../../Assets/template"],
"dest": "_site",
"postProcessors": ["CustomPostProcessor"]
}
{
"build": {
"content": [{ "files": [ "*.md" ] }],
"template": ["default", "../../Assets/template"],
"dest": "_site",
"postProcessors": ["CustomPostProcessor"]
}
""",
}
""",
["index.md"] = ""
});

Assert.Equal("customPostProcessor", outputs["customPostProcessor.txt"]());
}

[Fact]
public static async Task Build_With_Global_Metadata_Files()
{
var outputs = await Build(new()
{
["docfx.json"] =
"""
{
"build": {
"content": [{ "files": [ "*.md" ] }],
"dest": "_site",
"exportRawModel": true,
"globalMetadataFiles": ["projectMetadata1.json", "projectMetadata2.json"],
"globalMetadata": {
"meta1": "docfx.json",
"meta3": "docfx.json"
}
}
}
""",
["projectMetadata1.json"] =
"""
{
"meta1": "projectMetadata1.json",
"meta2": "projectMetadata2.json"
}
""",
["projectMetadata2.json"] =
"""
{
"meta2": "projectMetadata2.json"
}
""",
["index.md"] = ""
});

var metadata = JsonDocument.Parse(outputs["index.raw.json"]()).RootElement;
Assert.Equal("projectMetadata1.json", metadata.GetProperty("meta1").GetString());
Assert.Equal("projectMetadata2.json", metadata.GetProperty("meta2").GetString());
Assert.Equal("docfx.json", metadata.GetProperty("meta3").GetString());
}

[Fact]
public static async Task Build_With_File_Metadata_Files()
{
var outputs = await Build(new()
{
["docfx.json"] =
"""
{
"build": {
"content": [{ "files": [ "*.md" ] }],
"dest": "_site",
"exportRawModel": true,
"fileMetadataFiles": ["fileMetadata1.json", "fileMetadata2.json"],
"fileMetadata": {
"meta1": {
"a.md": "docfx.json"
}
}
}
}
""",
["fileMetadata1.json"] =
"""
{
"meta1": {
"a.md": "fileMetadata1.json",
"b.md": "fileMetadata1.json"
}
}
""",
["fileMetadata2.json"] =
"""
{
"meta1": {
"b.md": "fileMetadata2.json"
}
}
""",
["a.md"] = "",
["b.md"] = ""
});

var a = JsonDocument.Parse(outputs["a.raw.json"]()).RootElement;
var b = JsonDocument.Parse(outputs["b.raw.json"]()).RootElement;
Assert.Equal("fileMetadata1.json", a.GetProperty("meta1").GetString());
Assert.Equal("fileMetadata2.json", b.GetProperty("meta1").GetString());
}
}

0 comments on commit d5a3d28

Please sign in to comment.