|
| 1 | +//! Walk-up manifest detection — tries each known manifest format in priority order, returning |
| 2 | +//! the first one found by ascending from the process CWD. |
| 3 | +
|
| 4 | +use std::path::{Path, PathBuf}; |
| 5 | + |
| 6 | +use serde::Deserialize; |
| 7 | + |
| 8 | +#[derive(Debug, Clone, Default)] |
| 9 | +pub struct ManifestData { |
| 10 | + pub name: Option<String>, |
| 11 | + pub version: Option<String>, |
| 12 | + pub description: Option<String>, |
| 13 | + pub license: Option<String>, |
| 14 | + pub ecosystem: &'static str, |
| 15 | +} |
| 16 | + |
| 17 | +pub fn detect_manifest() -> Option<ManifestData> { |
| 18 | + let cwd = std::env::current_dir().ok()?; |
| 19 | + walk_up(&cwd) |
| 20 | +} |
| 21 | + |
| 22 | +pub fn cwd_cache_component() -> String { |
| 23 | + std::env::current_dir() |
| 24 | + .map(|p| p.display().to_string()) |
| 25 | + .unwrap_or_default() |
| 26 | +} |
| 27 | + |
| 28 | +fn walk_up(start: &Path) -> Option<ManifestData> { |
| 29 | + let mut cur: &Path = start; |
| 30 | + loop { |
| 31 | + if let Some(d) = try_in_dir(cur) { |
| 32 | + return Some(d); |
| 33 | + } |
| 34 | + cur = cur.parent()?; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +fn try_in_dir(dir: &Path) -> Option<ManifestData> { |
| 39 | + try_cargo(dir) |
| 40 | + .or_else(|| try_package_json(dir)) |
| 41 | + .or_else(|| try_pyproject(dir)) |
| 42 | + .or_else(|| try_go_mod(dir)) |
| 43 | + .or_else(|| try_composer(dir)) |
| 44 | +} |
| 45 | + |
| 46 | +// ── Cargo.toml ────────────────────────────────────────────────────────────── |
| 47 | + |
| 48 | +#[derive(Deserialize)] |
| 49 | +struct CargoToml { |
| 50 | + package: Option<CargoPackage>, |
| 51 | +} |
| 52 | + |
| 53 | +#[derive(Deserialize)] |
| 54 | +struct CargoPackage { |
| 55 | + name: Option<String>, |
| 56 | + version: Option<toml::Value>, |
| 57 | + description: Option<String>, |
| 58 | + license: Option<String>, |
| 59 | +} |
| 60 | + |
| 61 | +fn try_cargo(dir: &Path) -> Option<ManifestData> { |
| 62 | + let text = read_file(dir.join("Cargo.toml"))?; |
| 63 | + let t: CargoToml = toml::from_str(&text).ok()?; |
| 64 | + let pkg = t.package?; |
| 65 | + Some(ManifestData { |
| 66 | + name: pkg.name, |
| 67 | + version: toml_string(pkg.version), |
| 68 | + description: pkg.description, |
| 69 | + license: pkg.license, |
| 70 | + ecosystem: "cargo", |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +fn toml_string(v: Option<toml::Value>) -> Option<String> { |
| 75 | + match v? { |
| 76 | + toml::Value::String(s) => Some(s), |
| 77 | + toml::Value::Table(t) => t |
| 78 | + .get("version") |
| 79 | + .and_then(|v| v.as_str().map(str::to_string)), |
| 80 | + _ => None, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// ── package.json ───────────────────────────────────────────────────────────── |
| 85 | + |
| 86 | +#[derive(Deserialize)] |
| 87 | +struct PackageJson { |
| 88 | + name: Option<String>, |
| 89 | + version: Option<String>, |
| 90 | + description: Option<String>, |
| 91 | + license: Option<serde_json::Value>, |
| 92 | +} |
| 93 | + |
| 94 | +fn try_package_json(dir: &Path) -> Option<ManifestData> { |
| 95 | + let text = read_file(dir.join("package.json"))?; |
| 96 | + let p: PackageJson = serde_json::from_str(&text).ok()?; |
| 97 | + Some(ManifestData { |
| 98 | + name: p.name, |
| 99 | + version: p.version, |
| 100 | + description: p.description, |
| 101 | + license: p.license.and_then(json_license), |
| 102 | + ecosystem: "npm", |
| 103 | + }) |
| 104 | +} |
| 105 | + |
| 106 | +fn json_license(v: serde_json::Value) -> Option<String> { |
| 107 | + match v { |
| 108 | + serde_json::Value::String(s) => Some(s), |
| 109 | + serde_json::Value::Object(m) => m.get("type").and_then(|v| v.as_str().map(str::to_string)), |
| 110 | + _ => None, |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// ── pyproject.toml ─────────────────────────────────────────────────────────── |
| 115 | + |
| 116 | +#[derive(Deserialize)] |
| 117 | +struct PyprojectToml { |
| 118 | + project: Option<PyprojectProject>, |
| 119 | + tool: Option<PyprojectTool>, |
| 120 | +} |
| 121 | + |
| 122 | +#[derive(Deserialize)] |
| 123 | +struct PyprojectProject { |
| 124 | + name: Option<String>, |
| 125 | + version: Option<String>, |
| 126 | + description: Option<String>, |
| 127 | + license: Option<toml::Value>, |
| 128 | +} |
| 129 | + |
| 130 | +#[derive(Deserialize)] |
| 131 | +struct PyprojectTool { |
| 132 | + poetry: Option<PoetryProject>, |
| 133 | +} |
| 134 | + |
| 135 | +#[derive(Deserialize)] |
| 136 | +struct PoetryProject { |
| 137 | + name: Option<String>, |
| 138 | + version: Option<String>, |
| 139 | + description: Option<String>, |
| 140 | + license: Option<String>, |
| 141 | +} |
| 142 | + |
| 143 | +fn try_pyproject(dir: &Path) -> Option<ManifestData> { |
| 144 | + let text = read_file(dir.join("pyproject.toml"))?; |
| 145 | + let t: PyprojectToml = toml::from_str(&text).ok()?; |
| 146 | + if let Some(p) = t.project { |
| 147 | + return Some(ManifestData { |
| 148 | + name: p.name, |
| 149 | + version: p.version, |
| 150 | + description: p.description, |
| 151 | + license: pyproject_license(p.license), |
| 152 | + ecosystem: "python", |
| 153 | + }); |
| 154 | + } |
| 155 | + if let Some(poetry) = t.tool.and_then(|t| t.poetry) { |
| 156 | + return Some(ManifestData { |
| 157 | + name: poetry.name, |
| 158 | + version: poetry.version, |
| 159 | + description: poetry.description, |
| 160 | + license: poetry.license, |
| 161 | + ecosystem: "python", |
| 162 | + }); |
| 163 | + } |
| 164 | + None |
| 165 | +} |
| 166 | + |
| 167 | +fn pyproject_license(v: Option<toml::Value>) -> Option<String> { |
| 168 | + match v? { |
| 169 | + toml::Value::String(s) => Some(s), |
| 170 | + toml::Value::Table(t) => t |
| 171 | + .get("text") |
| 172 | + .or_else(|| t.get("expression")) |
| 173 | + .and_then(|v| v.as_str().map(str::to_string)), |
| 174 | + _ => None, |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +// ── go.mod ─────────────────────────────────────────────────────────────────── |
| 179 | + |
| 180 | +fn try_go_mod(dir: &Path) -> Option<ManifestData> { |
| 181 | + let text = read_file(dir.join("go.mod"))?; |
| 182 | + let name = text |
| 183 | + .lines() |
| 184 | + .find(|l| l.starts_with("module "))? |
| 185 | + .strip_prefix("module ")? |
| 186 | + .trim() |
| 187 | + .to_string(); |
| 188 | + Some(ManifestData { |
| 189 | + name: Some(name), |
| 190 | + version: None, |
| 191 | + description: None, |
| 192 | + license: None, |
| 193 | + ecosystem: "go", |
| 194 | + }) |
| 195 | +} |
| 196 | + |
| 197 | +// ── composer.json ──────────────────────────────────────────────────────────── |
| 198 | + |
| 199 | +#[derive(Deserialize)] |
| 200 | +struct ComposerJson { |
| 201 | + name: Option<String>, |
| 202 | + version: Option<String>, |
| 203 | + description: Option<String>, |
| 204 | + license: Option<serde_json::Value>, |
| 205 | +} |
| 206 | + |
| 207 | +fn try_composer(dir: &Path) -> Option<ManifestData> { |
| 208 | + let text = read_file(dir.join("composer.json"))?; |
| 209 | + let c: ComposerJson = serde_json::from_str(&text).ok()?; |
| 210 | + Some(ManifestData { |
| 211 | + name: c.name, |
| 212 | + version: c.version, |
| 213 | + description: c.description, |
| 214 | + license: c.license.and_then(|v| match v { |
| 215 | + serde_json::Value::String(s) => Some(s), |
| 216 | + serde_json::Value::Array(arr) => arr |
| 217 | + .into_iter() |
| 218 | + .filter_map(|v| v.as_str().map(str::to_string)) |
| 219 | + .reduce(|a, b| format!("{a}/{b}")), |
| 220 | + _ => None, |
| 221 | + }), |
| 222 | + ecosystem: "php", |
| 223 | + }) |
| 224 | +} |
| 225 | + |
| 226 | +// ── I/O helper ─────────────────────────────────────────────────────────────── |
| 227 | + |
| 228 | +fn read_file(path: PathBuf) -> Option<String> { |
| 229 | + std::fs::read_to_string(path).ok() |
| 230 | +} |
| 231 | + |
| 232 | +#[cfg(test)] |
| 233 | +mod tests { |
| 234 | + use super::*; |
| 235 | + use std::fs; |
| 236 | + use tempfile::tempdir; |
| 237 | + |
| 238 | + #[test] |
| 239 | + fn detects_cargo_toml() { |
| 240 | + let dir = tempdir().unwrap(); |
| 241 | + fs::write( |
| 242 | + dir.path().join("Cargo.toml"), |
| 243 | + "[package]\nname = \"myapp\"\nversion = \"1.2.3\"\ndescription = \"A tool\"\nlicense = \"MIT\"\n", |
| 244 | + ) |
| 245 | + .unwrap(); |
| 246 | + let d = try_in_dir(dir.path()).unwrap(); |
| 247 | + assert_eq!(d.name.as_deref(), Some("myapp")); |
| 248 | + assert_eq!(d.version.as_deref(), Some("1.2.3")); |
| 249 | + assert_eq!(d.description.as_deref(), Some("A tool")); |
| 250 | + assert_eq!(d.license.as_deref(), Some("MIT")); |
| 251 | + assert_eq!(d.ecosystem, "cargo"); |
| 252 | + } |
| 253 | + |
| 254 | + #[test] |
| 255 | + fn detects_package_json() { |
| 256 | + let dir = tempdir().unwrap(); |
| 257 | + fs::write( |
| 258 | + dir.path().join("package.json"), |
| 259 | + r#"{"name":"myapp","version":"0.1.0","description":"My app","license":"ISC"}"#, |
| 260 | + ) |
| 261 | + .unwrap(); |
| 262 | + let d = try_in_dir(dir.path()).unwrap(); |
| 263 | + assert_eq!(d.name.as_deref(), Some("myapp")); |
| 264 | + assert_eq!(d.version.as_deref(), Some("0.1.0")); |
| 265 | + assert_eq!(d.ecosystem, "npm"); |
| 266 | + } |
| 267 | + |
| 268 | + #[test] |
| 269 | + fn detects_pyproject_pep621() { |
| 270 | + let dir = tempdir().unwrap(); |
| 271 | + fs::write( |
| 272 | + dir.path().join("pyproject.toml"), |
| 273 | + "[project]\nname = \"mypkg\"\nversion = \"3.0.0\"\ndescription = \"Python thing\"\n", |
| 274 | + ) |
| 275 | + .unwrap(); |
| 276 | + let d = try_in_dir(dir.path()).unwrap(); |
| 277 | + assert_eq!(d.name.as_deref(), Some("mypkg")); |
| 278 | + assert_eq!(d.version.as_deref(), Some("3.0.0")); |
| 279 | + assert_eq!(d.ecosystem, "python"); |
| 280 | + } |
| 281 | + |
| 282 | + #[test] |
| 283 | + fn detects_pyproject_poetry() { |
| 284 | + let dir = tempdir().unwrap(); |
| 285 | + fs::write( |
| 286 | + dir.path().join("pyproject.toml"), |
| 287 | + "[tool.poetry]\nname = \"poem\"\nversion = \"0.5.0\"\ndescription = \"Verse\"\nlicense = \"Apache-2.0\"\n", |
| 288 | + ) |
| 289 | + .unwrap(); |
| 290 | + let d = try_in_dir(dir.path()).unwrap(); |
| 291 | + assert_eq!(d.name.as_deref(), Some("poem")); |
| 292 | + assert_eq!(d.ecosystem, "python"); |
| 293 | + } |
| 294 | + |
| 295 | + #[test] |
| 296 | + fn detects_go_mod() { |
| 297 | + let dir = tempdir().unwrap(); |
| 298 | + fs::write( |
| 299 | + dir.path().join("go.mod"), |
| 300 | + "module github.com/user/repo\n\ngo 1.21\n", |
| 301 | + ) |
| 302 | + .unwrap(); |
| 303 | + let d = try_in_dir(dir.path()).unwrap(); |
| 304 | + assert_eq!(d.name.as_deref(), Some("github.com/user/repo")); |
| 305 | + assert_eq!(d.ecosystem, "go"); |
| 306 | + } |
| 307 | + |
| 308 | + #[test] |
| 309 | + fn walk_up_finds_manifest_in_parent() { |
| 310 | + let dir = tempdir().unwrap(); |
| 311 | + fs::write( |
| 312 | + dir.path().join("Cargo.toml"), |
| 313 | + "[package]\nname = \"parent\"\nversion = \"0.0.1\"\n", |
| 314 | + ) |
| 315 | + .unwrap(); |
| 316 | + let child = dir.path().join("src").join("lib"); |
| 317 | + fs::create_dir_all(&child).unwrap(); |
| 318 | + let d = walk_up(&child).unwrap(); |
| 319 | + assert_eq!(d.name.as_deref(), Some("parent")); |
| 320 | + } |
| 321 | + |
| 322 | + #[test] |
| 323 | + fn returns_none_when_no_manifest_found() { |
| 324 | + let dir = tempdir().unwrap(); |
| 325 | + assert!(try_in_dir(dir.path()).is_none()); |
| 326 | + } |
| 327 | + |
| 328 | + #[test] |
| 329 | + fn cargo_takes_priority_over_package_json() { |
| 330 | + let dir = tempdir().unwrap(); |
| 331 | + fs::write( |
| 332 | + dir.path().join("Cargo.toml"), |
| 333 | + "[package]\nname = \"rust-app\"\nversion = \"1.0.0\"\n", |
| 334 | + ) |
| 335 | + .unwrap(); |
| 336 | + fs::write( |
| 337 | + dir.path().join("package.json"), |
| 338 | + r#"{"name":"js-app","version":"2.0.0"}"#, |
| 339 | + ) |
| 340 | + .unwrap(); |
| 341 | + let d = try_in_dir(dir.path()).unwrap(); |
| 342 | + assert_eq!(d.ecosystem, "cargo"); |
| 343 | + } |
| 344 | +} |
0 commit comments