From a7fa12b6ed25192299e434ee438254ed3ca813ad Mon Sep 17 00:00:00 2001 From: David MARKEY <1548408+d-markey@users.noreply.github.com> Date: Thu, 23 Feb 2023 23:50:38 +0100 Subject: [PATCH] Add support for `build_extensions` configuration of builders producing multiple files (dart-lang/source_gen#647) Allow lists or single strings in the options. --- pkgs/source_gen/source_gen/CHANGELOG.md | 5 ++ pkgs/source_gen/source_gen/lib/src/utils.dart | 24 +++++--- pkgs/source_gen/source_gen/pubspec.yaml | 2 +- .../source_gen/test/utils_test.dart | 56 +++++++++++++++++-- 4 files changed, 74 insertions(+), 13 deletions(-) diff --git a/pkgs/source_gen/source_gen/CHANGELOG.md b/pkgs/source_gen/source_gen/CHANGELOG.md index b765fdcbe..04b84b4c1 100644 --- a/pkgs/source_gen/source_gen/CHANGELOG.md +++ b/pkgs/source_gen/source_gen/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.3.0 + +* Add support for `build_extensions` configuration of builders producing multiple files. Eg: + `build_extensions: { '.dart': ['.stub.dart', '.web.dart', '.vm.dart'] }` + ## 1.2.7 * Update the value of the pubspec `repository` field. diff --git a/pkgs/source_gen/source_gen/lib/src/utils.dart b/pkgs/source_gen/source_gen/lib/src/utils.dart index c8f2d4633..1ad029165 100644 --- a/pkgs/source_gen/source_gen/lib/src/utils.dart +++ b/pkgs/source_gen/source_gen/lib/src/utils.dart @@ -176,7 +176,11 @@ Map> validatedBuildExtensionsFrom( Map> defaultExtensions, ) { final extensionsOption = optionsMap?.remove('build_extensions'); - if (extensionsOption == null) return defaultExtensions; + if (extensionsOption == null) { + // defaultExtensions are provided by the builder author, not the end user. + // It should be safe to skip validation. + return defaultExtensions; + } if (extensionsOption is! Map) { throw ArgumentError( @@ -195,15 +199,19 @@ Map> validatedBuildExtensionsFrom( ); } - final output = entry.value; - if (output is! String || !output.endsWith('.dart')) { - throw ArgumentError( - 'Invalid output extension `$output`. It should be a ' - 'string ending with `.dart`', - ); + final output = (entry.value is List) ? entry.value as List : [entry.value]; + + for (var i = 0; i < output.length; i++) { + final o = output[i]; + if (o is! String || (i == 0 && !o.endsWith('.dart'))) { + throw ArgumentError( + 'Invalid output extension `${entry.value}`. It should be a string ' + 'or a list of strings with the first ending with `.dart`', + ); + } } - result[input] = [output]; + result[input] = output.cast().toList(); } if (result.isEmpty) { diff --git a/pkgs/source_gen/source_gen/pubspec.yaml b/pkgs/source_gen/source_gen/pubspec.yaml index e9ebedb5f..b841ab869 100644 --- a/pkgs/source_gen/source_gen/pubspec.yaml +++ b/pkgs/source_gen/source_gen/pubspec.yaml @@ -1,5 +1,5 @@ name: source_gen -version: 1.2.7 +version: 1.3.0 description: >- Source code generation builders and utilities for the Dart build system repository: https://github.com/dart-lang/source_gen/tree/master/source_gen diff --git a/pkgs/source_gen/source_gen/test/utils_test.dart b/pkgs/source_gen/source_gen/test/utils_test.dart index f975bd41b..ab123168b 100644 --- a/pkgs/source_gen/source_gen/test/utils_test.dart +++ b/pkgs/source_gen/source_gen/test/utils_test.dart @@ -46,14 +46,42 @@ void main() { group('validatedBuildExtensionsFrom', () { test('no option given -> return defaultBuildExtension ', () { - final buildEtension = validatedBuildExtensionsFrom({}, { + final buildExtension = validatedBuildExtensionsFrom({}, { '.dart': ['.foo.dart'], }); - expect(buildEtension, { + expect(buildExtension, { '.dart': ['.foo.dart'], }); }); + test('allows multiple output extensions', () { + final buildExtensions = validatedBuildExtensionsFrom( + { + 'build_extensions': { + '.dart': ['.g.dart', '.h.dart'] + } + }, + {}, + ); + expect(buildExtensions, { + '.dart': ['.g.dart', '.h.dart'], + }); + }); + + test('allows multiple output extensions of various types ', () { + final buildExtensions = validatedBuildExtensionsFrom( + { + 'build_extensions': { + '.dart': ['.g.dart', '.swagger.json'] + } + }, + {}, + ); + expect(buildExtensions, { + '.dart': ['.g.dart', '.swagger.json'], + }); + }); + test("disallows options that aren't a map", () { expect( () => validatedBuildExtensionsFrom({'build_extensions': 'foo'}, {}), @@ -104,8 +132,28 @@ void main() { isArgumentError.having( (e) => e.message, 'message', - 'Invalid output extension `.out`. It should be a string ending ' - 'with `.dart`', + 'Invalid output extension `.out`. It should be a string or a list ' + 'of strings with the first ending with `.dart`', + ), + ), + ); + + expect( + () => validatedBuildExtensionsFrom( + { + 'build_extensions': { + '.dart': ['.out', '.g.dart'] + } + }, + {}, + ), + throwsA( + isArgumentError.having( + (e) => e.message, + 'message', + 'Invalid output extension `[.out, .g.dart]`. It should be a ' + 'string or a list of strings with the first ending with ' + '`.dart`', ), ), );