|
6 | 6 |
|
7 | 7 | `code_builder` is a fluent Dart API for generating valid Dart source code. |
8 | 8 |
|
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 |
12 | 10 |
|
| 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 | +``` |
0 commit comments