Skip to content

Commit

Permalink
feat(dynamite): allow per spec configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolas Rimikis <[email protected]>
  • Loading branch information
Leptopoda committed Dec 21, 2023
1 parent 119dfe9 commit 2f393a7
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 13 deletions.
5 changes: 5 additions & 0 deletions packages/dynamite/dynamite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ targets:
- 'factory .*\.fromJson\(Map<String, dynamic> json\) => jsonSerializers\.deserializeWith\(serializer, json\)!;'
- 'Map<String, dynamic> toJson\(\) => jsonSerializers\.serializeWith\(serializer, this\)! as Map<String, dynamic>;'
- 'static BuiltSet<.*> get values => _\$.*Values;'
overrides:
# Tighten linting rules for specific specs
lib/my_spec.openapi.json:
analyzer_ignores:
- camel_case_types
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,56 @@ abstract class DynamiteConfig implements Built<DynamiteConfig, DynamiteConfigBui

/// The specified line with the formatter should use.
int? get pageWidth;

/// A collection of dynamite configs for a specific file.
///
/// This parameter is only supported for the top level config.
// overrides is reserved in the build.yaml
BuiltMap<String, DynamiteConfig>? get overrides;

/// Gets the config for the given [uri].
///
/// Returns this if no override is set.
DynamiteConfig configFor(final String uri) {
if (overrides == null) {
return this;
}

DynamiteConfig? override;
for (final entry in overrides!.entries) {
if (RegExp(entry.key).hasMatch(uri)) {
override = entry.value;
break;
}
}

if (override == null) {
return this;
}

return merge(override);
}

/// Merges `this` config with the given [override].
///
/// Throws a [ArgumentError] if the override has overrides itself.
DynamiteConfig merge(final DynamiteConfig other) {
if (other.overrides != null) {
throw ArgumentError('Configs can only be merged with a config that does not have further overrides');
}

return rebuild((final b) {
final analyzerIgnores = other.analyzerIgnores;
if (analyzerIgnores != null) {
b.analyzerIgnores.replace(analyzerIgnores);
}

final coverageIgnores = other.coverageIgnores;
if (coverageIgnores != null) {
b.coverageIgnores.replace(coverageIgnores);
}
});
}
}

@SerializersFor([
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions packages/dynamite/dynamite/lib/src/openapi_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ class OpenAPIBuilder implements Builder {
}
}

try {
final inputId = buildStep.inputId;
final outputId = inputId.changeExtension('.dart');
final inputId = buildStep.inputId;
final outputId = inputId.changeExtension('.dart');

final emitter = DartEmitter(
orderDirectives: true,
useNullSafetySyntax: true,
);
final emitter = DartEmitter(
orderDirectives: true,
useNullSafetySyntax: true,
);

try {
final spec = switch (inputId.extension) {
'.json' => openapi.serializers.deserializeWith(
openapi.OpenAPI.serializer,
Expand All @@ -76,7 +76,8 @@ class OpenAPIBuilder implements Builder {
throw Exception('Only OpenAPI between $minSupportedVersion and $maxSupportedVersion are supported.');
}

final state = State(buildConfig);
final config = buildConfig.configFor(inputId.path);
final state = State(config);

// Imports need to be generated after everything else so we know if we need the local part directive,
// but they need to be added to the beginning of the output.
Expand Down

0 comments on commit 2f393a7

Please sign in to comment.