use std::path::{Path, PathBuf};
pub const VENDOR_ROOT: &str = ".ask/skills";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VendorResult {
pub vendor_dir: PathBuf,
pub skill_names: Vec<String>,
}
fn copy_dir_recursive(src: &Path, dst: &Path) -> std::io::Result<()> {
let root = std::fs::canonicalize(src).unwrap_or_else(|_| src.to_path_buf());
let mut visited: std::collections::HashSet<PathBuf> = std::collections::HashSet::new();
visited.insert(root.clone());
copy_dir_inner(&root, src, dst, &mut visited)
}
fn copy_dir_inner(
root: &Path,
src: &Path,
dst: &Path,
visited: &mut std::collections::HashSet<PathBuf>,
) -> std::io::Result<()> {
std::fs::create_dir_all(dst)?;
for entry in std::fs::read_dir(src)? {
let entry = entry?;
let from = entry.path();
let to = dst.join(entry.file_name());
let is_dir = std::fs::metadata(&from)
.map(|m| m.is_dir())
.unwrap_or_else(|_| entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false));
if is_dir {
match std::fs::canonicalize(&from) {
Ok(real) if real.starts_with(root) && visited.insert(real.clone()) => {
copy_dir_inner(root, &from, &to, visited)?;
visited.remove(&real);
}
_ => {}
}
} else {
let is_symlink = entry.file_type().map(|ft| ft.is_symlink()).unwrap_or(false);
if is_symlink {
match std::fs::canonicalize(&from) {
Ok(real) if real.starts_with(root) => {
std::fs::copy(&from, &to)?;
}
_ => {} }
} else {
std::fs::copy(&from, &to)?;
}
}
}
Ok(())
}
pub fn vendor_skills(
project_dir: &Path,
spec_key: &str,
sources: &[PathBuf],
) -> anyhow::Result<VendorResult> {
let root = project_dir.join(VENDOR_ROOT);
let vendor_dir = root.join(spec_key);
std::fs::create_dir_all(&root)?;
let staging = root.join(format!(".{spec_key}.tmp"));
if staging.exists() {
std::fs::remove_dir_all(&staging)?;
}
std::fs::create_dir_all(&staging)?;
let mut skill_names: Vec<String> = Vec::new();
for source in sources {
if !source.is_dir() {
continue;
}
let Some(name) = source.file_name() else {
continue;
};
let target = staging.join(name);
copy_dir_recursive(source, &target)?;
skill_names.push(name.to_string_lossy().into_owned());
}
if vendor_dir.exists() {
std::fs::remove_dir_all(&vendor_dir)?;
}
std::fs::rename(&staging, &vendor_dir)?;
Ok(VendorResult {
vendor_dir,
skill_names,
})
}
pub fn remove_vendor_dir(project_dir: &Path, spec_key: &str) -> anyhow::Result<()> {
let vendor_dir = project_dir.join(VENDOR_ROOT).join(spec_key);
if vendor_dir.exists() {
std::fs::remove_dir_all(&vendor_dir)?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn make_skill(root: &Path, name: &str) -> PathBuf {
let dir = root.join(name);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("SKILL.md"), format!("# {name}")).unwrap();
dir
}
#[test]
fn vendors_sources_and_reports_names() {
let proj = tempfile::tempdir().unwrap();
let srcroot = tempfile::tempdir().unwrap();
let a = make_skill(srcroot.path(), "a");
let b = make_skill(srcroot.path(), "b");
let res = vendor_skills(proj.path(), "npm__x__1", &[a, b]).unwrap();
assert_eq!(res.skill_names, vec!["a", "b"]);
assert!(res.vendor_dir.ends_with(".ask/skills/npm__x__1"));
assert!(res.vendor_dir.join("a/SKILL.md").exists());
assert!(res.vendor_dir.join("b/SKILL.md").exists());
}
#[test]
fn reinstall_replaces_stale_contents() {
let proj = tempfile::tempdir().unwrap();
let srcroot = tempfile::tempdir().unwrap();
let a = make_skill(srcroot.path(), "a");
vendor_skills(proj.path(), "k", std::slice::from_ref(&a)).unwrap();
let c = make_skill(srcroot.path(), "c");
let res = vendor_skills(proj.path(), "k", &[c]).unwrap();
assert_eq!(res.skill_names, vec!["c"]);
assert!(!res.vendor_dir.join("a").exists());
assert!(res.vendor_dir.join("c/SKILL.md").exists());
}
#[test]
fn skips_missing_and_non_dir_sources() {
let proj = tempfile::tempdir().unwrap();
let srcroot = tempfile::tempdir().unwrap();
let a = make_skill(srcroot.path(), "a");
let missing = srcroot.path().join("nope");
let res = vendor_skills(proj.path(), "k", &[a, missing]).unwrap();
assert_eq!(res.skill_names, vec!["a"]);
}
#[test]
fn remove_vendor_dir_is_idempotent() {
let proj = tempfile::tempdir().unwrap();
let srcroot = tempfile::tempdir().unwrap();
let a = make_skill(srcroot.path(), "a");
vendor_skills(proj.path(), "k", &[a]).unwrap();
remove_vendor_dir(proj.path(), "k").unwrap();
assert!(!proj.path().join(".ask/skills/k").exists());
remove_vendor_dir(proj.path(), "k").unwrap();
}
#[cfg(unix)]
#[test]
fn copies_through_a_directory_symlink() {
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("src");
let real = src.join("real");
std::fs::create_dir_all(&real).unwrap();
std::fs::write(real.join("a.md"), "A").unwrap();
std::os::unix::fs::symlink("real", src.join("link")).unwrap();
let dst = dir.path().join("dst");
copy_dir_recursive(&src, &dst).unwrap();
assert_eq!(std::fs::read_to_string(dst.join("real/a.md")).unwrap(), "A");
assert_eq!(std::fs::read_to_string(dst.join("link/a.md")).unwrap(), "A");
}
#[cfg(unix)]
#[test]
fn symlink_cycle_and_escape_are_guarded() {
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("src");
std::fs::create_dir_all(&src).unwrap();
std::fs::write(src.join("a.md"), "A").unwrap();
std::os::unix::fs::symlink(".", src.join("loop")).unwrap();
let outside = dir.path().join("outside");
std::fs::create_dir_all(&outside).unwrap();
std::fs::write(outside.join("secret.md"), "SECRET").unwrap();
std::os::unix::fs::symlink(&outside, src.join("escape")).unwrap();
std::fs::write(outside.join("secret.txt"), "FILESECRET").unwrap();
std::os::unix::fs::symlink(outside.join("secret.txt"), src.join("leak.md")).unwrap();
let dst = dir.path().join("dst");
copy_dir_recursive(&src, &dst).unwrap();
assert_eq!(std::fs::read_to_string(dst.join("a.md")).unwrap(), "A");
assert!(!dst.join("escape/secret.md").exists());
assert!(!dst.join("leak.md").exists());
}
}