generate method

  1. @override
Future<Snippet> generate(
  1. dynamic lib,
  2. dynamic step
)
override

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
Future<Snippet> generate(LibraryReader lib, BuildStep step) async {
  final snippet = Snippet();
  if (lib.classes.where((k) {
    return k.methods.where((method) {
      return macro.hasAnnotationOf(method);
    }).isNotEmpty;
  }).isEmpty) {
    return snippet;
  }
  lib.classes
      .expand((k) => [
            ...k.methods.map((r) => r.returnType),
            ...k.methods.expand((f) => f.parameters.map((p) => p.type))
          ])
      .expand((x) => x.isDartAsyncFuture || x.isDartAsyncFutureOr
          ? (x is ParameterizedType && x.typeArguments.isNotEmpty
              ? [x.typeArguments[0]]
              : <DartType>[])
          : [x])
      .where((x) => parameterizedType(x))
      .expand(generics)
      .forEach((init) {
    snippet.init(init);
  });

  snippet.import('import \'${lib.element.librarySource.uri.toString()}\';');
  for (var k in lib.classes) {
    final ms = k.methods.where((method) {
      return macro.hasAnnotationOf(method);
    }).toList();
    if (ms.isEmpty) {
      continue;
    }
    const mm = 'const MPI()';
    final dn = k.displayName;
    final kn = typename(k);
    final de = 'Defi<MPI>($mm, () => MPI$kn(ReferenceHandler($mm, $dn)))';
    snippet.import('import \'package:maas/macro/index.dart\';');
    snippet.import('import \'package:maas/mpc/index.dart\';');
    snippet.init('ark.store<MPI, $dn>(\'mpi\', $de);');
    snippet.define(proxy(snippet, kn, dn, k, ms));
  }
  return snippet;
}