publishVersion method
Implementation
Future<void> publishVersion(
String version, {
int? fencingEpoch,
String? routePath,
}) async {
final draft = _versionDir(version);
if (!await draft.exists()) {
throw StateError('Artifact draft 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 {
if (fencingEpoch != null) {
final epochs = await _loadPublishEpochs();
final epochKey = routePath != null ? _normalizePath(routePath) : '*';
final currentEpoch = epochs[epochKey] ?? epochs['*'] ?? 0;
if (fencingEpoch < currentEpoch) {
throw StaleFencingEpochException(
fencingEpoch,
currentEpoch,
routePath: routePath,
);
}
epochs[epochKey] = fencingEpoch;
if (fencingEpoch > (epochs['*'] ?? 0)) {
epochs['*'] = fencingEpoch;
}
await _savePublishEpochs(epochs);
}
final baseFile = File(
'${draft.path}${Platform.pathSeparator}.base_version',
);
final expectedBase = await baseFile.exists()
? (await baseFile.readAsString()).trim()
: '';
final current = await activeVersion ?? '';
if (current != expectedBase) {
throw StaleArtifactVersionException(expectedBase, current);
}
await temp.writeAsString(version, flush: true);
// Replace through rename without unlinking the old pointer first.
await temp.rename(pointer.path);
} finally {
if (await temp.exists()) await temp.delete();
await lockHandle.unlock();
await lockHandle.close();
}
}