Skip to content

Commit 2e2dac2

Browse files
committed
Add cleanSubOutputDirectory allow clean up output directory before generating
1 parent 2eab4ab commit 2e2dac2

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

openapi-generator-annotations/lib/src/openapi_generator_annotations_base.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ class Openapi {
5050
/// -o, --output
5151
final String? outputDirectory;
5252

53+
/// Defines whether the output directory should be cleaned up before generating the output.
54+
///
55+
/// e.g [''], ['lib/src']
56+
final List<dynamic>? cleanSubOutputDirectory;
57+
5358
/// Skips the default behavior of validating an input specification.
5459
///
5560
/// --skip-validate-spec
@@ -118,6 +123,7 @@ class Openapi {
118123
this.templateDirectory,
119124
required this.generatorName,
120125
this.outputDirectory,
126+
this.cleanSubOutputDirectory,
121127
this.typeMappings,
122128
this.importMappings,
123129
this.reservedWordsMappings,

openapi-generator/lib/src/extensions/type_methods.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ extension ReadProperty on ConstantReader {
172172
} else if (isA(v, Set)) {
173173
return v.setValue.map(convertToPropertyValue) as T;
174174
} else if (isA(v, List)) {
175-
return v.listValue.map(convertToPropertyValue) as T;
175+
return v.listValue.map(convertToPropertyValue).toList() as T;
176176
} else if (isA(v, Enum)) {
177177
return v.enumValue();
178178
} else {

openapi-generator/lib/src/models/generator_arguments.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class GeneratorArguments {
3232
/// Default: Directory.current.path
3333
final String? outputDirectory;
3434

35+
/// Defines whether the output directory should be cleaned up before generating the output.
36+
final List<dynamic>? cleanSubOutputDirectory;
37+
3538
/// Informs the generator to run source gen on the output.
3639
///
3740
/// Default: true
@@ -105,6 +108,8 @@ class GeneratorArguments {
105108
shouldFetchDependencies =
106109
annotations.readPropertyOrDefault('fetchDependencies', true),
107110
outputDirectory = annotations.readPropertyOrNull('outputDirectory'),
111+
cleanSubOutputDirectory =
112+
annotations.readPropertyOrNull('cleanSubOutputDirectory'),
108113
cachePath =
109114
annotations.readPropertyOrDefault('cachePath', defaultCachedPath),
110115
pubspecPath = annotations.readPropertyOrDefault<String>(

openapi-generator/lib/src/openapi_generator_runner.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,33 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
9090
),
9191
);
9292

93+
if (arguments.cleanSubOutputDirectory != null) {
94+
arguments.cleanSubOutputDirectory?.forEach((directory) {
95+
final childDirectory =
96+
Directory('${arguments.outputDirectory}/${directory.toString()}');
97+
try {
98+
final outputDirectory = Directory('${arguments.outputDirectory}')
99+
.resolveSymbolicLinksSync();
100+
// Make sure is sub directory, avoid ../../
101+
if (childDirectory
102+
.resolveSymbolicLinksSync()
103+
.startsWith(outputDirectory)) {
104+
childDirectory.delete(recursive: true);
105+
}
106+
} catch (e, st) {
107+
logOutputMessage(
108+
log: log,
109+
communication: OutputMessage(
110+
message: 'Output directory already empty',
111+
additionalContext: e,
112+
stackTrace: st,
113+
level: Level.WARNING,
114+
),
115+
);
116+
}
117+
});
118+
}
119+
93120
var binPath = (await Isolate.resolvePackageUri(
94121
Uri.parse('package:openapi_generator_cli/openapi-generator.jar')))!
95122
.toFilePath(windows: Platform.isWindows);

0 commit comments

Comments
 (0)