diff --git a/CHANGELOG.md b/CHANGELOG.md index f893f61..f252fc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,18 @@ +## 3.6.0 + +### Fixed updating the generated provider file +In this release we hopefully fixed the old problem with outdated `*.catalyst_builder.g.dart` files. + +#### Cause +The ServiceProviderBuilder did not emit an updated version since the `@GenerateServiceProvider` annotation +doesn't exist in the most files. + +#### Solution +We added a new `generatedProviderFile` option to the preflightBuilder configuration. You need to put the relative path +to the generated provider file (`*.catalyst_builder.g.dart`) in this option. +The PreflightBuilder will automatically delete the file if it exists. This lead to a full regeneration of the service +provider file. 🙌 + ## 3.5.2 ### Fix downgrade error diff --git a/README.md b/README.md index 8b0ccb7..60b036a 100644 --- a/README.md +++ b/README.md @@ -346,6 +346,10 @@ targets: $default: auto_apply_builders: true builders: + catalyst_builder|preflight: + options: + generatedProviderFile: 'lib/main.catalyst_builder.g.dart' # Enter the path to the generated service provider file + # This ensures that the file is reliably updated. catalyst_builder|buildServiceProvider: options: providerClassName: 'DefaultServiceProvider' # class name of the provider diff --git a/build.yaml b/build.yaml index 13a85fa..4a77cb4 100644 --- a/build.yaml +++ b/build.yaml @@ -6,6 +6,9 @@ builders: build_extensions: { '.dart': [ '.catalyst_builder.preflight.json' ] } build_to: cache auto_apply: dependents + defaults: + options: + generatedProviderFile: '' buildServiceProvider: import: 'package:catalyst_builder/src/builder/service_provider_builder.dart' diff --git a/example/build.yaml b/example/build.yaml index 850de8e..c7d81e5 100644 --- a/example/build.yaml +++ b/example/build.yaml @@ -2,6 +2,9 @@ targets: $default: auto_apply_builders: true builders: + catalyst_builder|preflight: + options: + generatedProviderFile: 'lib/example.catalyst_builder.g.dart' catalyst_builder|buildServiceProvider: options: providerClassName: 'ExampleProvider' diff --git a/lib/src/builder/preflight_builder.dart b/lib/src/builder/preflight_builder.dart index 5d879f9..c00be11 100644 --- a/lib/src/builder/preflight_builder.dart +++ b/lib/src/builder/preflight_builder.dart @@ -1,5 +1,6 @@ import 'dart:async'; import 'dart:convert'; +import 'dart:io' as io; import 'package:analyzer/dart/constant/value.dart'; import 'package:analyzer/dart/element/element.dart'; @@ -12,6 +13,10 @@ import 'dto/dto.dart'; /// The PreflightBuilder scans the files for @Service annotations. /// The result is stored in preflight.json files. class PreflightBuilder implements Builder { + final String _generatedProviderFile; + + PreflightBuilder(this._generatedProviderFile); + @override FutureOr build(BuildStep buildStep) async { if (!await buildStep.resolver.isLibrary(buildStep.inputId)) { @@ -20,19 +25,34 @@ class PreflightBuilder implements Builder { final entryLib = await buildStep.inputLibrary; - final preflightAsset = - buildStep.inputId.changeExtension('.catalyst_builder.preflight.json'); + await _deleteProviderFile(); + var extractedAnnotations = _extractAnnotations(entryLib); if (extractedAnnotations.services.isEmpty) { return; } + final preflightAsset = + buildStep.inputId.changeExtension('.catalyst_builder.preflight.json'); + await buildStep.writeAsString( preflightAsset, jsonEncode(extractedAnnotations), ); } + Future _deleteProviderFile() async { + if (_generatedProviderFile.isEmpty) { + return; + } + try { + var providerFile = io.File(_generatedProviderFile); + if (await providerFile.exists()) { + await providerFile.delete(); + } + } catch (_) {} + } + @override Map> get buildExtensions => { r'$lib$': [], @@ -219,4 +239,6 @@ class PreflightBuilder implements Builder { } /// Runs the preflight builder -Builder runPreflight(BuilderOptions options) => PreflightBuilder(); +Builder runPreflight(BuilderOptions options) => PreflightBuilder( + options.config['generatedProviderFile'].toString(), + ); diff --git a/pubspec.yaml b/pubspec.yaml index f278be3..4cbe7a7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: catalyst_builder description: A lightweight and easy to use dependency injection provider builder for dart. -version: 3.5.2 +version: 3.6.0 homepage: 'https://github.com/mintware-de/catalyst_builder' repository: 'https://github.com/mintware-de/catalyst_builder'