restoreBackup method

Future<void> restoreBackup(
  1. File originalFile
)

Restores a file from its .bak version and deletes the backup.

Implementation

Future<void> restoreBackup(File originalFile) async {
  final relativePath = p.relative(originalFile.path, from: projectDir);

  final backup = File(p.join(_backupRoot.path, relativePath));

  if (!await backup.exists()) {
    return;
  }

  try {
    await backup.copy(originalFile.path);
    await backup.delete();
  } catch (e, s) {
    throw BuildException(
      'Failed to restore backup for "${originalFile.path}"',
      fix:
          'Check file permissions and ensure the backup directory is accessible.',
      originalStackTrace: s,
    );
  }
}