injectToClass static method
Implementation
static Future<void> injectToClass({
required String filePath,
required String newContent,
String? anchor,
}) async {
final file = File(filePath);
if (!file.existsSync()) return;
String content = await file.readAsString();
if (anchor != null && content.contains(anchor)) {
content = content.replaceFirst(anchor, '$newContent\n $anchor');
} else {
// If no anchor, inject before the last closing brace
final lastBraceIndex = content.lastIndexOf('}');
if (lastBraceIndex != -1) {
content =
content.substring(0, lastBraceIndex) +
'\n $newContent\n' +
content.substring(lastBraceIndex);
}
}
await file.writeAsString(content, flush: true);
}