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
FutureOr<String?> generate(LibraryReader library, BuildStep buildStep) async {
final assetId = await buildStep.resolver.assetIdForElement(library.element);
final currentPath = "${Directory.current.path}/${assetId.path}";
final currentFile = File(currentPath);
String currentFileContent = await currentFile.readAsString();
final RegExp regex = RegExp(r"\/\/\/\#\#\#(.+?)\#\#\#");
final String functionIdentifier =
regex.firstMatch(currentFileContent)?.group(1) ?? '';
await File(currentPath.replaceAll(
RegExp(r'\d+\.wt'), functionIdentifier + ".welltested_test"))
.writeAsString(currentFileContent.replaceAll(regex, ''));
await currentFile.delete();
int startIndex = currentPath.indexOf(".wt");
int endIndex = currentPath.indexOf(".dart");
String currentMockFilePath =
currentPath.replaceRange(startIndex + 3, endIndex, ".mocks");
if (await File(currentMockFilePath).exists()) {
final currentMockFileContent =
await File(currentMockFilePath).readAsString();
await File(currentMockFilePath.replaceAll(
RegExp(r'\d+\.wt'), functionIdentifier + ".welltested_test"))
.writeAsString(currentMockFileContent);
await File(currentMockFilePath).delete();
}
return super.generate(library, buildStep);
}