From 90d133e4137a9553927d4298d565d362c2569457 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Mon, 14 Mar 2022 11:34:13 -0400 Subject: [PATCH 1/2] - adds a clean ouput argument --- CHANGELOG.md | 2 ++ src/Kiota.Builder/GenerationConfiguration.cs | 1 + src/Kiota.Builder/KiotaBuilder.cs | 9 +++++++++ src/kiota/KiotaHost.cs | 9 +++++++-- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aef5e9e6fb..5b75639b0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Adds a `--clean-output` argument to clean the target directory before generation #1357 + ### Changed ## [0.0.18] - 2022-03-14 diff --git a/src/Kiota.Builder/GenerationConfiguration.cs b/src/Kiota.Builder/GenerationConfiguration.cs index 78d7080ca6..a1b882f2bd 100644 --- a/src/Kiota.Builder/GenerationConfiguration.cs +++ b/src/Kiota.Builder/GenerationConfiguration.cs @@ -28,5 +28,6 @@ public class GenerationConfiguration { private static readonly HashSet BarreledLanguagesWithConstantFileName = new () { //TODO: add typescript once we have a barrel writer for it }; + public bool CleanOutput { get; set;} } } diff --git a/src/Kiota.Builder/KiotaBuilder.cs b/src/Kiota.Builder/KiotaBuilder.cs index 103b38bb26..4c98743295 100644 --- a/src/Kiota.Builder/KiotaBuilder.cs +++ b/src/Kiota.Builder/KiotaBuilder.cs @@ -29,6 +29,14 @@ public KiotaBuilder(ILogger logger, GenerationConfiguration config this.logger = logger; this.config = config; } + private void CleanOutputDirectory() + { + if(config.CleanOutput && Directory.Exists(config.OutputPath)) + { + logger.LogInformation("Cleaning output directory {path}", config.OutputPath); + Directory.Delete(config.OutputPath, true); + } + } public async Task GenerateSDK(CancellationToken cancellationToken) { @@ -37,6 +45,7 @@ public async Task GenerateSDK(CancellationToken cancellationToken) string inputPath = config.OpenAPIFilePath; try { + CleanOutputDirectory(); // doing this verification at the begining to give immediate feedback to the user Directory.CreateDirectory(config.OutputPath); } catch (Exception ex) { diff --git a/src/kiota/KiotaHost.cs b/src/kiota/KiotaHost.cs index 0fcb583bf3..d8dc15fa06 100644 --- a/src/kiota/KiotaHost.cs +++ b/src/kiota/KiotaHost.cs @@ -45,6 +45,9 @@ public RootCommand GetRootCommand() var deserializerOption = new Option>("--deserializer", () => new List {"Microsoft.Kiota.Serialization.Json.JsonParseNodeFactory"}, "The fully qualified class names for deserializers. Accepts multiple values."); deserializerOption.AddAlias("--ds"); + var cleanOutputOption = new Option("--clean-output", () => false, "Removes all files from the output directory before generating the code files."); + cleanOutputOption.AddAlias("-co"); + var command = new RootCommand { outputOption, languageOption, @@ -55,15 +58,16 @@ public RootCommand GetRootCommand() namespaceOption, serializerOption, deserializerOption, + cleanOutputOption, }; - command.SetHandler, List, CancellationToken>(HandleCommandCall, outputOption, languageOption, descriptionOption, backingStoreOption, classOption, logLevelOption, namespaceOption, serializerOption, deserializerOption); + command.SetHandler, List, bool, CancellationToken>(HandleCommandCall, outputOption, languageOption, descriptionOption, backingStoreOption, classOption, logLevelOption, namespaceOption, serializerOption, deserializerOption, cleanOutputOption); return command; } private void AssignIfNotNullOrEmpty(string input, Action assignment) { if (!string.IsNullOrEmpty(input)) assignment.Invoke(Configuration, input); } - private async Task HandleCommandCall(string output, GenerationLanguage language, string openapi, bool backingstore, string classname, LogLevel loglevel, string namespacename, List serializer, List deserializer, CancellationToken cancellationToken) { + private async Task HandleCommandCall(string output, GenerationLanguage language, string openapi, bool backingstore, string classname, LogLevel loglevel, string namespacename, List serializer, List deserializer, bool cleanOutput, CancellationToken cancellationToken) { AssignIfNotNullOrEmpty(output, (c, s) => c.OutputPath = s); AssignIfNotNullOrEmpty(openapi, (c, s) => c.OpenAPIFilePath = s); AssignIfNotNullOrEmpty(classname, (c, s) => c.ClientClassName = s); @@ -81,6 +85,7 @@ private async Task HandleCommandCall(string output, GenerationLanguage lang Configuration.OpenAPIFilePath = GetAbsolutePath(Configuration.OpenAPIFilePath); Configuration.OutputPath = GetAbsolutePath(Configuration.OutputPath); + Configuration.CleanOutput = cleanOutput; var logger = LoggerFactory.Create((builder) => { builder From e20ae7ca1628c11ded18398a18cebc4a43b2aa0e Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Mon, 14 Mar 2022 12:18:32 -0400 Subject: [PATCH 2/2] - updates the arguments list documenation for clean output Signed-off-by: Vincent Biret --- docs/using.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/using.md b/docs/using.md index 8c1f1f6622..1584bcd69d 100644 --- a/docs/using.md +++ b/docs/using.md @@ -14,6 +14,7 @@ Kiota accepts the following parameters during the generation. ```shell kiota [--backing-store | -b] [--class-name | -c] + [--clean-output | -co] [--deserializer | -ds] [--language | -l] [--loglevel | -ll] @@ -37,6 +38,14 @@ kiota --backing-store The class name to use for the core client class. Defaults to `ApiClient`. +### `--clean-output (-co)` + +Delete the output directory before generating the client. Defaults to false. + +```shell +kiota --clean-output +``` + #### Accepted values The provided name MUST be a valid class name for the target language.