Skip to content

Commit 924d485

Browse files
authored
Add a flag to GeneratorBuilder. (#179)
1 parent 88fb50a commit 924d485

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
```
3434

3535
In Dart SDK `>=1.25.0` this can be relaxed as `part of` can refer to a path.
36+
To opt-in, `GeneratorBuilder` now has a new flag, `requireLibraryDirective`.
37+
Set it to `false`, and also set your `sdk` constraint appropriately:
38+
39+
```yaml
40+
sdk: '>=1.25.0 <2.0.0'
41+
```
3642
3743
* Added `findType`, an utility method for `LibraryElement#getType` that also
3844
traverses `export` directives for publicly exported types. For example, to

lib/src/builder.dart

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,24 @@ class GeneratorBuilder extends Builder {
3939
/// Whether to emit a standalone (non-`part`) file in this builder.
4040
final bool isStandalone;
4141

42+
final bool _requireLibraryDirective;
43+
4244
/// Wrap [generators] to form a [Builder]-compatible API.
45+
///
46+
/// May set [requireLibraryDirective] to `false` in order to opt-in to
47+
/// supporting a `1.25.0` feature of `part of` being usable without an
48+
/// explicit `library` directive. Developers should restrict their `pubspec`
49+
/// accordingly:
50+
/// ```yaml
51+
/// sdk: '>=1.25.0 <2.0.0'
52+
/// ```
4353
GeneratorBuilder(this.generators,
4454
{OutputFormatter formatOutput,
4555
this.generatedExtension: '.g.dart',
46-
this.isStandalone: false})
47-
: formatOutput = formatOutput ?? _formatter.format {
56+
this.isStandalone: false,
57+
bool requireLibraryDirective: true})
58+
: formatOutput = formatOutput ?? _formatter.format,
59+
_requireLibraryDirective = requireLibraryDirective {
4860
if (generatedExtension == null) {
4961
throw new ArgumentError.notNull('generatedExtension');
5062
}
@@ -86,7 +98,11 @@ class GeneratorBuilder extends Builder {
8698

8799
if (!isStandalone) {
88100
var asset = buildStep.inputId;
89-
var name = nameOfPartial(library, asset);
101+
var name = nameOfPartial(
102+
library,
103+
asset,
104+
allowUnnamedPartials: !_requireLibraryDirective,
105+
);
90106
if (name == null) {
91107
var suggest = suggestLibraryName(asset);
92108
throw new InvalidGenerationSourceError(

lib/src/utils.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:analyzer/dart/ast/ast.dart';
66
import 'package:analyzer/dart/ast/standard_resolution_map.dart';
77
import 'package:analyzer/dart/element/element.dart';
88
import 'package:build/build.dart';
9+
import 'package:path/path.dart' as p;
910

1011
String friendlyNameForElement(Element element) {
1112
var friendlyName = element.displayName;
@@ -56,7 +57,8 @@ String nameOfPartial(
5657
return element.name;
5758
}
5859
if (allowUnnamedPartials) {
59-
return '\'package:${source.package}/${source.path}\'';
60+
var sourceUrl = p.basename(source.uri.toString());
61+
return '\'$sourceUrl\'';
6062
}
6163
return null;
6264
}

test/builder_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ void main() {
7575
throwsA(const isInstanceOf<InvalidGenerationSourceError>()));
7676
});
7777

78+
test('Allow no "library" when requireLibraryDirective=false', () async {
79+
var sources = _createPackageStub(pkgName, testLibContent: 'class A {}');
80+
var builder = new GeneratorBuilder([const CommentGenerator()],
81+
requireLibraryDirective: false);
82+
await testBuilder(builder, sources,
83+
outputs: {'$pkgName|lib/test_lib.g.dart': _testGenNoLibrary});
84+
});
85+
7886
test(
7987
'Simple Generator test for library',
8088
() => _generateTest(
@@ -306,3 +314,15 @@ part of test_lib;
306314
307315
// Code for "class Customer"
308316
''';
317+
318+
const _testGenNoLibrary = r'''// GENERATED CODE - DO NOT MODIFY BY HAND
319+
320+
part of 'test_lib.dart';
321+
322+
// **************************************************************************
323+
// Generator: CommentGenerator
324+
// Target: class A
325+
// **************************************************************************
326+
327+
// Code for "class A"
328+
''';

0 commit comments

Comments
 (0)