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 values = <String>{};
  // @Entity found
  bool isEntity = library.annotatedWith(entityChecker).isNotEmpty;
  // @Convertable found
  bool isConverter = library.annotatedWith(converterChecker).isNotEmpty;

  if (isEntity || isConverter) {
    final lib = Library((builder) {
      builder
        ..body.add(const Code(
            "// ignore_for_file: invalid_use_of_internal_member\n"))
        ..body.add(const Code('\n'));
    });

    return _dartfmt.format('${lib.accept(DartEmitter())}');
  } else {
    return values.join('\n\n');
  }
}