From 56c1ce025923b50d01029201f2c525b41ffd5db8 Mon Sep 17 00:00:00 2001 From: Klemen Tusar Date: Mon, 22 Jan 2024 18:46:27 +0000 Subject: [PATCH] :bookmark: release chopper_generator v7.1.1 (#563) --- .github/dependabot.yml | 5 ++ chopper_generator/CHANGELOG.md | 4 ++ .../lib/src/builder_factory.dart | 64 +++++++++++++++---- chopper_generator/pubspec.yaml | 3 +- 4 files changed, 62 insertions(+), 14 deletions(-) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 68152415..cc598d95 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -1,5 +1,10 @@ version: 2 updates: + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + target-branch: "develop" - package-ecosystem: "pub" directory: "/chopper" schedule: diff --git a/chopper_generator/CHANGELOG.md b/chopper_generator/CHANGELOG.md index 6a1cd984..08e09416 100644 --- a/chopper_generator/CHANGELOG.md +++ b/chopper_generator/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 7.1.1 + +- Add option to override build_extension via build.yaml ([#562](https://github.com/lejard-h/chopper/pull/562)) + ## 7.1.0 - Add ability to omit `Response` in service ([#545](https://github.com/lejard-h/chopper/pull/545)) diff --git a/chopper_generator/lib/src/builder_factory.dart b/chopper_generator/lib/src/builder_factory.dart index f5ff89d1..34e6108f 100644 --- a/chopper_generator/lib/src/builder_factory.dart +++ b/chopper_generator/lib/src/builder_factory.dart @@ -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'; +} diff --git a/chopper_generator/pubspec.yaml b/chopper_generator/pubspec.yaml index 56c4f80a..43870f85 100644 --- a/chopper_generator/pubspec.yaml +++ b/chopper_generator/pubspec.yaml @@ -1,6 +1,6 @@ name: chopper_generator description: Chopper is an http client generator using source_gen, inspired by Retrofit -version: 7.1.0 +version: 7.1.1 documentation: https://hadrien-lejard.gitbook.io/chopper repository: https://github.com/lejard-h/chopper @@ -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