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
Future<String?> generate(LibraryReader library, BuildStep buildStep) async {
final routes = library.classes.expand(RouteHandler.fromClass).toList();
if (routes.isEmpty) return null;
final routables = routes.groupListsBy((e) => e.routable.element);
return routables.entries.map((_) {
final MapEntry(key: class$, value: routes) = _;
final routesCode = routes.map(_codeAddRoute).join();
return '''
Router get _${codePublicVarName('${class$.name}Router')} => Router()\n
$routesCode;''';
}).join('\n');
}