createBackup method

Future<File> createBackup(
  1. File file
)

Creates a backup of a file (.bak extension). Returns the backup file.

Implementation

Future<File> createBackup(File file) async {
  if (!file.existsSync()) {
    throw BuildException(
      'File not found for backup: "${file.path}"',
      fix:
          'Ensure mandatory project files (like pubspec.yaml) exist before running build.',
    );
  }

  try {
    final relativePath = p.relative(file.path, from: projectDir);
    final backupFile = File(p.join(_backupRoot.path, relativePath));

    await backupFile.parent.create(recursive: true);

    return await file.copy(backupFile.path);
  } catch (e, s) {
    throw BuildException(
      'Failed to create backup file for "${file.path}"',
      fix: 'Check directory permissions for backup directory.',
      originalStackTrace: s,
    );
  }
}