From d7b662a67b3d95fe03c1acfb6435918560b2181c Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Tue, 12 Dec 2023 09:09:46 -0800 Subject: [PATCH] Add a new field `Library.generatedByComment` to support emitting 'generated by' comments. (#441) add a Library.generatedByComment field --- CHANGELOG.md | 4 ++++ README.md | 4 +--- lib/src/emitter.dart | 6 ++++++ lib/src/specs/library.dart | 8 +++++++ lib/src/specs/library.g.dart | 21 ++++++++++++++++++ pubspec.yaml | 2 +- test/specs/library_test.dart | 41 ++++++++++++++++++++++++++++++++++++ tool/regenerate.sh | 7 ++++++ 8 files changed, 89 insertions(+), 4 deletions(-) create mode 100755 tool/regenerate.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index c216a928..ee23d39c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 4.9.0 + +* Add `Library.generatedByComment` to support emitting 'generated by' comments. + ## 4.8.0 * Add `Expression.operatorSubtract` diff --git a/README.md b/README.md index 2d77a256..f149c4ff 100644 --- a/README.md +++ b/README.md @@ -103,9 +103,7 @@ run from the snapshot instead of from source to avoid problems with deleted files. These steps must be run without deleting the source files. ```bash -$ dart run build_runner generate-build-script -$ dart compile kernel .dart_tool/build/entrypoint/build.dart -$ dart .dart_tool/build/entrypoint/build.dill build --delete-conflicting-outputs +./tool/regenerate.sh ``` [build_runner]: https://pub.dev/packages/build_runner diff --git a/lib/src/emitter.dart b/lib/src/emitter.dart index 591b992c..617cb7bf 100644 --- a/lib/src/emitter.dart +++ b/lib/src/emitter.dart @@ -483,6 +483,12 @@ class DartEmitter extends Object output.writeln(); } + if (spec.generatedByComment != null) { + output + ..writeln('// ${spec.generatedByComment}') + ..writeln(); + } + if (spec.ignoreForFile.isNotEmpty) { final ignores = spec.ignoreForFile.toList()..sort(); final lines = ['// ignore_for_file: ${ignores.first}']; diff --git a/lib/src/specs/library.dart b/lib/src/specs/library.dart index 0e3d7f3f..69a6dbfb 100644 --- a/lib/src/specs/library.dart +++ b/lib/src/specs/library.dart @@ -30,6 +30,12 @@ abstract class Library /// Line comments to place at the start of the library. BuiltList get comments; + /// A comment indicating the tool this library was generated by. + /// + /// This is typically of the form `Generated by xxx.`; it should exclude + /// leading line comment characters. + String? get generatedByComment; + /// A list of analysis issues to ignore (`ignore_for_file: ...`). BuiltList get ignoreForFile; @@ -59,6 +65,8 @@ abstract class LibraryBuilder ListBuilder directives = ListBuilder(); ListBuilder comments = ListBuilder(); + String? generatedByComment; ListBuilder ignoreForFile = ListBuilder(); + String? name; } diff --git a/lib/src/specs/library.g.dart b/lib/src/specs/library.g.dart index 6bf19b30..1858cd6d 100644 --- a/lib/src/specs/library.g.dart +++ b/lib/src/specs/library.g.dart @@ -16,6 +16,8 @@ class _$Library extends Library { @override final BuiltList comments; @override + final String? generatedByComment; + @override final BuiltList ignoreForFile; @override final String? name; @@ -28,6 +30,7 @@ class _$Library extends Library { required this.directives, required this.body, required this.comments, + this.generatedByComment, required this.ignoreForFile, this.name}) : super._() { @@ -55,6 +58,7 @@ class _$Library extends Library { directives == other.directives && body == other.body && comments == other.comments && + generatedByComment == other.generatedByComment && ignoreForFile == other.ignoreForFile && name == other.name; } @@ -66,6 +70,7 @@ class _$Library extends Library { _$hash = $jc(_$hash, directives.hashCode); _$hash = $jc(_$hash, body.hashCode); _$hash = $jc(_$hash, comments.hashCode); + _$hash = $jc(_$hash, generatedByComment.hashCode); _$hash = $jc(_$hash, ignoreForFile.hashCode); _$hash = $jc(_$hash, name.hashCode); _$hash = $jf(_$hash); @@ -79,6 +84,7 @@ class _$Library extends Library { ..add('directives', directives) ..add('body', body) ..add('comments', comments) + ..add('generatedByComment', generatedByComment) ..add('ignoreForFile', ignoreForFile) ..add('name', name)) .toString(); @@ -136,6 +142,18 @@ class _$LibraryBuilder extends LibraryBuilder { super.comments = comments; } + @override + String? get generatedByComment { + _$this; + return super.generatedByComment; + } + + @override + set generatedByComment(String? generatedByComment) { + _$this; + super.generatedByComment = generatedByComment; + } + @override ListBuilder get ignoreForFile { _$this; @@ -169,6 +187,7 @@ class _$LibraryBuilder extends LibraryBuilder { super.directives = $v.directives.toBuilder(); super.body = $v.body.toBuilder(); super.comments = $v.comments.toBuilder(); + super.generatedByComment = $v.generatedByComment; super.ignoreForFile = $v.ignoreForFile.toBuilder(); super.name = $v.name; _$v = null; @@ -199,6 +218,7 @@ class _$LibraryBuilder extends LibraryBuilder { directives: directives.build(), body: body.build(), comments: comments.build(), + generatedByComment: generatedByComment, ignoreForFile: ignoreForFile.build(), name: name); } catch (_) { @@ -212,6 +232,7 @@ class _$LibraryBuilder extends LibraryBuilder { body.build(); _$failedField = 'comments'; comments.build(); + _$failedField = 'ignoreForFile'; ignoreForFile.build(); } catch (e) { diff --git a/pubspec.yaml b/pubspec.yaml index 0d26965f..f088adfe 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: code_builder -version: 4.8.0 +version: 4.9.0 description: >- A fluent, builder-based library for generating valid Dart code repository: https://github.com/dart-lang/code_builder diff --git a/test/specs/library_test.dart b/test/specs/library_test.dart index 3c5502ec..a2974fed 100644 --- a/test/specs/library_test.dart +++ b/test/specs/library_test.dart @@ -53,6 +53,23 @@ void main() { ); }); + test('should emit a source file with a generated-by comment', () { + expect( + Library( + (b) => b + ..generatedByComment = 'Generated by fooBar.' + ..body.add( + Class((b) => b..name = 'Foo'), + ), + ), + equalsDart(r''' + // Generated by fooBar. + + class Foo { } + ''', DartEmitter(allocator: Allocator())), + ); + }); + test('should emit a source file with ignore comments', () { expect( Library( @@ -93,6 +110,30 @@ void main() { ); }); + test('should emit with line comments, generated-by, and ignore-for-file', + () { + expect( + Library( + (b) => b + ..comments.add('Generic copyright statement.') + ..generatedByComment = 'Generated by fooBar.' + ..ignoreForFile.add('sort_constructors_first') + ..body.add( + Class((b) => b..name = 'Foo'), + ), + ), + equalsDart(r''' + // Generic copyright statement. + + // Generated by fooBar. + + // ignore_for_file: sort_constructors_first + + class Foo { } + ''', DartEmitter(allocator: Allocator())), + ); + }); + test('should emit a source file with manual imports', () { expect( Library((b) => b diff --git a/tool/regenerate.sh b/tool/regenerate.sh new file mode 100755 index 00000000..722e98ae --- /dev/null +++ b/tool/regenerate.sh @@ -0,0 +1,7 @@ +# Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +# for details. All rights reserved. Use of this source code is governed by a +# BSD-style license that can be found in the LICENSE file. + +dart run build_runner generate-build-script +dart compile kernel .dart_tool/build/entrypoint/build.dart +dart .dart_tool/build/entrypoint/build.dill build --delete-conflicting-outputs