generate method

  1. @override
FutureOr<String?> generate(
  1. LibraryReader library,
  2. BuildStep buildStep
)

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
FutureOr<String?> generate(LibraryReader library, BuildStep buildStep) async {
  final allDepsInStep = <DependencyConfig>[];
  for (var clazz in library.classes) {
    if (_moduleChecker.hasAnnotationOfExact(clazz)) {
      throwIf(
        !clazz.isAbstract,
        '[${clazz.name}] must be an abstract class!',
        element: clazz,
      );
      final executables = <ExecutableElement>[
        ...clazz.accessors,
        ...clazz.methods,
      ];
      for (var element in executables) {
        if (element.isPrivate) continue;
        allDepsInStep.add(
          DependencyResolver(
            getResolver(await buildStep.resolver.libraries.toList()),
          ).resolveModuleMember(clazz, element),
        );
      }
    } else if (_hasInjectable(clazz) ||
        (_autoRegister && _hasConventionalMatch(clazz))) {
      allDepsInStep.add(DependencyResolver(
        getResolver(await buildStep.resolver.libraries.toList()),
      ).resolve(clazz));
    }
  }
  return allDepsInStep.isNotEmpty ? jsonEncode(allDepsInStep) : null;
}