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

chore: drop support of template as zip files and embedded resources #8811

Merged
merged 1 commit into from
May 30, 2023
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
1 change: 0 additions & 1 deletion src/Microsoft.DocAsCode.App/Helpers/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ internal static class Constants
{
public const string ConfigFileName = "docfx.json";
public const string DefaultTemplateName = "default";
public const string EmbeddedTemplateFolderName = "Template";
}
3 changes: 1 addition & 2 deletions src/Microsoft.DocAsCode.App/RunBuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public static string Exec(BuildJsonConfig config, BuildOptions options, string c
config.Templates = new ListWithStringFallback { DocAsCode.Constants.DefaultTemplateName };
}

var assembly = typeof(Docset).Assembly;
var templateManager = new TemplateManager(assembly, Constants.EmbeddedTemplateFolderName, config.Templates, config.Themes, configDirectory);
var templateManager = new TemplateManager(config.Templates, config.Themes, configDirectory);

EnvironmentContext.SetGitFeaturesDisabled(config.DisableGitFeatures);
EnvironmentContext.SetBaseDirectory(Path.GetFullPath(string.IsNullOrEmpty(configDirectory) ? Directory.GetCurrentDirectory() : configDirectory));
Expand Down
76 changes: 0 additions & 76 deletions src/Microsoft.DocAsCode.Build.Engine/ResourceFinder.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Reflection;

using Microsoft.DocAsCode.Common;

#nullable enable

namespace Microsoft.DocAsCode.Build.Engine;

[Serializable]
public class TemplateManager
{
private readonly List<string> _templates = new();
private readonly List<string> _themes = new();
private readonly ResourceFinder _finder;
private readonly List<string> _templates;
private readonly List<string>? _themes;
private readonly string _baseDirectory;

public TemplateManager(Assembly assembly, string rootNamespace, List<string> templates, List<string> themes, string baseDirectory)
public TemplateManager(List<string> templates, List<string>? themes, string? baseDirectory)
{
_finder = new ResourceFinder(assembly, rootNamespace, baseDirectory);
_templates = templates;
_themes = themes;
_baseDirectory = baseDirectory ?? Directory.GetCurrentDirectory();
}

public bool TryExportTemplateFiles(string outputDirectory, string regexFilter = null)
public bool TryExportTemplateFiles(string outputDirectory, string? regexFilter = null)
{
return TryExportResourceFiles(_templates, outputDirectory, true, regexFilter);
}
Expand All @@ -33,8 +32,27 @@ public TemplateProcessor GetTemplateProcessor(DocumentBuildContext context, int

public CompositeResourceReader CreateTemplateResource() => CreateTemplateResource(_templates);

private CompositeResourceReader CreateTemplateResource(IEnumerable<string> resources) =>
new(resources.Select(s => _finder.Find(s)).Where(s => s != null));
private CompositeResourceReader CreateTemplateResource(IEnumerable<string> resources)
{
return new(resources.Select(FindResource).Where(s => s != null));

ResourceFileReader? FindResource(string name)
{
var directory = Path.Combine(AppContext.BaseDirectory, "templates", name);
if (Directory.Exists(directory))
{
return new LocalFileResourceReader(directory);
}

directory = Path.Combine(_baseDirectory, name);
if (Directory.Exists(directory))
{
return new LocalFileResourceReader(directory);
}

return null;
}
}

public void ProcessTheme(string outputDirectory, bool overwrite)
{
Expand All @@ -48,10 +66,14 @@ public void ProcessTheme(string outputDirectory, bool overwrite)
}
}

private bool TryExportResourceFiles(IEnumerable<string> resourceNames, string outputDirectory, bool overwrite, string regexFilter = null)
private bool TryExportResourceFiles(IEnumerable<string> resourceNames, string outputDirectory, bool overwrite, string? regexFilter = null)
{
if (string.IsNullOrEmpty(outputDirectory)) throw new ArgumentNullException(nameof(outputDirectory));
if (!resourceNames.Any()) return false;
if (string.IsNullOrEmpty(outputDirectory))
throw new ArgumentNullException(nameof(outputDirectory));

if (!resourceNames.Any())
return false;

bool isEmpty = true;

using (new LoggerPhaseScope("ExportResourceFiles", LogLevel.Verbose))
Expand Down
2 changes: 1 addition & 1 deletion src/docfx/Models/TemplateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public override int Execute(CommandContext context, Options options)

foreach (var template in templates)
{
var manager = new TemplateManager(typeof(Docset).Assembly, Constants.EmbeddedTemplateFolderName, new List<string> { template }, null, null);
var manager = new TemplateManager(new List<string> { template }, null, null);
if (manager.TryExportTemplateFiles(Path.Combine(outputFolder, template)))
{
Logger.LogInfo($"{template} is exported to {outputFolder}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ConceptualDocumentProcessorTest()
// Prepare conceptual template
var templateCreator = new FileCreator(_templateFolder);
var file = templateCreator.CreateFile(@"{{{conceptual}}}", "conceptual.html.tmpl", "default");
_templateManager = new TemplateManager(null, null, new List<string> { "default" }, null, _templateFolder);
_templateManager = new TemplateManager(new List<string> { "default" }, null, _templateFolder);
}

public override void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ private void BuildDocument(
OutputBaseDir = Path.Combine(Directory.GetCurrentDirectory(), _outputFolder),
ApplyTemplateSettings = applyTemplateSettings,
Metadata = metadata?.ToImmutableDictionary(),
TemplateManager = new TemplateManager(null, null, new List<string> { _templateFolder }, null, null),
TemplateManager = new TemplateManager(new List<string> { _templateFolder }, null, null),
TemplateDir = templateFolder,
VersionDir = versionDir,
XRefMaps = ImmutableArray.Create("TestData/xrefmap.yml"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="tmpl\tmpl1.zip" />
<None Include="PostProcessors\**;tmpl\tmpl1\sub\file1;tmpl\tmpl1\tmpl1.dot.$" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,13 @@ public TemplateManagerUnitTest()
_outputFolder = GetRandomFolder();
}

[Trait("Related", "ResourceFinder")]
[Fact]
public void TestResourceFinderFromAssembly()
{
var testFinder = new ResourceFinder(this.GetType().Assembly, "tmpl");

// 1. Support tmpl1.zip
using var result = testFinder.Find("tmpl1");
Assert.NotNull(result);
Assert.Equal(2, result.Names.Count());
var item = result.GetResource("tmpl1.dot.$");
Assert.Equal("This is file with complex filename characters", item);

// backslash is also supported
item = result.GetResource(@"sub\file1");
Assert.Equal("This is file inside a subfolder", item);
}

[Trait("Related", "ResourceFinder")]
[Fact]
public void TestTemplateManagerWithMutipleThemesShouldWork()
{
// If the same resource name exists in the override folder, use the overriden one
var themes = new List<string> { "tmpl1", "tmpl/tmpl1" };
var manager = new TemplateManager(GetType().Assembly, "tmpl", null, themes, null);
var manager = new TemplateManager(null, themes, null);
var outputFolder = Path.Combine(_outputFolder, "TestTemplateManager_MutipleThemes");
manager.ProcessTheme(outputFolder, true);
// 1. Support tmpl1.zip
Expand Down Expand Up @@ -569,9 +551,8 @@ private static void ProcessTemplate(string templateName, string inputFolder, IEn
if (Directory.Exists(templateFolder))
Directory.Delete(templateFolder, true);
WriteTemplate(templateFolder, templateFiles);
using var resource = new ResourceFinder(null, null).Find(templateFolder);
var context = new DocumentBuildContext(inputFolder);
var processor = new TemplateProcessor(resource, context, 4);
var processor = new TemplateProcessor(new LocalFileResourceReader(templateFolder), context, 4);
foreach (var item in items)
{
if (item.ResourceFile != null)
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ManagedReferenceDocumentProcessorTest()
TransformDocument = true,
};

_templateManager = new TemplateManager(null, null, new List<string> { "template" }, null, "TestData/");
_templateManager = new TemplateManager(new List<string> { "template" }, null, "TestData/");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public SplitRestApiToOperationLevelTest()
RawModelExportSettings = { Export = true },
TransformDocument = true,
};
_templateManager = new TemplateManager(null, null, new List<string> { "template" }, null, "TestData/");
_templateManager = new TemplateManager(new List<string> { "template" }, null, "TestData/");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public SplitRestApiToTagLevelTest()
RawModelExportSettings = { Export = true },
TransformDocument = true,
};
_templateManager = new TemplateManager(null, null, new List<string> { "template" }, null, "TestData/");
_templateManager = new TemplateManager(new List<string> { "template" }, null, "TestData/");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public LimitationReachedTest()
TransformDocument = true,
};

_templateManager = new TemplateManager(null, null, new List<string> { "template" }, null, _templateFolder);
_templateManager = new TemplateManager(new List<string> { "template" }, null, _templateFolder);
}

[Fact(Skip = "Mannually run this testcase, as it will influence the result of other test cases")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void OverwriteUneditableTest()
_defaultFiles = new FileCollection(Directory.GetCurrentDirectory());
_applyTemplateSettings = new ApplyTemplateSettings(_inputFolder, _outputFolder) { RawModelExportSettings = { Export = true }, TransformDocument = true, };

_templateManager = new TemplateManager(null, null, new List<string> { "template" }, null, _templateFolder);
_templateManager = new TemplateManager(new List<string> { "template" }, null, _templateFolder);

_listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(null);
_rawModelFilePath = GetRawModelFilePath("FragmentsValidation.yml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public MergeMarkdownFragmentsTest()
_defaultFiles = new FileCollection(Directory.GetCurrentDirectory());
_applyTemplateSettings = new ApplyTemplateSettings(_inputFolder, _outputFolder) { RawModelExportSettings = { Export = true }, TransformDocument = true, };

_templateManager = new TemplateManager(null, null, new List<string> { "template" }, null, _templateFolder);
_templateManager = new TemplateManager(new List<string> { "template" }, null, _templateFolder);

_listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(null);
_rawModelFilePath = GetRawModelFilePath("Suppressions.yml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public SchemaDrivenProcessorTest()
TransformDocument = true,
};

_templateManager = new TemplateManager(null, null, new List<string> { "template" }, null, _templateFolder);
_templateManager = new TemplateManager(new List<string> { "template" }, null, _templateFolder);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public SchemaMergerTest()
TransformDocument = true,
};

_templateManager = new TemplateManager(null, null, new List<string> { "template" }, null, _templateFolder);
_templateManager = new TemplateManager(new List<string> { "template" }, null, _templateFolder);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public UniversalReferenceDocumentProcessorTest()
RawModelExportSettings = { Export = true },
TransformDocument = true,
};
_templateManager = new TemplateManager(null, null, new List<string> { "template" }, null, "TestData/");
_templateManager = new TemplateManager(new List<string> { "template" }, null, "TestData/");
}

#region Python
Expand Down