restoreCachedPackages method
Future<int>
restoreCachedPackages(
- String bundle, {
- String? configPath,
- String? projectRoot,
- WayfinderCancellation? cancel,
Copies each package file select reads for bundle that the cache
lacks out of its source's mirror, as after upgrading from Wayfinder
0.2, whose cache kept only the mirrors. Runs only git show on a
mirror that already exists, under its lock: never fetches, never
waits for another holder of a mirror, and never writes in the project.
Does nothing unless the lock is current and holds bundle's chain.
Binds bundle as select does. Returns how many files it wrote;
select reports what is still missing.
Implementation
Future<int> restoreCachedPackages(
String bundle, {
String? configPath,
String? projectRoot,
WayfinderCancellation? cancel,
}) async {
var restored = 0;
try {
final bound = await WayfinderProjectConfig.bind(
bundle,
configPath: configPath,
projectRoot: projectRoot,
);
final lock = await _readLock(
File(p.join(bound.projectRoot, 'wayfinder.lock')),
);
if (lock == null ||
lock.configurationSha256 !=
canonicalConfigurationSha256(bound.source)) {
return 0;
}
final session = _Session(
projectRoot: bound.projectRoot,
upgrade: false,
previous: lock,
cancel: cancel,
);
for (final locked
in lock.chainOf(bound.binding.id) ?? <LockedPackage>[]) {
final snapshot = _snapshotFile(locked.commit, locked.path);
final mirror = _repositoryCache(
_gitLocation(locked.git, bound.projectRoot),
);
if (await snapshot.exists() || !await mirror.exists()) continue;
final held = await WayfinderFileLock.tryExclusive(_mirrorLock(mirror));
if (held == null) continue;
try {
final result = await _git(session, [
'show',
'${locked.commit}:${locked.path}/wayfinder-profile.json',
], workingDirectory: mirror.path);
if (result.exitCode != 0) continue;
await _keep(snapshot, result.stdout);
restored++;
} finally {
await held.release();
}
}
} on BundleBindingException {
// select reports the binding.
} on FileSystemException {
// select reports what it cannot read.
} on FormatException {
// select reports a configuration it cannot parse.
} on ProcessException {
// Git cannot run; select reports the missing file.
} on WayfinderException catch (error) {
if (error.code == 'cancelled') rethrow;
}
return restored;
}