From 941dc2d04ff69dde29ef033297e81ee91f5e8bca Mon Sep 17 00:00:00 2001 From: b2nkuu Date: Sat, 17 Aug 2024 14:46:20 +0700 Subject: [PATCH] feat: add keep config at build.yaml --- README.md | 17 +++++++ packages/command/bin/flutter_gen_command.dart | 14 +++++- packages/core/lib/flutter_generator.dart | 4 +- packages/core/lib/settings/config.dart | 45 ++++++++++++++----- packages/core/test/flutter_gen_test.dart | 22 +++++++++ .../core/test_resources/build_output.yaml | 8 ++++ packages/runner/lib/flutter_gen_runner.dart | 11 ++++- 7 files changed, 106 insertions(+), 15 deletions(-) create mode 100644 packages/core/test_resources/build_output.yaml diff --git a/README.md b/README.md index 17ac5980..bbe41b68 100644 --- a/README.md +++ b/README.md @@ -267,6 +267,23 @@ class $AssetsImagesGen { } ``` +#### Support generate at **build.yaml** +Support generates dart files based on the key **`targets.$default.builders`** and **`flutter_gen.options`** +of [`build.yaml`](https://github.com/dart-lang/build/blob/master/build_config/README.md). + +```yaml +# build.yaml +# ... + +targets: + $default: + builders: + flutter_gen: + options: + output: lib/build_gen/ # Optional (default: lib/gen/) + line_length: 120 # Optional (default: 80) +``` + #### Including additional metadata At build time, additional metadata may be included in the generated class, by using the diff --git a/packages/command/bin/flutter_gen_command.dart b/packages/command/bin/flutter_gen_command.dart index 3ed33f6d..cee70e64 100644 --- a/packages/command/bin/flutter_gen_command.dart +++ b/packages/command/bin/flutter_gen_command.dart @@ -14,6 +14,13 @@ void main(List args) { defaultsTo: 'pubspec.yaml', ); + parser.addOption( + 'build', + abbr: 'b', + help: 'Set the path of build.yaml.', + defaultsTo: 'build.yaml', + ); + parser.addFlag( 'help', abbr: 'h', @@ -45,5 +52,10 @@ void main(List args) { } final pubspecPath = safeCast(results['config']); - FlutterGenerator(File(pubspecPath!).absolute).build(); + final pubspecFile = File(pubspecPath!).absolute; + + final buildPath = safeCast(results['build']); + final buildFile = File(buildPath!).absolute; + + FlutterGenerator(pubspecFile, buildFile: buildFile).build(); } diff --git a/packages/core/lib/flutter_generator.dart b/packages/core/lib/flutter_generator.dart index c683e8af..ec35cd09 100644 --- a/packages/core/lib/flutter_generator.dart +++ b/packages/core/lib/flutter_generator.dart @@ -11,18 +11,20 @@ import 'package:path/path.dart'; class FlutterGenerator { const FlutterGenerator( this.pubspecFile, { + this.buildFile, this.assetsName = 'assets.gen.dart', this.colorsName = 'colors.gen.dart', this.fontsName = 'fonts.gen.dart', }); final File pubspecFile; + final File? buildFile; final String assetsName; final String colorsName; final String fontsName; Future build({Config? config, FileWriter? writer}) async { - config ??= loadPubspecConfigOrNull(pubspecFile); + config ??= loadPubspecConfigOrNull(pubspecFile, buildFile: buildFile); if (config == null) return; final flutter = config.pubspec.flutter; diff --git a/packages/core/lib/settings/config.dart b/packages/core/lib/settings/config.dart index b9018e88..452690e0 100644 --- a/packages/core/lib/settings/config.dart +++ b/packages/core/lib/settings/config.dart @@ -15,23 +15,46 @@ class Config { final File pubspecFile; } -Config loadPubspecConfig(File pubspecFile) { - stdout.writeln('$flutterGenVersion Loading ... ' - '${normalize(join( - basename(pubspecFile.parent.path), - basename(pubspecFile.path), - ))}'); - final content = pubspecFile.readAsStringSync(); - final userMap = loadYaml(content) as Map?; +Config loadPubspecConfig(File pubspecFile, {File? buildFile}) { + final pubspecLocaleHint = normalize( + join(basename(pubspecFile.parent.path), basename(pubspecFile.path)), + ); + final buildLocaleHint = buildFile != null && buildFile.existsSync() + ? ', ${normalize( + join(basename(buildFile.parent.path), basename(buildFile.path)), + )} ' + : ''; + + stdout.writeln( + '$flutterGenVersion Loading ... $pubspecLocaleHint$buildLocaleHint', + ); + final defaultMap = loadYaml(configDefaultYamlContent) as Map?; - final mergedMap = mergeMap([defaultMap, userMap]); + + final pubspecContent = pubspecFile.readAsStringSync(); + final pubspecMap = loadYaml(pubspecContent) as Map?; + + var mergedMap = mergeMap([defaultMap, pubspecMap]); + + if (buildFile != null && buildFile.existsSync()) { + final buildContent = buildFile.readAsStringSync(); + final rawMap = loadYaml(buildContent) as Map?; + final optionBuildMap = rawMap?['targets']?[r'$default']?['builders'] + ?['flutter_gen']?['options']; + + if (optionBuildMap != null) { + final buildMap = {'flutter_gen': optionBuildMap}; + mergedMap = mergeMap([mergedMap, buildMap]); + } + } + final pubspec = Pubspec.fromJson(mergedMap); return Config._(pubspec: pubspec, pubspecFile: pubspecFile); } -Config? loadPubspecConfigOrNull(File pubspecFile) { +Config? loadPubspecConfigOrNull(File pubspecFile, {File? buildFile}) { try { - return loadPubspecConfig(pubspecFile); + return loadPubspecConfig(pubspecFile, buildFile: buildFile); } on FileSystemException catch (e) { stderr.writeln(e.message); } on InvalidSettingsException catch (e) { diff --git a/packages/core/test/flutter_gen_test.dart b/packages/core/test/flutter_gen_test.dart index cdcf03f4..4be77622 100644 --- a/packages/core/test/flutter_gen_test.dart +++ b/packages/core/test/flutter_gen_test.dart @@ -180,5 +180,27 @@ void main() { expect(File('test_resources/lib/gen/$fonts').existsSync(), false); expect(File('test_resources/lib/gen/$colors').existsSync(), false); }); + + test('with build_output.yaml', () async { + const pubspec = 'test_resources/pubspec_normal.yaml'; + const build = 'test_resources/build_output.yaml'; + const assets = 'pubspec_assets.gen.dart'; + const colors = 'pubspec_colors.gen.dart'; + const fonts = 'pubspec_fonts.gen.dart'; + + await FlutterGenerator( + File(pubspec), + buildFile: File(build), + assetsName: assets, + colorsName: colors, + fontsName: fonts, + ).build(); + expect(File('test_resources/lib/build_gen/$assets').readAsStringSync(), + isNotEmpty); + expect(File('test_resources/lib/build_gen/$fonts').readAsStringSync(), + isNotEmpty); + expect(File('test_resources/lib/build_gen/$colors').readAsStringSync(), + isNotEmpty); + }); }); } diff --git a/packages/core/test_resources/build_output.yaml b/packages/core/test_resources/build_output.yaml new file mode 100644 index 00000000..7a458884 --- /dev/null +++ b/packages/core/test_resources/build_output.yaml @@ -0,0 +1,8 @@ +name: test + +targets: + $default: + builders: + flutter_gen: + options: + output: lib/build_gen/ \ No newline at end of file diff --git a/packages/runner/lib/flutter_gen_runner.dart b/packages/runner/lib/flutter_gen_runner.dart index 47c085fd..ace44a0c 100644 --- a/packages/runner/lib/flutter_gen_runner.dart +++ b/packages/runner/lib/flutter_gen_runner.dart @@ -21,8 +21,15 @@ class FlutterGenBuilder extends Builder { ); } - final generator = FlutterGenerator(File('pubspec.yaml')); - late final _config = loadPubspecConfigOrNull(generator.pubspecFile); + final generator = FlutterGenerator( + File('pubspec.yaml'), + buildFile: File('build.yaml'), + ); + + late final _config = loadPubspecConfigOrNull( + generator.pubspecFile, + buildFile: generator.buildFile, + ); _FlutterGenBuilderState? _currentState; @override