generateForAnnotatedElement method
FutureOr<String>
generateForAnnotatedElement(
- Element element,
- ConstantReader annotation,
- BuildStep buildStep
Implement to return source code to generate for element
.
This method is invoked based on finding elements annotated with an
instance of T
. The annotation
is provided as a ConstantReader
.
Supported return values include a single String or multiple String instances within an Iterable or Stream. It is also valid to return a Future of String, Iterable, or Stream.
Implementations should return null
when no content is generated. Empty
or whitespace-only String instances are also ignored.
Implementation
@override
FutureOr<String> generateForAnnotatedElement(
final Element element,
final ConstantReader annotation,
final BuildStep buildStep,
) {
final database = _getDatabase(element);
final databaseClass = DatabaseWriter(database).write();
final daoClasses = database.daoGetters
.map((daoGetter) => daoGetter.dao)
.map((dao) => DaoWriter(
dao,
database.streamEntities,
database.hasViewStreams,
).write());
final distinctTypeConverterFields = database.allTypeConverters
.distinctBy((element) => element.name)
.map((typeConverter) =>
TypeConverterFieldWriter(typeConverter.name).write());
final library = Library((builder) {
builder
..body.add(DatabaseBuilderContractWriter(database.name).write())
..body.add(FloorWriter(database.name).write())
..body.add(DatabaseBuilderWriter(database.name).write())
..body.add(databaseClass)
..body.addAll(daoClasses);
if (distinctTypeConverterFields.isNotEmpty) {
builder
..body.add(const Code('// ignore_for_file: unused_element\n'))
..body.addAll(distinctTypeConverterFields);
}
});
return library.accept(DartEmitter()).toString();
}