Skip to content

Commit d4a1f27

Browse files
feature: Optionally omit generated description comments in output (#720)
Related: #706. This PR adds an optional `bool? writesDescriptions` field to the `_Builder` base class constructor. By default, it is `true`, but if explicitly set to `false`, the generated description comments will not be included in the generated output. See related link for my use-case. This will be useful so that _any_ file can be generated with this tool for Dart.
1 parent ae17dde commit d4a1f27

File tree

3 files changed

+93
-10
lines changed

3 files changed

+93
-10
lines changed

source_gen/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Support all the glob quotes.
1111
- Require `analyzer: ^6.9.0`
1212
- Require Dart 3.5.0
13+
- `LibraryBuilder`, `PartBuilder`, and `SharedPartBuilder` now take an optional `writeDescriptions` boolean. When set to `false`, headers and generator descriptions for the files will not be included in the builder output.
1314

1415
## 1.5.0
1516

source_gen/lib/src/builder.dart

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class _Builder extends Builder {
3535

3636
final String _header;
3737

38+
/// Whether to include or emit the gen part descriptions. Defaults to true.
39+
final bool _writeDescriptions;
40+
3841
/// Whether to allow syntax errors in input libraries.
3942
final bool allowSyntaxErrors;
4043

@@ -51,6 +54,7 @@ class _Builder extends Builder {
5154
String generatedExtension = '.g.dart',
5255
List<String> additionalOutputExtensions = const [],
5356
String? header,
57+
bool? writeDescriptions,
5458
this.allowSyntaxErrors = false,
5559
BuilderOptions? options,
5660
}) : _generatedExtension = generatedExtension,
@@ -61,6 +65,7 @@ class _Builder extends Builder {
6165
...additionalOutputExtensions,
6266
],
6367
}),
68+
_writeDescriptions = writeDescriptions ?? true,
6469
_header = (header ?? defaultFileHeader).trim() {
6570
if (_generatedExtension.isEmpty || !_generatedExtension.startsWith('.')) {
6671
throw ArgumentError.value(
@@ -153,16 +158,19 @@ class _Builder extends Builder {
153158
}
154159

155160
for (var item in generatedOutputs) {
156-
contentBuffer
157-
..writeln()
158-
..writeln(_headerLine)
159-
..writeAll(
160-
LineSplitter.split(item.generatorDescription)
161-
.map((line) => '// $line\n'),
162-
)
163-
..writeln(_headerLine)
164-
..writeln()
165-
..writeln(item.output);
161+
if (_writeDescriptions) {
162+
contentBuffer
163+
..writeln()
164+
..writeln(_headerLine)
165+
..writeAll(
166+
LineSplitter.split(item.generatorDescription)
167+
.map((line) => '// $line\n'),
168+
)
169+
..writeln(_headerLine)
170+
..writeln();
171+
}
172+
173+
contentBuffer.writeln(item.output);
166174
}
167175

168176
var genPartContent = contentBuffer.toString();
@@ -219,12 +227,19 @@ class SharedPartBuilder extends _Builder {
219227
///
220228
/// [allowSyntaxErrors] indicates whether to allow syntax errors in input
221229
/// libraries.
230+
///
231+
/// [writeDescriptions] adds comments to the output used to separate the
232+
/// sections of the file generated from different generators, and reveals
233+
/// which generator produced the following output.
234+
/// If `null`, [writeDescriptions] is set to true which is the default value.
235+
/// If [writeDescriptions] is false, no generator descriptions are added.
222236
SharedPartBuilder(
223237
super.generators,
224238
String partId, {
225239
super.formatOutput,
226240
super.additionalOutputExtensions,
227241
super.allowSyntaxErrors,
242+
super.writeDescriptions,
228243
}) : super(
229244
generatedExtension: '.$partId.g.part',
230245
header: '',
@@ -265,6 +280,12 @@ class PartBuilder extends _Builder {
265280
/// [formatOutput] is called to format the generated code. Defaults to
266281
/// [DartFormatter.format].
267282
///
283+
/// [writeDescriptions] adds comments to the output used to separate the
284+
/// sections of the file generated from different generators, and reveals
285+
/// which generator produced the following output.
286+
/// If `null`, [writeDescriptions] is set to true which is the default value.
287+
/// If [writeDescriptions] is false, no generator descriptions are added.
288+
///
268289
/// [header] is used to specify the content at the top of each generated file.
269290
/// If `null`, the content of [defaultFileHeader] is used.
270291
/// If [header] is an empty `String` no header is added.
@@ -279,6 +300,7 @@ class PartBuilder extends _Builder {
279300
String generatedExtension, {
280301
super.formatOutput,
281302
super.additionalOutputExtensions,
303+
super.writeDescriptions,
282304
super.header,
283305
super.allowSyntaxErrors,
284306
super.options,
@@ -305,6 +327,12 @@ class LibraryBuilder extends _Builder {
305327
/// [formatOutput] is called to format the generated code. Defaults to
306328
/// using the standard [DartFormatter].
307329
///
330+
/// [writeDescriptions] adds comments to the output used to separate the
331+
/// sections of the file generated from different generators, and reveals
332+
/// which generator produced the following output.
333+
/// If `null`, [writeDescriptions] is set to true which is the default value.
334+
/// If [writeDescriptions] is false, no generator descriptions are added.
335+
///
308336
/// [header] is used to specify the content at the top of each generated file.
309337
/// If `null`, the content of [defaultFileHeader] is used.
310338
/// If [header] is an empty `String` no header is added.
@@ -316,6 +344,7 @@ class LibraryBuilder extends _Builder {
316344
super.formatOutput,
317345
super.generatedExtension,
318346
super.additionalOutputExtensions,
347+
super.writeDescriptions,
319348
super.header,
320349
super.allowSyntaxErrors,
321350
super.options,

source_gen/test/builder_test.dart

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,59 @@ void main() {
8484
);
8585
});
8686

87+
test('Omits generated comments if writeDescriptions is explicitly false',
88+
() async {
89+
final srcs = _createPackageStub();
90+
91+
// Explicitly empty header
92+
final builderEmptyHeader = LibraryBuilder(
93+
const CommentGenerator(),
94+
header: '',
95+
writeDescriptions: false,
96+
);
97+
98+
const expected = '''
99+
// Code for "class Person"
100+
// Code for "class Customer"
101+
''';
102+
103+
await testBuilder(
104+
builderEmptyHeader,
105+
srcs,
106+
generateFor: {'$_pkgName|lib/test_lib.dart'},
107+
outputs: {
108+
'$_pkgName|lib/test_lib.g.dart': decodedMatches(startsWith(expected)),
109+
},
110+
);
111+
});
112+
113+
test('When writeDescriptions is true, generated comments are present',
114+
() async {
115+
final srcs = _createPackageStub();
116+
117+
// Null value for writeDescriptions resolves to true
118+
final builder = LibraryBuilder(
119+
const CommentGenerator(),
120+
// We omit header to inspect the generator descriptions
121+
header: '',
122+
);
123+
124+
const expected = '''
125+
// **************************************************************************
126+
// CommentGenerator
127+
// **************************************************************************
128+
''';
129+
130+
await testBuilder(
131+
builder,
132+
srcs,
133+
generateFor: {'$_pkgName|lib/test_lib.dart'},
134+
outputs: {
135+
'$_pkgName|lib/test_lib.g.dart': decodedMatches(contains(expected)),
136+
},
137+
);
138+
});
139+
87140
test('Expect no error when multiple generators used on nonstandalone builder',
88141
() async {
89142
expect(

0 commit comments

Comments
 (0)