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 Nov 22, 2023
1 parent f3ef7c3 commit a758d6a
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 15 deletions.
5 changes: 5 additions & 0 deletions packages/dynamite/dynamite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,10 @@ 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

```
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,66 @@ 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;

/// Whether to apply code optimizations.
///
/// The built code will adhere more to the dart code style but is equally as valid.
bool get applyOptimizations;

@BuiltValueHook(initializeBuilder: true)
static void _defaults(final DynamiteConfigBuilder b) {
b.applyOptimizations = true;
}

/// 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 @@ -36,15 +36,15 @@ class OpenAPIBuilder implements Builder {

@override
Future<void> build(final BuildStep buildStep) async {
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 @@ -62,7 +62,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 a758d6a

Please sign in to comment.