Skip to content

Commit

Permalink
Merge pull request #1379 from microsoft/feature/clean-output
Browse files Browse the repository at this point in the history
- adds a clean ouput argument
  • Loading branch information
baywet authored Mar 15, 2022
2 parents 75958df + e20ae7c commit 20ad6cf
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions docs/using.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/Kiota.Builder/GenerationConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ public class GenerationConfiguration {
private static readonly HashSet<GenerationLanguage> BarreledLanguagesWithConstantFileName = new () {
//TODO: add typescript once we have a barrel writer for it
};
public bool CleanOutput { get; set;}
}
}
9 changes: 9 additions & 0 deletions src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public KiotaBuilder(ILogger<KiotaBuilder> 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)
{
Expand All @@ -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) {
Expand Down
9 changes: 7 additions & 2 deletions src/kiota/KiotaHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public RootCommand GetRootCommand()
var deserializerOption = new Option<List<string>>("--deserializer", () => new List<string> {"Microsoft.Kiota.Serialization.Json.JsonParseNodeFactory"}, "The fully qualified class names for deserializers. Accepts multiple values.");
deserializerOption.AddAlias("--ds");

var cleanOutputOption = new Option<bool>("--clean-output", () => false, "Removes all files from the output directory before generating the code files.");
cleanOutputOption.AddAlias("-co");

var command = new RootCommand {
outputOption,
languageOption,
Expand All @@ -55,15 +58,16 @@ public RootCommand GetRootCommand()
namespaceOption,
serializerOption,
deserializerOption,
cleanOutputOption,
};
command.SetHandler<string, GenerationLanguage, string, bool, string, LogLevel, string, List<string>, List<string>, CancellationToken>(HandleCommandCall, outputOption, languageOption, descriptionOption, backingStoreOption, classOption, logLevelOption, namespaceOption, serializerOption, deserializerOption);
command.SetHandler<string, GenerationLanguage, string, bool, string, LogLevel, string, List<string>, List<string>, bool, CancellationToken>(HandleCommandCall, outputOption, languageOption, descriptionOption, backingStoreOption, classOption, logLevelOption, namespaceOption, serializerOption, deserializerOption, cleanOutputOption);
return command;
}
private void AssignIfNotNullOrEmpty(string input, Action<GenerationConfiguration, string> assignment) {
if (!string.IsNullOrEmpty(input))
assignment.Invoke(Configuration, input);
}
private async Task<int> HandleCommandCall(string output, GenerationLanguage language, string openapi, bool backingstore, string classname, LogLevel loglevel, string namespacename, List<string> serializer, List<string> deserializer, CancellationToken cancellationToken) {
private async Task<int> HandleCommandCall(string output, GenerationLanguage language, string openapi, bool backingstore, string classname, LogLevel loglevel, string namespacename, List<string> serializer, List<string> 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);
Expand All @@ -81,6 +85,7 @@ private async Task<int> HandleCommandCall(string output, GenerationLanguage lang

Configuration.OpenAPIFilePath = GetAbsolutePath(Configuration.OpenAPIFilePath);
Configuration.OutputPath = GetAbsolutePath(Configuration.OutputPath);
Configuration.CleanOutput = cleanOutput;

var logger = LoggerFactory.Create((builder) => {
builder
Expand Down

0 comments on commit 20ad6cf

Please sign in to comment.