build method

Future<String> build(
  1. Element node,
  2. Iterable<DartObject> annotations
)

Implementation

Future<String> build(Element node, Iterable<DartObject> annotations) async {
  final imports = StringBuffer();
  final classes = StringBuffer();
  for (final annotation in annotations) {
    final note = annotation.getField('folders');
    final parts =
        node.source?.uri.toString().split(RegExp(r'[\\/]', unicode: true));
    final dir = parts?.sublist(1, parts.length - 1).join('/');
    if (note!.isNull || dir!.isEmpty) {
      continue;
    }
    for (final name in note.toListValue()!) {
      Iterable<File> scope = Directory('$dir/${name.toStringValue()}')
          .listSync(recursive: true)
          .where((entity) =>
              entity is File &&
              entity.path.endsWith('.dart') &&
              !entity.path.endsWith('.mocks.dart') &&
              !entity.path.endsWith('.wrapper.dart'))
          .cast<File>();
      for (final file in scope) {
        final path = file.path
            .toString()
            .replaceAll('\\', '/')
            .replaceAll('$dir/', '');
        imports.writeln("import '$path';");
        String content = await file.readAsString();
        final regex = RegExp(r'class\s+(\w+)');
        final matches = regex.allMatches(content);
        for (final match in matches) {
          classes.writeln('  ${match.group(1)}(),');
        }
      }
    }
  }
  return '${imports.toString()}\nvar classList = [\n${classes.toString()}\n];';
}