writeDirectory static method
Write bundle to a fresh .mbd/ tree at mbdPath.
Steps:
- Create
mbdPath(recursive). When the directory exists and is non-empty, throw BundleWriteException unlessoverwriteistrue, in which case existing entries are removed first. - Serialise
bundleviabundle.toJsonand write JSON to<mbdPath>/manifest.jsonindented byindentspaces (default 2; pass 0 for single-line output). - For every entry in
reservedFiles, invoke the matching BundleResources method:String→ BundleResources.writeUint8List/List<int>→ BundleResources.writeBytes- any other Object → BundleResources.writeJson (same indent)
Returns the absolute path of the written .mbd/ directory.
Implementation
static Future<String> writeDirectory(
McpBundle bundle,
String mbdPath, {
Map<BundleFolder, Map<String, Object>> reservedFiles = const {},
int indent = 2,
bool overwrite = false,
}) async {
final dir = Directory(mbdPath);
if (await dir.exists()) {
final existing = await dir.list().toList();
if (existing.isNotEmpty) {
if (!overwrite) {
throw BundleWriteException(
'Refusing to overwrite non-empty directory: $mbdPath '
'(pass overwrite: true to replace contents)',
uri: Uri.directory(dir.absolute.path),
);
}
for (final entity in existing) {
await entity.delete(recursive: true);
}
}
} else {
await dir.create(recursive: true);
}
final absRoot = dir.absolute.path;
await _writeManifestFile(bundle, absRoot, indent: indent);
for (final entry in reservedFiles.entries) {
final resources = BundleResources(
bundleRoot: absRoot,
folder: entry.key,
);
for (final fileEntry in entry.value.entries) {
final value = fileEntry.value;
if (value is String) {
await resources.write(fileEntry.key, value);
} else if (value is Uint8List) {
await resources.writeBytes(fileEntry.key, value);
} else if (value is List<int>) {
await resources.writeBytes(
fileEntry.key,
Uint8List.fromList(value),
);
} else {
await resources.writeJson(
fileEntry.key,
value,
indent: indent,
);
}
}
}
return absRoot;
}