replaceSectionWith method

void replaceSectionWith({
  1. required String startTag,
  2. required String endTag,
  3. required String content,
})

Replaces the content between startTag and endTag with content

Implementation

void replaceSectionWith({
  required String startTag,
  required String endTag,
  required String content,
}) {
  final original = readAsStringSync();
  final startIndex = original.indexOf(startTag);
  if (startIndex == -1) {
    throw "startTag $startTag not found in ${absolute.path}";
  }
  final start = startIndex + startTag.length;
  final end = original.indexOf(endTag, start);
  if (end == -1) {
    throw "endTag $endTag not found in ${absolute.path}";
  }
  final mutated = original.replaceRange(start, end, content);
  writeAsStringSync(mutated);
}