Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit 05aaa0f

Browse files
committed
Prepare to publish 2.0.0-alpha (#118)
1 parent ce474a2 commit 05aaa0f

File tree

7 files changed

+141
-4
lines changed

7 files changed

+141
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 2.0.0-alpha
2+
3+
* Complete re-write to not use `package:analyzer`.
4+
* Code generation now properly uses the _builder_ pattern (via `built_value`).
5+
* See examples and tests for details.
6+
17
## 1.0.4
28

39
* Added `isInstanceOf` to `ExpressionBuilder`, which performs an `is` check:

README.md

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,79 @@
66

77
`code_builder` is a fluent Dart API for generating valid Dart source code.
88

9-
**Experimental Branch for _2.0_**:
10-
- Drops use of the `package:analyzer` API.
11-
- Uses a proper [builder pattern](https://github.com/square/javapoet).
9+
## Contributing
1210

11+
* Read and help us document common patterns over [at the wiki][wiki].
12+
* Is there a *bug* in the code? [File an issue][issue].
13+
14+
If a feature is missing (the Dart language is always evolving) or you'd like an
15+
easier or better way to do something, consider [opening a pull request][pull].
16+
You can always [file an issue][issue], but generally speaking feature requests
17+
will be on a best-effort basis.
18+
19+
[wiki]: https://github.com/dart-lang/code_builder/wiki
20+
[issue]: https://github.com/dart-lang/code_builder/issues
21+
[pull]: https://github.com/dart-lang/code_builder/pulls
22+
23+
## Usage
24+
25+
`code_builder` has a narrow and user-friendly API.
26+
27+
For example creating a class with a method:
28+
29+
```dart
30+
import 'package:code_builder/code_builder.dart';
31+
import 'package:dart_style/dart_style.dart';
32+
33+
void main() {
34+
final animal = new Class((b) => b
35+
..name = 'Animal'
36+
..extend = const Reference.localScope('Organism').toType()
37+
..methods.add(new Method.returnsVoid((b) => b
38+
..name = 'eat'
39+
..lambda = true
40+
..body = new Code((b) => b..code = 'print(\'Yum\')'))));
41+
final emitter = const DartEmitter();
42+
print(new DartFormatter().format('${animal.accept(emitter)}'));
43+
}
44+
```
45+
46+
Outputs:
47+
```dart
48+
class Animal extends Organism {
49+
void eat() => print('Yum!');
50+
}
51+
```
52+
53+
Have a complicated set of dependencies for your generated code?
54+
`code_builder` supports automatic scoping of your ASTs to automatically
55+
use prefixes to avoid symbol conflicts:
56+
57+
```dart
58+
import 'package:code_builder/code_builder.dart';
59+
import 'package:dart_style/dart_style.dart';
60+
61+
void main() {
62+
final library = new File((b) => b.body.addAll([
63+
new Method((b) => b
64+
..body = new Code((b) => b.code = '')
65+
..name = 'doThing'
66+
..returns = const Reference('Thing', 'package:a/a.dart').toType()),
67+
new Method((b) => b
68+
..body = new Code((b) => b..code = '')
69+
..name = 'doOther'
70+
..returns = const Reference('Other', 'package:b/b.dart').toType()),
71+
]));
72+
final emitter = new DartEmitter(new Allocator.simplePrefixing());
73+
print(new DartFormatter().format('${library.accept(emitter)}'));
74+
}
75+
```
76+
77+
Outputs:
78+
```dart
79+
import 'package:a/a.dart' as _1;
80+
import 'package:b/b.dart' as _2;
81+
82+
_1.Thing doThing() {}
83+
_2.Other doOther() {}
84+
```

example/animal.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:code_builder/code_builder.dart';
6+
import 'package:dart_style/dart_style.dart';
7+
8+
void main() {
9+
final animal = new Class((b) => b
10+
..name = 'Animal'
11+
..extend = const Reference.localScope('Organism').toType()
12+
..methods.add(new Method.returnsVoid((b) => b
13+
..name = 'eat'
14+
..lambda = true
15+
..body = new Code((b) => b..code = 'print(\'Yum\')'))));
16+
final emitter = const DartEmitter();
17+
print(new DartFormatter().format('${animal.accept(emitter)}'));
18+
}

example/scope.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:code_builder/code_builder.dart';
6+
import 'package:dart_style/dart_style.dart';
7+
8+
void main() {
9+
final library = new File((b) => b.body.addAll([
10+
new Method((b) => b
11+
..body = new Code((b) => b.code = '')
12+
..name = 'doThing'
13+
..returns = const Reference('Thing', 'package:a/a.dart').toType()),
14+
new Method((b) => b
15+
..body = new Code((b) => b..code = '')
16+
..name = 'doOther'
17+
..returns = const Reference('Other', 'package:b/b.dart').toType()),
18+
]));
19+
final emitter = new DartEmitter(new Allocator.simplePrefixing());
20+
print(new DartFormatter().format('${library.accept(emitter)}'));
21+
}

lib/src/specs/method.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,23 @@ import 'type_reference.dart';
2020

2121
part 'method.g.dart';
2222

23+
final TypeReference _$void = const Reference.localScope('void').toType();
24+
2325
@immutable
2426
abstract class Method extends Object
2527
with HasAnnotations, HasGenerics, HasDartDocs
2628
implements Built<Method, MethodBuilder>, Reference, Spec {
2729
factory Method([void updates(MethodBuilder b)]) = _$Method;
2830

31+
factory Method.returnsVoid([void updates(MethodBuilder b)]) {
32+
return new Method((b) {
33+
if (updates != null) {
34+
updates(b);
35+
}
36+
b.returns = _$void;
37+
});
38+
}
39+
2940
Method._();
3041

3142
@override

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: code_builder
2-
version: 2.0.0-dev
2+
version: 2.0.0-alpha
33
description: A fluent API for generating Dart code
44
author: Dart Team <[email protected]>
55
homepage: https://github.com/dart-lang/code_builder

test/specs/method_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ void main() {
5151
);
5252
});
5353

54+
test('should create a method with a void return type', () {
55+
expect(
56+
new Method.returnsVoid((b) => b..name = 'foo'),
57+
equalsDart(r'''
58+
void foo();
59+
'''),
60+
);
61+
});
62+
5463
test('should create a method with generic types', () {
5564
expect(
5665
new Method((b) => b

0 commit comments

Comments
 (0)