Skip to content

Commit 8db8518

Browse files
samschendeljakemac53
authored andcommitted
Added field isStandalone to GeneratorBuilder to allow generation of a… (#86)
* Added field isStandalone to GeneratorBuilder to allow generation of a file that is not part of the source file. * Added tests for isStandalone builder option. * Throw error when 2+ generators used w/ standalone - When both more than one generator and isStandalone: true passed to GeneratorBuilder, an ArgumentError is thrown. - Wrote test to verify that this happens. * Actually have to include isStandalone in ArgumentError check. * Added test for non-standalone, multiple generators * Ran dartfmt.
1 parent 9280e26 commit 8db8518

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

lib/src/builder.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ import 'utils.dart';
1414
class GeneratorBuilder extends Builder {
1515
final List<Generator> generators;
1616
final String generatedExtension;
17+
final bool isStandalone;
1718

18-
GeneratorBuilder(this.generators, {this.generatedExtension: '.g.dart'}) {
19+
GeneratorBuilder(this.generators,
20+
{this.generatedExtension: '.g.dart', this.isStandalone: false}) {
1921
// TODO: validate that generatedExtension starts with a `.'
2022
// not null, empty, etc
23+
if (this.isStandalone && this.generators.length > 1) {
24+
throw new ArgumentError(
25+
'Only one generator can be used to generate a standalone file.');
26+
}
2127
}
2228

2329
@override
@@ -49,8 +55,10 @@ class GeneratorBuilder extends Builder {
4955

5056
var contentBuffer = new StringBuffer();
5157

52-
contentBuffer.writeln('part of ${library.name};');
53-
contentBuffer.writeln();
58+
if (!isStandalone) {
59+
contentBuffer.writeln('part of ${library.name};');
60+
contentBuffer.writeln();
61+
}
5462

5563
for (GeneratedOutput output in generatedOutputs) {
5664
contentBuffer.writeln('');

test/builder_test.dart

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,41 @@ void main() {
2626
{'$pkgName|lib/test_lib.g.dart': contains('not valid code!'),});
2727
});
2828

29+
test('Generate standalone output file', () async {
30+
var srcs = _createPackageStub(pkgName);
31+
var phaseGroup = new PhaseGroup.singleAction(
32+
new GeneratorBuilder([const CommentGenerator()], isStandalone: true),
33+
new InputSet(pkgName, ['lib/test_lib.dart']));
34+
await testPhases(phaseGroup, pkgName, srcs,
35+
{'$pkgName|lib/test_lib.g.dart': _testGenStandaloneContent,});
36+
});
37+
38+
test('Generate explicitly non-standalone output file', () async {
39+
var srcs = _createPackageStub(pkgName);
40+
var phaseGroup = new PhaseGroup.singleAction(
41+
new GeneratorBuilder([const CommentGenerator()], isStandalone: false),
42+
new InputSet(pkgName, ['lib/test_lib.dart']));
43+
await testPhases(phaseGroup, pkgName, srcs,
44+
{'$pkgName|lib/test_lib.g.dart': _testGenPartContent,});
45+
});
46+
47+
test('Expect error when multiple generators used on a standalone builder',
48+
() async {
49+
expect(
50+
() => new GeneratorBuilder(
51+
[const CommentGenerator(), const _NoOpGenerator()],
52+
isStandalone: true),
53+
throwsA(new isInstanceOf<ArgumentError>()));
54+
});
55+
56+
test('Expect no error when multiple generators used on nonstandalone builder',
57+
() async {
58+
expect(
59+
() => new GeneratorBuilder(
60+
[const CommentGenerator(), const _NoOpGenerator()]),
61+
returnsNormally);
62+
});
63+
2964
test(
3065
'Simple Generator test for library',
3166
() => _generateTest(
@@ -151,6 +186,23 @@ part of test_lib;
151186
// Code for "test_lib"
152187
''';
153188

189+
const _testGenStandaloneContent = r'''// GENERATED CODE - DO NOT MODIFY BY HAND
190+
191+
// **************************************************************************
192+
// Generator: CommentGenerator
193+
// Target: class Person
194+
// **************************************************************************
195+
196+
// Code for "class Person"
197+
198+
// **************************************************************************
199+
// Generator: CommentGenerator
200+
// Target: class Customer
201+
// **************************************************************************
202+
203+
// Code for "class Customer"
204+
''';
205+
154206
const _testGenPartContentForClassesAndLibrary =
155207
r'''// GENERATED CODE - DO NOT MODIFY BY HAND
156208

0 commit comments

Comments
 (0)