|
| 1 | +use std::fs; |
| 2 | +use std::path::{Path, PathBuf}; |
| 3 | +use std::process::Command; |
| 4 | + |
| 5 | +use anyhow::{Context, Result}; |
| 6 | +use serde::Serialize; |
| 7 | + |
| 8 | +#[derive(Debug, Serialize)] |
| 9 | +pub struct SkillSummary { |
| 10 | + pub id: String, |
| 11 | + pub namespace: String, |
| 12 | + pub name: String, |
| 13 | + pub path: String, |
| 14 | + pub description: Option<String>, |
| 15 | + pub installed: Option<bool>, |
| 16 | +} |
| 17 | + |
| 18 | +#[derive(Debug, Serialize)] |
| 19 | +pub struct SkillList { |
| 20 | + pub source: Option<String>, |
| 21 | + pub resolved_ref: Option<String>, |
| 22 | + pub skills: Vec<SkillSummary>, |
| 23 | +} |
| 24 | + |
| 25 | +#[derive(Debug, Serialize)] |
| 26 | +pub struct SkillInstall { |
| 27 | + pub id: String, |
| 28 | + pub source: String, |
| 29 | + pub resolved_ref: Option<String>, |
| 30 | + pub installed_path: String, |
| 31 | +} |
| 32 | + |
| 33 | +pub fn list(source: Option<&str>, dest: Option<&Path>) -> Result<SkillList> { |
| 34 | + let Some(source) = source else { |
| 35 | + return Ok(SkillList { |
| 36 | + source: None, |
| 37 | + resolved_ref: None, |
| 38 | + skills: Vec::new(), |
| 39 | + }); |
| 40 | + }; |
| 41 | + reject_remote_source(source)?; |
| 42 | + let source_path = PathBuf::from(source); |
| 43 | + let root = skills_root(&source_path); |
| 44 | + let mut skills = Vec::new(); |
| 45 | + |
| 46 | + if root.exists() { |
| 47 | + for namespace in read_dirs(&root)? { |
| 48 | + for skill in read_dirs(&namespace)? { |
| 49 | + let skill_file = skill.join("SKILL.md"); |
| 50 | + if !skill_file.exists() { |
| 51 | + continue; |
| 52 | + } |
| 53 | + let namespace_name = file_name(&namespace)?; |
| 54 | + let skill_name = file_name(&skill)?; |
| 55 | + let id = format!("{namespace_name}/{skill_name}"); |
| 56 | + skills.push(SkillSummary { |
| 57 | + id: id.clone(), |
| 58 | + namespace: namespace_name, |
| 59 | + name: skill_name, |
| 60 | + path: skill.display().to_string(), |
| 61 | + description: read_description(&skill_file)?, |
| 62 | + installed: dest.map(|dest| dest.join(&id).join("SKILL.md").exists()), |
| 63 | + }); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + skills.sort_by(|left, right| left.id.cmp(&right.id)); |
| 69 | + |
| 70 | + Ok(SkillList { |
| 71 | + source: Some(source_path.display().to_string()), |
| 72 | + resolved_ref: git_head(&source_path), |
| 73 | + skills, |
| 74 | + }) |
| 75 | +} |
| 76 | + |
| 77 | +pub fn install(source: &str, dest: &Path, id: &str, force: bool) -> Result<SkillInstall> { |
| 78 | + reject_remote_source(source)?; |
| 79 | + let source_path = PathBuf::from(source); |
| 80 | + let root = skills_root(&source_path); |
| 81 | + let source_skill = root.join(id); |
| 82 | + let skill_file = source_skill.join("SKILL.md"); |
| 83 | + if !skill_file.exists() { |
| 84 | + anyhow::bail!("Skill not found in source: {id}"); |
| 85 | + } |
| 86 | + |
| 87 | + let target = dest.join(id); |
| 88 | + if target.exists() { |
| 89 | + if force { |
| 90 | + fs::remove_dir_all(&target)?; |
| 91 | + } else { |
| 92 | + anyhow::bail!("Skill already exists at {}", target.display()); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + copy_dir(&source_skill, &target)?; |
| 97 | + |
| 98 | + Ok(SkillInstall { |
| 99 | + id: id.to_string(), |
| 100 | + source: source_path.display().to_string(), |
| 101 | + resolved_ref: git_head(&source_path), |
| 102 | + installed_path: target.display().to_string(), |
| 103 | + }) |
| 104 | +} |
| 105 | + |
| 106 | +fn skills_root(source: &Path) -> PathBuf { |
| 107 | + let nested = source.join("skills"); |
| 108 | + if nested.exists() { |
| 109 | + nested |
| 110 | + } else { |
| 111 | + source.to_path_buf() |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +fn read_dirs(path: &Path) -> Result<Vec<PathBuf>> { |
| 116 | + let mut dirs = Vec::new(); |
| 117 | + for entry in fs::read_dir(path)? { |
| 118 | + let entry = entry?; |
| 119 | + if entry.file_type()?.is_dir() { |
| 120 | + dirs.push(entry.path()); |
| 121 | + } |
| 122 | + } |
| 123 | + dirs.sort(); |
| 124 | + Ok(dirs) |
| 125 | +} |
| 126 | + |
| 127 | +fn file_name(path: &Path) -> Result<String> { |
| 128 | + Ok(path |
| 129 | + .file_name() |
| 130 | + .and_then(|name| name.to_str()) |
| 131 | + .ok_or_else(|| anyhow::anyhow!("Invalid skill path: {}", path.display()))? |
| 132 | + .to_string()) |
| 133 | +} |
| 134 | + |
| 135 | +fn read_description(path: &Path) -> Result<Option<String>> { |
| 136 | + let body = fs::read_to_string(path)?; |
| 137 | + let mut in_frontmatter = false; |
| 138 | + for line in body.lines() { |
| 139 | + let trimmed = line.trim(); |
| 140 | + if trimmed == "---" { |
| 141 | + if in_frontmatter { |
| 142 | + break; |
| 143 | + } |
| 144 | + in_frontmatter = true; |
| 145 | + continue; |
| 146 | + } |
| 147 | + if in_frontmatter { |
| 148 | + if let Some(description) = trimmed.strip_prefix("description:") { |
| 149 | + return Ok(Some(description.trim().trim_matches('"').to_string())); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + Ok(None) |
| 154 | +} |
| 155 | + |
| 156 | +fn copy_dir(source: &Path, target: &Path) -> Result<()> { |
| 157 | + fs::create_dir_all(target)?; |
| 158 | + for entry in fs::read_dir(source)? { |
| 159 | + let entry = entry?; |
| 160 | + let source_path = entry.path(); |
| 161 | + let target_path = target.join(entry.file_name()); |
| 162 | + if entry.file_type()?.is_dir() { |
| 163 | + copy_dir(&source_path, &target_path)?; |
| 164 | + } else { |
| 165 | + fs::copy(&source_path, &target_path) |
| 166 | + .with_context(|| format!("failed to copy {}", source_path.display()))?; |
| 167 | + } |
| 168 | + } |
| 169 | + Ok(()) |
| 170 | +} |
| 171 | + |
| 172 | +fn reject_remote_source(source: &str) -> Result<()> { |
| 173 | + if source.starts_with("https://") || source.starts_with("http://") || source.starts_with("git@") |
| 174 | + { |
| 175 | + anyhow::bail!( |
| 176 | + "Remote skill sources are not fetched by this command. Clone and pin the source locally, then pass the local path." |
| 177 | + ); |
| 178 | + } |
| 179 | + Ok(()) |
| 180 | +} |
| 181 | + |
| 182 | +fn git_head(path: &Path) -> Option<String> { |
| 183 | + let output = Command::new("git") |
| 184 | + .arg("-C") |
| 185 | + .arg(path) |
| 186 | + .arg("rev-parse") |
| 187 | + .arg("HEAD") |
| 188 | + .output() |
| 189 | + .ok()?; |
| 190 | + if output.status.success() { |
| 191 | + Some(String::from_utf8_lossy(&output.stdout).trim().to_string()) |
| 192 | + } else { |
| 193 | + None |
| 194 | + } |
| 195 | +} |
0 commit comments