use std::path::{Path, PathBuf};
use openmoat_hosts::{Bundle, Host};
use serde::Deserialize;
use super::host_version::{Version, parse};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BundledCopy {
pub editor: &'static str,
pub dir: PathBuf,
pub version: Version,
}
pub fn copies(host: Host) -> Vec<BundledCopy> {
let Ok(home) = crate::home::user_home() else {
return Vec::new();
};
host.spec()
.bundled
.iter()
.flat_map(|bundle| in_home(&home, bundle))
.collect()
}
fn in_home(home: &Path, bundle: &Bundle) -> Vec<BundledCopy> {
let dir = bundle
.extensions_dir
.iter()
.fold(home.to_path_buf(), |dir, part| dir.join(part));
let mut found = match std::fs::read(dir.join("extensions.json")) {
Ok(bytes) => listed(&dir, bundle, &bytes),
Err(_) => scanned(&dir, bundle),
};
found.sort_by_key(|copy| std::cmp::Reverse(copy.version));
found
}
#[derive(Deserialize)]
struct Entry {
identifier: Identifier,
version: String,
#[serde(rename = "relativeLocation")]
relative_location: Option<String>,
}
#[derive(Deserialize)]
struct Identifier {
id: String,
}
fn listed(dir: &Path, bundle: &Bundle, bytes: &[u8]) -> Vec<BundledCopy> {
let Ok(entries) = serde_json::from_slice::<Vec<serde_json::Value>>(bytes) else {
return scanned(dir, bundle);
};
entries
.into_iter()
.filter_map(|value| serde_json::from_value::<Entry>(value).ok())
.filter(|e| e.identifier.id.eq_ignore_ascii_case(bundle.extension_id))
.filter_map(|e| {
let location = e.relative_location?;
let inside = Path::new(&location)
.components()
.all(|part| matches!(part, std::path::Component::Normal(_)));
if !inside {
return None;
}
let folder = dir.join(location);
let version = parse(&e.version)?;
folder.is_dir().then_some(BundledCopy {
editor: bundle.editor,
dir: folder,
version,
})
})
.collect()
}
fn scanned(dir: &Path, bundle: &Bundle) -> Vec<BundledCopy> {
let Ok(entries) = std::fs::read_dir(dir) else {
return Vec::new();
};
let prefix = format!("{}-", bundle.extension_id);
entries
.filter_map(Result::ok)
.filter(|e| e.path().is_dir())
.filter_map(|e| {
let name = e.file_name().to_string_lossy().to_ascii_lowercase();
let version = parse(name.strip_prefix(&prefix)?)?;
Some(BundledCopy {
editor: bundle.editor,
dir: e.path(),
version,
})
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
const BUNDLE: Bundle = Bundle::new(
"VS Code",
&[".vscode", "extensions"],
"anthropic.claude-code",
);
fn extensions(home: &Path) -> PathBuf {
let dir = home.join(".vscode/extensions");
std::fs::create_dir_all(&dir).unwrap();
dir
}
#[test]
fn the_list_names_the_installed_copies_whose_folders_are_there() {
let home = tempfile::tempdir().unwrap();
let dir = extensions(home.path());
std::fs::create_dir(dir.join("anthropic.claude-code-2.1.318-darwin-arm64")).unwrap();
std::fs::create_dir(dir.join("anthropic.claude-code-2.1.200-darwin-arm64")).unwrap();
let list = serde_json::json!([
{"identifier": {"id": "Anthropic.claude-code"}, "version": "2.1.318",
"relativeLocation": "anthropic.claude-code-2.1.318-darwin-arm64"},
{"identifier": {"id": "anthropic.claude-code"}, "version": "2.1.100",
"relativeLocation": "anthropic.claude-code-2.1.100-darwin-arm64"},
{"identifier": {"id": "ms-python.python"}, "version": "2026.1.0",
"relativeLocation": "ms-python.python-2026.1.0"},
{"identifier": {"id": "anthropic.claude-code"}, "version": "9.9.9",
"relativeLocation": "../../outside"},
{"not": "an entry"}
]);
std::fs::create_dir(home.path().join("outside")).unwrap();
std::fs::write(dir.join("extensions.json"), list.to_string()).unwrap();
let found = in_home(home.path(), &BUNDLE);
assert_eq!(found.len(), 1, "{found:?}");
assert_eq!(found[0].version, (2, 1, 318));
assert!(
found[0]
.dir
.ends_with("anthropic.claude-code-2.1.318-darwin-arm64")
);
}
#[test]
fn without_a_list_every_folder_counts() {
let home = tempfile::tempdir().unwrap();
let dir = extensions(home.path());
for name in [
"anthropic.claude-code-2.1.300",
"anthropic.claude-code-2.1.318-win32-x64",
"anthropic.claude-code-x",
"other.claude-code-9.9.9",
] {
std::fs::create_dir(dir.join(name)).unwrap();
}
std::fs::write(dir.join("anthropic.claude-code-3.0.0"), "a file").unwrap();
let found: Vec<Version> = in_home(home.path(), &BUNDLE)
.into_iter()
.map(|c| c.version)
.collect();
assert_eq!(found, [(2, 1, 318), (2, 1, 300)]);
std::fs::write(dir.join("extensions.json"), "not json").unwrap();
assert_eq!(in_home(home.path(), &BUNDLE).len(), 2, "an unreadable list");
}
#[test]
fn no_folder_no_copies() {
let home = tempfile::tempdir().unwrap();
assert_eq!(in_home(home.path(), &BUNDLE), Vec::new());
}
}