saveVersionDependencies method
Implementation
Future<void> saveVersionDependencies(
String version,
Map<String, RouteDependency> deps,
) async {
final directory = _versionDir(version);
await _saveIndex(directory, deps);
final shardsDep = <String, Map<String, Map<String, Object?>>>{};
final shardsRoute = <String, Map<String, Map<String, Object?>>>{};
final entries = await _loadRouteArtifacts(directory);
for (final dependency in deps.values) {
final file = _artifactFile(directory, dependency.path);
if (await file.exists()) {
final content = await file.readAsString();
final hash = sha256.convert(utf8.encode(content)).toString();
final entry = RouteArtifactEntry(
path: dependency.path,
objectHash: hash,
contentLength: utf8.encode(content).length,
generatedAt: dependency.generatedAt,
);
entries[dependency.path] = entry;
final key = _shardKey(dependency.path);
shardsRoute.putIfAbsent(key, () => {})[dependency.path] = entry
.toJson();
} else if (entries.containsKey(dependency.path)) {
final existing = entries[dependency.path]!;
final key = _shardKey(dependency.path);
shardsRoute.putIfAbsent(key, () => {})[dependency.path] = existing
.toJson();
}
final key = _shardKey(dependency.path);
shardsDep.putIfAbsent(key, () => {})[dependency.path] = dependency
.toJson();
}
entries.removeWhere((path, _) => !deps.containsKey(path));
await _saveRouteArtifacts(directory, entries);
final validPaths = deps.keys.toSet();
await _cleanAndWriteShards(
_routeShardsDir(directory),
shardsRoute,
validPaths,
);
await _cleanAndWriteShards(_depShardsDir(directory), shardsDep, validPaths);
final reverse = <String, List<String>>{};
final routes = <String, List<String>>{};
for (final dependency in deps.values) {
routes
.putIfAbsent(dependency.routeId, () => <String>[])
.add(dependency.path);
for (final tag in dependency.tags.toSet()) {
reverse.putIfAbsent(tag, () => <String>[]).add(dependency.path);
}
}
for (final paths in [...reverse.values, ...routes.values]) {
paths.sort();
}
await _writeJsonIndex(directory, '.meta_mantle_reverse_index.json', {
'tags': reverse,
'routes': routes,
});
final manifest = deps.values.toList()
..sort((a, b) => a.path.compareTo(b.path));
await _writeJsonIndex(directory, 'route-manifest.json', {
'routes': [
for (final dependency in manifest)
{
'path': dependency.path,
'routeId': dependency.routeId,
'canonicalUrl': dependency.canonicalUrl,
'indexable': dependency.indexable,
'generatedAt': dependency.generatedAt.toUtc().toIso8601String(),
'lastModified': dependency.lastModified?.toUtc().toIso8601String(),
},
],
});
}