Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
3 changes: 3 additions & 0 deletions source_gen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2.1.0-wip
- `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.

## 2.0.0-wip

- **Breaking Change**: Change `formatOutput` function to accept a language
Expand Down
35 changes: 25 additions & 10 deletions source_gen/lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class _Builder extends Builder {

final String _header;

/// Whether to include or emit the gen part descriptions. Defaults to true.
final bool _writeDescriptions;

/// Whether to allow syntax errors in input libraries.
final bool allowSyntaxErrors;

Expand All @@ -51,6 +54,7 @@ class _Builder extends Builder {
String generatedExtension = '.g.dart',
List<String> additionalOutputExtensions = const [],
String? header,
bool? writeDescriptions,
this.allowSyntaxErrors = false,
BuilderOptions? options,
}) : _generatedExtension = generatedExtension,
Expand All @@ -61,6 +65,7 @@ class _Builder extends Builder {
...additionalOutputExtensions,
],
}),
_writeDescriptions = writeDescriptions ?? true,
_header = (header ?? defaultFileHeader).trim() {
if (_generatedExtension.isEmpty || !_generatedExtension.startsWith('.')) {
throw ArgumentError.value(
Expand Down Expand Up @@ -153,16 +158,19 @@ class _Builder extends Builder {
}

for (var item in generatedOutputs) {
contentBuffer
..writeln()
..writeln(_headerLine)
..writeAll(
LineSplitter.split(item.generatorDescription)
.map((line) => '// $line\n'),
)
..writeln(_headerLine)
..writeln()
..writeln(item.output);
if (_writeDescriptions) {
contentBuffer
..writeln()
..writeln(_headerLine)
..writeAll(
LineSplitter.split(item.generatorDescription)
.map((line) => '// $line\n'),
)
..writeln(_headerLine)
..writeln();
}

contentBuffer.writeln(item.output);
}

var genPartContent = contentBuffer.toString();
Expand Down Expand Up @@ -225,6 +233,7 @@ class SharedPartBuilder extends _Builder {
super.formatOutput,
super.additionalOutputExtensions,
super.allowSyntaxErrors,
super.writeDescriptions,
}) : super(
generatedExtension: '.$partId.g.part',
header: '',
Expand Down Expand Up @@ -265,6 +274,10 @@ class PartBuilder extends _Builder {
/// [formatOutput] is called to format the generated code. Defaults to
/// [DartFormatter.format].
///
/// If [writeDescriptions] is false, then no generated descriptions will be
/// embeded into the generated out file. The default is true, which includes
/// the name of the dart files/parts/libraries which were used to generate.
///
/// [header] is used to specify the content at the top of each generated file.
/// If `null`, the content of [defaultFileHeader] is used.
/// If [header] is an empty `String` no header is added.
Expand All @@ -279,6 +292,7 @@ class PartBuilder extends _Builder {
String generatedExtension, {
super.formatOutput,
super.additionalOutputExtensions,
super.writeDescriptions,
super.header,
super.allowSyntaxErrors,
super.options,
Expand Down Expand Up @@ -316,6 +330,7 @@ class LibraryBuilder extends _Builder {
super.formatOutput,
super.generatedExtension,
super.additionalOutputExtensions,
super.writeDescriptions,
super.header,
super.allowSyntaxErrors,
super.options,
Expand Down
2 changes: 1 addition & 1 deletion source_gen/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: source_gen
version: 2.0.0-wip
version: 2.1.0-wip
description: >-
Source code generation builders and utilities for the Dart build system
repository: https://github.com/dart-lang/source_gen/tree/master/source_gen
Expand Down
19 changes: 19 additions & 0 deletions source_gen/test/builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ void main() {
);
});

test('Omits generated comments if writeDescriptions is explicitly false',
() async {
final srcs = _createPackageStub();
final builder = LibraryBuilder(
const CommentGenerator(),
header: '',
writeDescriptions: false,
);
await testBuilder(
builder,
srcs,
generateFor: {'$_pkgName|lib/test_lib.dart'},
outputs: {
'$_pkgName|lib/test_lib.g.dart':
decodedMatches(isNot(startsWith('// ***'))),
},
);
});

test('Expect no error when multiple generators used on nonstandalone builder',
() async {
expect(
Expand Down