Skip to content

Commit

Permalink
feat: add keep config at build.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
b2nkuu committed Aug 17, 2024
1 parent 34eff4f commit 941dc2d
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 15 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion packages/command/bin/flutter_gen_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ void main(List<String> args) {
defaultsTo: 'pubspec.yaml',
);

parser.addOption(
'build',
abbr: 'b',
help: 'Set the path of build.yaml.',
defaultsTo: 'build.yaml',
);

parser.addFlag(
'help',
abbr: 'h',
Expand Down Expand Up @@ -45,5 +52,10 @@ void main(List<String> args) {
}

final pubspecPath = safeCast<String>(results['config']);
FlutterGenerator(File(pubspecPath!).absolute).build();
final pubspecFile = File(pubspecPath!).absolute;

final buildPath = safeCast<String>(results['build']);
final buildFile = File(buildPath!).absolute;

FlutterGenerator(pubspecFile, buildFile: buildFile).build();
}
4 changes: 3 additions & 1 deletion packages/core/lib/flutter_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> build({Config? config, FileWriter? writer}) async {
config ??= loadPubspecConfigOrNull(pubspecFile);
config ??= loadPubspecConfigOrNull(pubspecFile, buildFile: buildFile);
if (config == null) return;

final flutter = config.pubspec.flutter;
Expand Down
45 changes: 34 additions & 11 deletions packages/core/lib/settings/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 22 additions & 0 deletions packages/core/test/flutter_gen_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
}
8 changes: 8 additions & 0 deletions packages/core/test_resources/build_output.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: test

targets:
$default:
builders:
flutter_gen:
options:
output: lib/build_gen/
11 changes: 9 additions & 2 deletions packages/runner/lib/flutter_gen_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 941dc2d

Please sign in to comment.