From a7a2c11367133d260b92c53a46f15d1c79e11363 Mon Sep 17 00:00:00 2001 From: dvolper Date: Sun, 5 May 2024 19:35:34 +0200 Subject: [PATCH] clear output directory before generations --- src/Doki.Output.Abstractions/IOutput.cs | 4 ++++ src/Doki.Output.Abstractions/OutputOptions.cs | 2 ++ .../OutputOptionsExtensions.cs | 14 ++++++++++++++ src/Doki.Output.ClassLibrary/ClassLibraryOutput.cs | 7 +++++++ src/Doki.Output.Markdown/MarkdownOutput.cs | 7 +++++++ src/Doki/DocumentationGenerator.cs | 10 ++++++++++ 6 files changed, 44 insertions(+) create mode 100644 src/Doki.Output.Abstractions/OutputOptionsExtensions.cs diff --git a/src/Doki.Output.Abstractions/IOutput.cs b/src/Doki.Output.Abstractions/IOutput.cs index 9ccf79d..fd1e457 100644 --- a/src/Doki.Output.Abstractions/IOutput.cs +++ b/src/Doki.Output.Abstractions/IOutput.cs @@ -5,6 +5,10 @@ /// public interface IOutput { + Task BeginAsync(CancellationToken cancellationToken = default) => Task.CompletedTask; + + Task EndAsync(CancellationToken cancellationToken = default) => Task.CompletedTask; + /// /// Writes the content list to the output. /// diff --git a/src/Doki.Output.Abstractions/OutputOptions.cs b/src/Doki.Output.Abstractions/OutputOptions.cs index 214dea9..1cdb1dd 100644 --- a/src/Doki.Output.Abstractions/OutputOptions.cs +++ b/src/Doki.Output.Abstractions/OutputOptions.cs @@ -6,4 +6,6 @@ namespace Doki.Output; public record OutputOptions where T : IOutput { [JsonPropertyName("outputPath")] public DirectoryInfo OutputDirectory { get; init; } = null!; + + [JsonPropertyName("clearOutput")] public bool ClearOutput { get; init; } = true; } \ No newline at end of file diff --git a/src/Doki.Output.Abstractions/OutputOptionsExtensions.cs b/src/Doki.Output.Abstractions/OutputOptionsExtensions.cs new file mode 100644 index 0000000..51bf1d7 --- /dev/null +++ b/src/Doki.Output.Abstractions/OutputOptionsExtensions.cs @@ -0,0 +1,14 @@ +namespace Doki.Output; + +public static class OutputOptionsExtensions +{ + public static void ClearOutputDirectoryIfRequired(this OutputOptions options) where T : IOutput + { + ArgumentNullException.ThrowIfNull(options); + + if (options is { ClearOutput: true, OutputDirectory.Exists: true }) + { + options.OutputDirectory.Delete(true); + } + } +} \ No newline at end of file diff --git a/src/Doki.Output.ClassLibrary/ClassLibraryOutput.cs b/src/Doki.Output.ClassLibrary/ClassLibraryOutput.cs index cf9527e..8594aea 100644 --- a/src/Doki.Output.ClassLibrary/ClassLibraryOutput.cs +++ b/src/Doki.Output.ClassLibrary/ClassLibraryOutput.cs @@ -2,6 +2,13 @@ public sealed class ClassLibraryOutput(ClassLibraryOutputOptions options) : IOutput { + public Task BeginAsync(CancellationToken cancellationToken = default) + { + options.ClearOutputDirectoryIfRequired(); + + return Task.CompletedTask; + } + public async Task WriteAsync(ContentList contentList, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(contentList); diff --git a/src/Doki.Output.Markdown/MarkdownOutput.cs b/src/Doki.Output.Markdown/MarkdownOutput.cs index bd7a4c7..b801c9e 100644 --- a/src/Doki.Output.Markdown/MarkdownOutput.cs +++ b/src/Doki.Output.Markdown/MarkdownOutput.cs @@ -5,6 +5,13 @@ namespace Doki.Output.Markdown; public sealed class MarkdownOutput(OutputOptions options) : IOutput { + public Task BeginAsync(CancellationToken cancellationToken = default) + { + options.ClearOutputDirectoryIfRequired(); + + return Task.CompletedTask; + } + public async Task WriteAsync(ContentList contentList, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(contentList); diff --git a/src/Doki/DocumentationGenerator.cs b/src/Doki/DocumentationGenerator.cs index 8345e7f..d5c4440 100644 --- a/src/Doki/DocumentationGenerator.cs +++ b/src/Doki/DocumentationGenerator.cs @@ -154,6 +154,11 @@ public async Task GenerateAsync(ILogger logger, CancellationToken cancellationTo logger.LogInformation("Generating documentation for {AssemblyCount} assemblies.", _assemblies.Count); + foreach (var output in outputs) + { + await output.BeginAsync(cancellationToken); + } + var assemblies = new ContentList { Id = ContentList.Assemblies, @@ -184,6 +189,11 @@ await GenerateAssemblyDocumentationAsync(new GeneratorContext { await output.WriteAsync(assemblies, cancellationToken); } + + foreach (var output in outputs) + { + await output.EndAsync(cancellationToken); + } } private async Task GenerateAssemblyDocumentationAsync(GeneratorContext context,