Name: code_builder
Owner: Dart
Description: A fluent API for generating valid Dart source code
Created: 2016-08-18 20:34:56.0
Updated: 2018-05-07 01:39:40.0
Pushed: 2018-05-07 01:39:39.0
Homepage: https://pub.dartlang.org/packages/code_builder
Size: 367
Language: Dart
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
code_builder
is a fluent Dart API for generating valid Dart source code.
code_builder
has a narrow and user-friendly API.
See the example
and test
folders for additional examples.
For example creating a class with a method:
rt 'package:code_builder/code_builder.dart';
rt 'package:dart_style/dart_style.dart';
main() {
nal animal = new Class((b) => b
..name = 'Animal'
..extend = refer('Organism')
..methods.add(new Method.returnsVoid((b) => b
..name = 'eat'
..body = const Code('print(\'Yum\')'))));
nal emitter = new DartEmitter();
int(new DartFormatter().format('${animal.accept(emitter)}'));
Outputs:
s Animal extends Organism {
id eat() => print('Yum!');
Have a complicated set of dependencies for your generated code?
code_builder
supports automatic scoping of your ASTs to automatically
use prefixes to avoid symbol conflicts:
rt 'package:code_builder/code_builder.dart';
rt 'package:dart_style/dart_style.dart';
main() {
nal library = new Library((b) => b.body.addAll([
new Method((b) => b
..body = const Code('')
..name = 'doThing'
..returns = refer('Thing', 'package:a/a.dart')),
new Method((b) => b
..body = const Code('')
..name = 'doOther'
..returns = refer('Other', 'package:b/b.dart')),
]));
nal emitter = new DartEmitter(new Allocator.simplePrefixing());
int(new DartFormatter().format('${library.accept(emitter)}'));
Outputs:
rt 'package:a/a.dart' as _i1;
rt 'package:b/b.dart' as _i2;
Thing doThing() {}
Other doOther() {}
If a feature is missing (the Dart language is always evolving) or you'd like an easier or better way to do something, consider opening a pull request. You can always file an issue, but generally speaking feature requests will be on a best-effort basis.
NOTE: Due to the evolving Dart SDK the local
dartfmt
must be used to format this repository. You can run it simply from the command-line:b run dart_style:format -w .
.g.dart
) filesNOTE: There is currently a limitation in
build_runner
that requires a workaround for developing this package. We expect this to be unnecessary in the future.
Use build_runner
:
build.disabled.yaml build.yaml
b run build_runner build --delete-conflicting-outputs
build.yaml build.disabled.yaml