Skip to content

Commit

Permalink
✨ add option to override build_extension (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse authored Jan 21, 2024
1 parent 78fae30 commit d369217
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 13 deletions.
64 changes: 51 additions & 13 deletions chopper_generator/lib/src/builder_factory.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,61 @@
import 'package:build/build.dart';
import 'package:chopper/chopper.dart' show ChopperApi;
import 'package:source_gen/source_gen.dart';
import 'package:yaml/yaml.dart';

import 'generator.dart';

/// Creates a [PartBuilder] used to generate code for [ChopperApi] annotated
/// classes. The [options] are provided by Dart's build system and read from the
/// `build.yaml` file.
Builder chopperGeneratorFactory(BuilderOptions options) => PartBuilder(
Builder chopperGeneratorFactory(BuilderOptions options) {
final String buildExtension = _getBuildExtension(options);

return PartBuilder(
[const ChopperGenerator()],
buildExtension,
header: options.config['header'],
formatOutput: PartBuilder(
[const ChopperGenerator()],
'.chopper.dart',
header: options.config['header'],
formatOutput:
PartBuilder([const ChopperGenerator()], '.chopper.dart').formatOutput,
options: !options.config.containsKey('build_extensions')
? options.overrideWith(
BuilderOptions({
'build_extensions': {'.dart': '.chopper.dart'},
}),
)
: options,
);
buildExtension,
).formatOutput,
options: !options.config.containsKey('build_extensions')
? options.overrideWith(
BuilderOptions({
'build_extensions': {
'.dart': [buildExtension]
},
}),
)
: options,
);
}

/// Returns the build extension for the generated file.
///
/// If the `build.yaml` file contains a `build_extensions` key, it will be used
/// to determine the extension. Otherwise, the default extension `.chopper.dart`
/// will be used.
///
/// Example `build.yaml`:
///
/// ```yaml
/// targets:
/// $default:
/// builders:
/// chopper_generator:
/// options:
/// build_extensions: {".dart": [".chopper.g.dart"]}
/// ```
String _getBuildExtension(BuilderOptions options) {
if (options.config.containsKey('build_extensions')) {
final YamlMap buildExtensions = options.config['build_extensions'];
if (buildExtensions.containsKey('.dart')) {
final YamlList dartBuildExtensions = buildExtensions['.dart'];
if (dartBuildExtensions.isNotEmpty) {
return dartBuildExtensions.first;
}
}
}
return '.chopper.dart';
}
1 change: 1 addition & 0 deletions chopper_generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies:
logging: ^1.2.0
meta: ^1.9.1
source_gen: ^1.4.0
yaml: ^3.1.2

dev_dependencies:
build_runner: ^2.4.6
Expand Down
16 changes: 16 additions & 0 deletions faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,22 @@ It goes without saying that running the code generation is a pre-requisite at th
flutter pub run build_runner build
```

##### Changing the default extension of the generated files

If you want to change the default extension of the generated files from `.chopper.dart` to
something else, you can do so by adding the following to your `build.yaml` file:

```yaml
targets:
$default:
builders:
chopper_generator:
options:
# This assumes you want the files to end with `.chopper.g.dart`
# instead of the default `.chopper.dart`.
build_extensions: { ".dart": [ ".chopper.g.dart" ] }
```
#### Configure a WorkerPool and run the example
```dart
Expand Down

0 comments on commit d369217

Please sign in to comment.