|
| 1 | +import 'dart:collection'; |
| 2 | +import 'dart:io'; |
| 3 | + |
1 | 4 | import 'package:build/build.dart'; |
| 5 | +import 'package:collection/collection.dart'; |
| 6 | +import 'package:crypto/crypto.dart'; |
| 7 | +import 'package:flutter_gen_core/flutter_generator.dart'; |
| 8 | +import 'package:flutter_gen_core/settings/config.dart'; |
| 9 | + |
| 10 | +import 'package:glob/glob.dart'; |
| 11 | + |
| 12 | +Builder build(BuilderOptions options) => FlutterGenBuilder(); |
| 13 | + |
| 14 | +class FlutterGenBuilder extends Builder { |
| 15 | + final FlutterGenerator generator = FlutterGenerator(File('pubspec.yaml')); |
| 16 | + _FlutterGenBuilderState? _currentState; |
| 17 | + |
| 18 | + @override |
| 19 | + Future<void> build(BuildStep buildStep) async { |
| 20 | + final config = await generator.getConfig(); |
| 21 | + if (config == null) return; |
| 22 | + |
| 23 | + final state = await _createState(config, buildStep); |
| 24 | + if (_currentState != null && _currentState!.equals(state)) return; |
| 25 | + _currentState = state; |
| 26 | + |
| 27 | + await generator.build(config: config); |
| 28 | + } |
| 29 | + |
| 30 | + @override |
| 31 | + Map<String, List<String>> get buildExtensions => { |
| 32 | + "\$package\$": ['.gen.dart'] |
| 33 | + }; |
| 34 | + |
| 35 | + Future<_FlutterGenBuilderState> _createState( |
| 36 | + Config config, BuildStep buildStep) async { |
| 37 | + final pubspec = config.pubspec; |
| 38 | + |
| 39 | + final HashSet<String> assets = HashSet(); |
| 40 | + if (pubspec.flutterGen.assets.enabled) { |
| 41 | + for (var assetInput in pubspec.flutter.assets) { |
| 42 | + if (assetInput.isEmpty) continue; |
| 43 | + if (assetInput.endsWith("/")) assetInput += "*"; |
| 44 | + await for (var assetId in buildStep.findAssets(Glob(assetInput))) { |
| 45 | + assets.add(assetId.path); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + final HashMap<String, Digest> colors = HashMap(); |
| 51 | + if (pubspec.flutterGen.colors.enabled) { |
| 52 | + for (var colorInput in pubspec.flutterGen.colors.inputs) { |
| 53 | + if (colorInput.isEmpty) continue; |
| 54 | + await for (var assetId in buildStep.findAssets(Glob(colorInput))) { |
| 55 | + final digest = await buildStep.digest(assetId); |
| 56 | + colors[assetId.path] = digest; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + final pubspecAsset = |
| 62 | + await buildStep.findAssets(Glob(config.pubspecFile.path)).single; |
| 63 | + |
| 64 | + final pubspecDigest = await buildStep.digest(pubspecAsset); |
| 65 | + |
| 66 | + return _FlutterGenBuilderState(pubspecDigest, assets, colors); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +class _FlutterGenBuilderState { |
| 71 | + final Digest pubspecDigest; |
| 72 | + final HashSet<String> assets; |
| 73 | + final HashMap<String, Digest> colors; |
2 | 74 |
|
3 | | -import 'flutter_gen_builder.dart'; |
| 75 | + _FlutterGenBuilderState(this.pubspecDigest, this.assets, this.colors); |
4 | 76 |
|
5 | | -Builder build(BuilderOptions options) { |
6 | | - return FlutterGenBuilder(); |
| 77 | + bool equals(_FlutterGenBuilderState state) { |
| 78 | + return pubspecDigest == state.pubspecDigest && |
| 79 | + const SetEquality().equals(assets, state.assets) && |
| 80 | + const MapEquality().equals(colors, state.colors); |
| 81 | + } |
7 | 82 | } |
0 commit comments