Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update the generated provider file #22

Merged
merged 1 commit into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 3 additions & 0 deletions example/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
28 changes: 25 additions & 3 deletions lib/src/builder/preflight_builder.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<void> build(BuildStep buildStep) async {
if (!await buildStep.resolver.isLibrary(buildStep.inputId)) {
Expand All @@ -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<void> _deleteProviderFile() async {
if (_generatedProviderFile.isEmpty) {
return;
}
try {
var providerFile = io.File(_generatedProviderFile);
if (await providerFile.exists()) {
await providerFile.delete();
}
} catch (_) {}
}

@override
Map<String, List<String>> get buildExtensions => {
r'$lib$': [],
Expand Down Expand Up @@ -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(),
);
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
Loading