Skip to content

Commit

Permalink
Added option to disable caching
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss committed Apr 12, 2024
1 parent 2eab4ab commit 4d5daaf
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 @@ -111,6 +111,10 @@ class Openapi {
/// Include in depth logging output from run commands.
final bool debugLogging;

/// 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 @@ -129,7 +133,8 @@ class Openapi {
this.cachePath,
this.projectPubspecPath,
this.debugLogging = false,
});
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 @@ -112,7 +116,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 @@ -170,32 +170,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 4d5daaf

Please sign in to comment.