rewrite static method

Future<List<String>> rewrite(
  1. AssetIndex index,
  2. String from,
  3. String to
)

Points every mention of from at to, once from has been renamed to on disk: a file's path, or a folder's and so every path under it. Returns the files changed.

Each file is written whole or not at all, and as it was before goes to a new folder in backupFolder first. Files of installed packages (packages/…) are theirs and are left as they are.

Implementation

static Future<List<String>> rewrite(
  AssetIndex index,
  String from,
  String to,
) async {
  if (from == to) return const [];
  final folder = FileSystemEntity.isDirectorySync(index.absolute(to));
  String? backups;
  final changed = <String>[];
  for (final file in await _candidates(index)) {
    if (file == to || AssetIndex.isPackagePath(file)) continue;
    final kind = _kindOf(file);
    if (kind == null) continue;
    final text = await _read(index, file);
    if (text == null) continue;
    // Where the file was before the rename: inside a renamed folder, it
    // moved with it.
    final was = _moved(file, to, from, folder) ?? file;
    final (edited, mentions) = _edit(kind, text, was, file, from, to, folder);
    if (!mentions || edited == text) continue;
    backups ??= await _newBackupFolder(index);
    await ProjectFiles.writeString('$backups/$file', text);
    await ProjectFiles.writeString(index.absolute(file), edited);
    changed.add(file);
  }
  return changed;
}