Skip to content

Commit

Permalink
Add cleanSubOutputDirectory allow clean up output directory before ge…
Browse files Browse the repository at this point in the history
…nerating
  • Loading branch information
quyenvsp committed Aug 22, 2024
1 parent 2eab4ab commit 2e2dac2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class Openapi {
/// -o, --output
final String? outputDirectory;

/// Defines whether the output directory should be cleaned up before generating the output.
///
/// e.g [''], ['lib/src']
final List<dynamic>? cleanSubOutputDirectory;

/// Skips the default behavior of validating an input specification.
///
/// --skip-validate-spec
Expand Down Expand Up @@ -118,6 +123,7 @@ class Openapi {
this.templateDirectory,
required this.generatorName,
this.outputDirectory,
this.cleanSubOutputDirectory,
this.typeMappings,
this.importMappings,
this.reservedWordsMappings,
Expand Down
2 changes: 1 addition & 1 deletion openapi-generator/lib/src/extensions/type_methods.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ extension ReadProperty on ConstantReader {
} else if (isA(v, Set)) {
return v.setValue.map(convertToPropertyValue) as T;
} else if (isA(v, List)) {
return v.listValue.map(convertToPropertyValue) as T;
return v.listValue.map(convertToPropertyValue).toList() as T;
} else if (isA(v, Enum)) {
return v.enumValue();
} else {
Expand Down
5 changes: 5 additions & 0 deletions openapi-generator/lib/src/models/generator_arguments.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class GeneratorArguments {
/// Default: Directory.current.path
final String? outputDirectory;

/// Defines whether the output directory should be cleaned up before generating the output.
final List<dynamic>? cleanSubOutputDirectory;

/// Informs the generator to run source gen on the output.
///
/// Default: true
Expand Down Expand Up @@ -105,6 +108,8 @@ class GeneratorArguments {
shouldFetchDependencies =
annotations.readPropertyOrDefault('fetchDependencies', true),
outputDirectory = annotations.readPropertyOrNull('outputDirectory'),
cleanSubOutputDirectory =
annotations.readPropertyOrNull('cleanSubOutputDirectory'),
cachePath =
annotations.readPropertyOrDefault('cachePath', defaultCachedPath),
pubspecPath = annotations.readPropertyOrDefault<String>(
Expand Down
27 changes: 27 additions & 0 deletions openapi-generator/lib/src/openapi_generator_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,33 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
),
);

if (arguments.cleanSubOutputDirectory != null) {
arguments.cleanSubOutputDirectory?.forEach((directory) {
final childDirectory =
Directory('${arguments.outputDirectory}/${directory.toString()}');
try {
final outputDirectory = Directory('${arguments.outputDirectory}')
.resolveSymbolicLinksSync();
// Make sure is sub directory, avoid ../../
if (childDirectory
.resolveSymbolicLinksSync()
.startsWith(outputDirectory)) {
childDirectory.delete(recursive: true);
}
} catch (e, st) {
logOutputMessage(
log: log,
communication: OutputMessage(
message: 'Output directory already empty',
additionalContext: e,
stackTrace: st,
level: Level.WARNING,
),
);
}
});
}

var binPath = (await Isolate.resolvePackageUri(
Uri.parse('package:openapi_generator_cli/openapi-generator.jar')))!
.toFilePath(windows: Platform.isWindows);
Expand Down

0 comments on commit 2e2dac2

Please sign in to comment.