Skip to content

Commit c4dfd1d

Browse files
authored
Add support for build_extensions configuration of builders producing multiple files (#647)
Allow lists or single strings in the options.
1 parent f1011be commit c4dfd1d

File tree

4 files changed

+74
-13
lines changed

4 files changed

+74
-13
lines changed

source_gen/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.3.0
2+
3+
* Add support for `build_extensions` configuration of builders producing multiple files. Eg:
4+
`build_extensions: { '.dart': ['.stub.dart', '.web.dart', '.vm.dart'] }`
5+
16
## 1.2.7
27

38
* Update the value of the pubspec `repository` field.

source_gen/lib/src/utils.dart

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,11 @@ Map<String, List<String>> validatedBuildExtensionsFrom(
176176
Map<String, List<String>> defaultExtensions,
177177
) {
178178
final extensionsOption = optionsMap?.remove('build_extensions');
179-
if (extensionsOption == null) return defaultExtensions;
179+
if (extensionsOption == null) {
180+
// defaultExtensions are provided by the builder author, not the end user.
181+
// It should be safe to skip validation.
182+
return defaultExtensions;
183+
}
180184

181185
if (extensionsOption is! Map) {
182186
throw ArgumentError(
@@ -195,15 +199,19 @@ Map<String, List<String>> validatedBuildExtensionsFrom(
195199
);
196200
}
197201

198-
final output = entry.value;
199-
if (output is! String || !output.endsWith('.dart')) {
200-
throw ArgumentError(
201-
'Invalid output extension `$output`. It should be a '
202-
'string ending with `.dart`',
203-
);
202+
final output = (entry.value is List) ? entry.value as List : [entry.value];
203+
204+
for (var i = 0; i < output.length; i++) {
205+
final o = output[i];
206+
if (o is! String || (i == 0 && !o.endsWith('.dart'))) {
207+
throw ArgumentError(
208+
'Invalid output extension `${entry.value}`. It should be a string '
209+
'or a list of strings with the first ending with `.dart`',
210+
);
211+
}
204212
}
205213

206-
result[input] = [output];
214+
result[input] = output.cast<String>().toList();
207215
}
208216

209217
if (result.isEmpty) {

source_gen/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: source_gen
2-
version: 1.2.7
2+
version: 1.3.0
33
description: >-
44
Source code generation builders and utilities for the Dart build system
55
repository: https://github.com/dart-lang/source_gen/tree/master/source_gen

source_gen/test/utils_test.dart

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,42 @@ void main() {
4646

4747
group('validatedBuildExtensionsFrom', () {
4848
test('no option given -> return defaultBuildExtension ', () {
49-
final buildEtension = validatedBuildExtensionsFrom({}, {
49+
final buildExtension = validatedBuildExtensionsFrom({}, {
5050
'.dart': ['.foo.dart'],
5151
});
52-
expect(buildEtension, {
52+
expect(buildExtension, {
5353
'.dart': ['.foo.dart'],
5454
});
5555
});
5656

57+
test('allows multiple output extensions', () {
58+
final buildExtensions = validatedBuildExtensionsFrom(
59+
{
60+
'build_extensions': {
61+
'.dart': ['.g.dart', '.h.dart']
62+
}
63+
},
64+
{},
65+
);
66+
expect(buildExtensions, {
67+
'.dart': ['.g.dart', '.h.dart'],
68+
});
69+
});
70+
71+
test('allows multiple output extensions of various types ', () {
72+
final buildExtensions = validatedBuildExtensionsFrom(
73+
{
74+
'build_extensions': {
75+
'.dart': ['.g.dart', '.swagger.json']
76+
}
77+
},
78+
{},
79+
);
80+
expect(buildExtensions, {
81+
'.dart': ['.g.dart', '.swagger.json'],
82+
});
83+
});
84+
5785
test("disallows options that aren't a map", () {
5886
expect(
5987
() => validatedBuildExtensionsFrom({'build_extensions': 'foo'}, {}),
@@ -104,8 +132,28 @@ void main() {
104132
isArgumentError.having(
105133
(e) => e.message,
106134
'message',
107-
'Invalid output extension `.out`. It should be a string ending '
108-
'with `.dart`',
135+
'Invalid output extension `.out`. It should be a string or a list '
136+
'of strings with the first ending with `.dart`',
137+
),
138+
),
139+
);
140+
141+
expect(
142+
() => validatedBuildExtensionsFrom(
143+
{
144+
'build_extensions': {
145+
'.dart': ['.out', '.g.dart']
146+
}
147+
},
148+
{},
149+
),
150+
throwsA(
151+
isArgumentError.having(
152+
(e) => e.message,
153+
'message',
154+
'Invalid output extension `[.out, .g.dart]`. It should be a '
155+
'string or a list of strings with the first ending with '
156+
'`.dart`',
109157
),
110158
),
111159
);

0 commit comments

Comments
 (0)