build method
Implementation
Future<String> build(Element node, Iterable<DartObject> annotations) async {
final scope = StringBuffer();
for (final annotation in annotations) {
final note = annotation.getField('folders');
final extension = annotation.getField('extension');
final parts =
node.source?.uri.toString().split(RegExp(r'[\\/]', unicode: true));
final dir = parts?.sublist(1, parts.length - 1).join('/');
if (note == null ||
note.isNull ||
dir == null ||
dir.isEmpty ||
extension == null ||
extension.isNull) {
continue;
}
final templateFile = await File('$dir/${parts?.last}').readAsString();
final regClass = RegExp(r'class\s+(\w+)');
final chunks = templateFile.split(regClass);
scope.writeln(chunks[0]);
final template = chunks[1];
for (final name in note.toListValue()!) {
Iterable<File> files = Directory('$dir/${name.toStringValue()}')
.listSync(recursive: true)
.where((entity) =>
entity is File &&
entity.path.endsWith(extension.toStringValue()!))
.cast<File>();
for (final file in files) {
String content = await file.readAsString();
final pattern = RegExp(r"(Scenario(?: Outline)?:)");
final matches = pattern.allMatches(content);
final scenarioNames = matches.map((m) => m.group(0)).toList();
final scenarios = content.split(pattern).toList();
scenarioNames.asMap().forEach((index, scenario) {
final name =
scenarios[index + 1].split(RegExp('(\n|\r)')).first.trim();
scope.writeln('class ${name.replaceAll(' ', '')}$template'
.replaceAll('%step%', name)
.replaceAll(
'%feature%',
'${scenarios[0]}\n$scenario${scenarios[index + 1]}'
.replaceAll('\$', '\\\$')));
});
}
}
}
return scope.toString();
}