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

✨ Support build.yaml to configure #555

Merged
merged 5 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,24 @@ flutter:
style: italic
```

### build.yaml

You can also configure generate options in the `build.yaml`, it will be read before the `pubspec.yaml` if it exists.


```yaml
# build.yaml
# ...

targets:
$default:
builders:
flutter_gen:
options:
output: lib/build_gen/ # Optional (default: lib/gen/)
line_length: 120 # Optional (default: 80)
```

## Available Parsers

### Assets
Expand Down Expand Up @@ -296,6 +314,28 @@ 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).
b2nkuu marked this conversation as resolved.
Show resolved Hide resolved

```yaml
# build.yaml
# ...

targets:
$default:
builders:
flutter_gen:
options:
output: lib/build_gen/ # Optional (default: lib/gen/)
line_length: 120 # Optional (default: 80)
```
##### Prority of configure (High -> Low)
```
build.yaml -> pubspec.yaml -> default_config
```


#### 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
48 changes: 37 additions & 11 deletions packages/core/lib/settings/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,49 @@ 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()
? join(basename(buildFile.parent.path), basename(buildFile.path))
: '';

stdout.writeln(
'$flutterGenVersion Loading ...',
);

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]);
stdout.writeln(
'Reading FlutterGen options from $pubspecLocaleHint',
);

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]);
stdout.writeln(
'Reading FlutterGen options from $buildLocaleHint',
);
}
}

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 @@ -179,5 +179,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);
});
});
}
19 changes: 19 additions & 0 deletions packages/core/test_resources/build_output.yaml
b2nkuu marked this conversation as resolved.
Show resolved Hide resolved
b2nkuu marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: test

targets:
$default:
builders:
flutter_gen:
options:
output: lib/build_gen/
line_length: 80

integrations:
flutter_svg: true
flare_flutter: true
rive: true
lottie: true

colors:
inputs:
- assets/color/colors.xml
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
Loading