dart-archive/code_builder
有効なDartソースコードを生成するためのフレンドリーなAPI
- Stars
- 427
- Forks
- 61
- 最終プッシュ(UTC)
- 2024/10/31
- プロジェクト状態
- アーカイブ済み
使用している依存関係
依存関係一覧 12 件
- built_collection
^5.0.0 - built_value
^8.0.0 - collection
^1.15.0 - matcher
^0.12.10 - meta
^1.3.0 - build開発用
^2.0.0 - build_runner開発用
^2.0.3 - built_value_generator開発用
^8.0.0 - dart_flutter_team_lints開発用
^3.0.0 - dart_style開発用
^2.3.7 - source_gen開発用
^1.0.0 - test開発用
^1.16.0
元の README
以下は英語原文のスナップショットです。最新版は GitHub をご覧ください。
README を開く / 閉じる
[!IMPORTANT]
This repo has moved to https://github.com/dart-lang/tools/tree/main/pkgs/code_builder
Dart CI Pub package package publisher Gitter chat
A fluent, builder-based library for generating valid Dart 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:
import 'package:code_builder/code_builder.dart';
import 'package:dart_style/dart_style.dart';
void main() {
final animal = Class((b) => b
..name = 'Animal'
..extend = refer('Organism')
..methods.add(Method.returnsVoid((b) => b
..name = 'eat'
..body = const Code("print('Yum!');"))));
final emitter = DartEmitter();
print(
DartFormatter(languageVersion: DartFormatter.latestLanguageVersion)
.format('${animal.accept(emitter)}'),
);
}
Outputs:
class Animal extends Organism {
void 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:
import 'package:code_builder/code_builder.dart';
import 'package:dart_style/dart_style.dart';
void main() {
final library = Library((b) => b.body.addAll([
Method((b) => b
..body = const Code('')
..name = 'doThing'
..returns = refer('Thing', 'package:a/a.dart')),
Method((b) => b
..body = const Code('')
..name = 'doOther'
..returns = refer('Other', 'package:b/b.dart')),
]));
final emitter = DartEmitter.scoped();
print(
DartFormatter(languageVersion: DartFormatter.latestLanguageVersion)
.format('${library.accept(emitter)}'),
);
}
Outputs:
import 'package:a/a.dart' as _i1;
import 'package:b/b.dart' as _i2;
_i1.Thing doThing() {}
_i2.Other doOther() {}
Contributing
- Read and help us document common patterns over at the wiki.
- Is there a bug in the code? File an issue.
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
dartfmtmust be used to format this repository. You can run it simply from the command-line:$ dart run dart_style:format -w .
Updating generated (.g.dart) files
NOTE: There is currently a limitation in
build_runnerthat requires a workaround for developing this package since it is a dependency of the build system.
Make a snapshot of the generated build_runner build script and
run from the snapshot instead of from source to avoid problems with deleted
files. These steps must be run without deleting the source files.
./tool/regenerate.sh