dart_builder 0.1.0 dart_builder: ^0.1.0 copied to clipboard
A fluent API for creating Dart code.
Dart Builder #
Getting Started #
The most common scenario is creating a new generated file in a transformer
or build step. Dart builder provides the SourceFile
class to output either a
library or a part file (of another library):
var file = new SourceFile.library('bar');
file.toSource(); // Outputs "library bar;\n"
It's possible to import other libraries or files:
new SourceFile.library('bar', imports: [
new ImportDirective(Uri.parse('package:foo/foo.dart'))
]);
And include other dart constructs, like classes or methods:
new SourceFile.library('bar', topLevelElements: [
new ClassRef('Foo')
]);
NOTE: By default, SourceFile.toSource()
applies the dart formatter.
Creating a class #
new ClassRef('Foo', fields: [
new FieldRef('bar')
], methods: [
new MethodRef('baz')
]);
See tests for more examples.