Skip to content

Commit 639cf5b

Browse files
authored
Merge pull request #150 from noncheat/main
fix: added option to delete folders before generation
2 parents 0d04c85 + 2e2dac2 commit 639cf5b

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
@@ -122,6 +127,7 @@ class Openapi {
122127
this.templateDirectory,
123128
required this.generatorName,
124129
this.outputDirectory,
130+
this.cleanSubOutputDirectory,
125131
this.typeMappings,
126132
this.importMappings,
127133
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
@@ -112,6 +115,8 @@ class GeneratorArguments {
112115
updateAnnotatedFile =
113116
annotations.readPropertyOrDefault('updateAnnotatedFile', true),
114117
outputDirectory = annotations.readPropertyOrNull('outputDirectory'),
118+
cleanSubOutputDirectory =
119+
annotations.readPropertyOrNull('cleanSubOutputDirectory'),
115120
cachePath =
116121
annotations.readPropertyOrDefault('cachePath', defaultCachedPath),
117122
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)