Skip to content

Commit

Permalink
Merge pull request #145 from ricardoboss/issues/144-disable-cache
Browse files Browse the repository at this point in the history
feat: Added option to disable caching
  • Loading branch information
gibahjoe authored Nov 5, 2024
2 parents 6224377 + d1ae279 commit 7f8770b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,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,
Expand All @@ -150,7 +154,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.
Expand Down
7 changes: 6 additions & 1 deletion openapi-generator/lib/src/models/generator_arguments.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
37 changes: 23 additions & 14 deletions openapi-generator/lib/src/openapi_generator_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,32 +192,41 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
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(
Expand Down

0 comments on commit 7f8770b

Please sign in to comment.