From d1ae279841125a76b7eb6b577e19b2511d841e90 Mon Sep 17 00:00:00 2001 From: Ricardo Boss Date: Fri, 12 Apr 2024 14:42:22 +0200 Subject: [PATCH] Added option to disable caching --- .../openapi_generator_annotations_base.dart | 7 +++- .../lib/src/models/generator_arguments.dart | 7 +++- .../lib/src/openapi_generator_runner.dart | 37 ++++++++++++------- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/openapi-generator-annotations/lib/src/openapi_generator_annotations_base.dart b/openapi-generator-annotations/lib/src/openapi_generator_annotations_base.dart index 7483dd7..ae3fb35 100644 --- a/openapi-generator-annotations/lib/src/openapi_generator_annotations_base.dart +++ b/openapi-generator-annotations/lib/src/openapi_generator_annotations_base.dart @@ -127,6 +127,10 @@ class Openapi { /// Defaults to true final bool updateAnnotatedFile; + /// Whether to disable caching the spec file. Defaults to `true` if the + /// [inputSpec] is not a [RemoteSpec]. + final bool disableCache; + const Openapi({ this.additionalProperties, this.skipSpecValidation = false, @@ -148,7 +152,8 @@ class Openapi { this.projectPubspecPath, this.debugLogging = false, this.updateAnnotatedFile = true, - }); + bool? disableCache, + }) : disableCache = disableCache ?? inputSpec is! RemoteSpec; } /// Provides the input spec file to be used. diff --git a/openapi-generator/lib/src/models/generator_arguments.dart b/openapi-generator/lib/src/models/generator_arguments.dart index b6a6651..ffbfc9f 100644 --- a/openapi-generator/lib/src/models/generator_arguments.dart +++ b/openapi-generator/lib/src/models/generator_arguments.dart @@ -20,6 +20,10 @@ class GeneratorArguments { /// /// The default location is: .dart_tool/openapi-generator-cache.json final String cachePath; + + /// Informs the generator to disable the cache. + final bool disableCache; + final bool isDebug; /// Use a custom pubspec file when generating. @@ -130,7 +134,8 @@ class GeneratorArguments { '${Directory.current.path}${Platform.pathSeparator}pubspec.yaml'), isDebug = annotations.readPropertyOrDefault('debugLogging', false), inputSpec = - annotations.readPropertyOrDefault('inputSpec', InputSpec.json()); + annotations.readPropertyOrDefault('inputSpec', InputSpec.json()), + disableCache = annotations.readPropertyOrDefault('disableCache', false); /// The stringified name of the [Generator]. String get generatorName => generator == Generator.dart diff --git a/openapi-generator/lib/src/openapi_generator_runner.dart b/openapi-generator/lib/src/openapi_generator_runner.dart index 1fd2398..69fff64 100755 --- a/openapi-generator/lib/src/openapi_generator_runner.dart +++ b/openapi-generator/lib/src/openapi_generator_runner.dart @@ -192,32 +192,41 @@ class OpenapiGenerator extends GeneratorForAnnotation { await runOpenApiJar(arguments: args); await fetchDependencies(baseCommand: baseCommand, args: args); await generateSources(baseCommand: baseCommand, args: args); - if (!args.hasLocalCache) { + if (args.disableCache) { logOutputMessage( log: log, communication: OutputMessage( - message: 'No local cache found. Creating one.', - level: Level.CONFIG, + message: 'Cache disabled. Skipping cache update.', ), ); } else { + if (!args.hasLocalCache) { + logOutputMessage( + log: log, + communication: OutputMessage( + message: 'No local cache found. Creating one.', + level: Level.CONFIG, + ), + ); + } else { + logOutputMessage( + log: log, + communication: OutputMessage( + message: 'Local cache found. Overwriting existing one.', + level: Level.CONFIG, + ), + ); + } + await cacheSpec( + outputLocation: args.cachePath, + spec: await loadSpec(specConfig: args.inputSpec)); logOutputMessage( log: log, communication: OutputMessage( - message: 'Local cache found. Overwriting existing one.', - level: Level.CONFIG, + message: 'Successfully cached spec changes.', ), ); } - await cacheSpec( - outputLocation: args.cachePath, - spec: await loadSpec(specConfig: args.inputSpec)); - logOutputMessage( - log: log, - communication: OutputMessage( - message: 'Successfully cached spec changes.', - ), - ); } } catch (e, st) { logOutputMessage(