generate method
Generates Dart code for an input Dart library.
May create additional outputs through the buildStep
, but the 'primary'
output is Dart code returned through the Future. If there is nothing to
generate for this library may return null, or a Future that resolves to
null or the empty string.
Implementation
@override
String? generate(LibraryReader library, BuildStep? buildStep) {
try {
// An injector is an abstract class where all abstract methods are
// annotated with Register.
final injectors = library.classes
.where((c) =>
c.isAbstract &&
c.methods.where((m) => m.isAbstract).isNotEmpty &&
c.methods
.where((m) => m.isAbstract && _isRegisterMethod(m))
.isNotEmpty)
.toList();
if (injectors.isEmpty) {
return null;
}
final file = Library((lb) => lb
..body.addAll(
injectors.map((i) => _generateInjector(i, library, buildStep))));
final DartEmitter emitter = DartEmitter(allocator: Allocator());
return DartFormatter().format('${file.accept(emitter)}');
} catch (e) {
if (e is KiwiGeneratorError || e is UnresolvedAnnotationException) {
rethrow;
} else if (e is Error) {
throw KiwiGeneratorError(
'Something went wrong with the KiwiGenerator. Please create a new ticket with a copy of your error to https://github.com/vanlooverenkoen/kiwi/issues/new?labels=kiwi_generator,bug',
error: e);
} else {
throw KiwiGeneratorError(
'Something went wrong with the KiwiGenerator. Please create a new ticket with a copy of your error to https://github.com/vanlooverenkoen/kiwi/issues/new?labels=kiwi_generator,bug');
}
}
}