rollbackTo method

Future<void> rollbackTo(
  1. String targetVersion
)

Rolls back publication atomically to a previous valid generation.

Implementation

Future<void> rollbackTo(String targetVersion) async {
  final targetDir = _versionDir(targetVersion);
  if (!await targetDir.exists()) {
    throw ArgumentError(
      'Target rollback version "$targetVersion" does not exist.',
    );
  }
  final lockFile = File('${root.path}${Platform.pathSeparator}.publish.lock');
  await root.create(recursive: true);
  final lockHandle = await lockFile.open(mode: FileMode.append);
  await lockHandle.lock(FileLock.exclusive);
  final pointer = File(
    '${root.path}${Platform.pathSeparator}.active_version',
  );
  final temp = File('${pointer.path}.${_nonce()}.tmp');
  try {
    await temp.writeAsString(targetVersion, flush: true);
    await temp.rename(pointer.path);
  } finally {
    if (await temp.exists()) await temp.delete();
    await lockHandle.unlock();
    await lockHandle.close();
  }
}