dart-lang/code_builder

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

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

code_builder

Pub package Build status Latest docs Gitter chat

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

Usage

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() {}
Contributing

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 .
Updating generated (.g.dart) files

NOTE: 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

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.