1use crate::lockfile::{self, LOCKFILES};
4use crate::{locate, norm_name, Dep, Eco};
5use std::collections::HashSet;
6use std::path::{Path, PathBuf};
7
8#[derive(Debug, Clone)]
9pub struct Project {
10 pub root: PathBuf,
11 pub lockfiles: Vec<String>,
13 pub deps: Vec<Dep>,
14 pub warnings: Vec<String>,
15}
16
17#[derive(Debug, Clone, PartialEq)]
20pub struct Spec {
21 pub eco: Option<Eco>,
22 pub name: String,
23 pub version: Option<String>,
24}
25
26impl Spec {
27 pub fn parse(s: &str) -> Spec {
28 let s = s.trim();
29 let (eco, rest) = match s.split_once(':') {
30 Some((e, r)) if Eco::parse(e).is_some() && !e.contains('/') => (Eco::parse(e), r),
31 _ => (None, s),
32 };
33 let start = if rest.starts_with('@') { 1 } else { 0 };
34 let (name, version) = match rest[start..].find('@') {
35 Some(i) => (&rest[..i + start], Some(rest[i + start + 1..].trim_start_matches('v').to_string())),
36 None => match rest.find("==") {
37 Some(i) => (&rest[..i], Some(rest[i + 2..].to_string())),
38 None => (rest, None),
39 },
40 };
41 let version = version.filter(|v| !v.is_empty());
42 Spec {
43 eco,
44 name: name.to_string(),
45 version,
46 }
47 }
48
49 fn name_matches(&self, d: &Dep) -> bool {
50 if let Some(e) = self.eco {
51 if e != d.eco {
52 return false;
53 }
54 }
55 if norm_name(d.eco, &d.name) == norm_name(d.eco, &self.name) {
56 return true;
57 }
58 if d.eco == Eco::Go {
60 let segs: Vec<&str> = d.name.split('/').collect();
61 let base = match segs.last() {
62 Some(l) if l.len() >= 2 && l.starts_with('v') && l[1..].chars().all(|c| c.is_ascii_digit()) && segs.len() > 1 => segs[segs.len() - 2],
63 Some(l) => l,
64 None => "",
65 };
66 return base == self.name || d.name.ends_with(&format!("/{}", self.name));
67 }
68 false
69 }
70
71 pub fn version_matches(&self, v: &str) -> bool {
74 match &self.version {
75 None => true,
76 Some(want) => {
77 let v = v.trim_start_matches('v');
78 v == want || v.starts_with(&format!("{want}.")) || v.starts_with(&format!("{want}-"))
79 }
80 }
81 }
82
83 pub fn is_exact(&self) -> bool {
84 self.version.as_ref().is_some_and(|v| v.split('.').count() >= 3 || self.eco == Some(Eco::Go))
85 }
86}
87
88fn declares(dir: &Path, eco: Eco) -> bool {
90 match eco {
91 Eco::Npm => dir.join("package.json").is_file(),
92 Eco::Cargo => dir.join("Cargo.toml").is_file(),
93 Eco::Go => dir.join("go.mod").is_file(),
94 Eco::PyPI => ["pyproject.toml", "setup.py", "setup.cfg", "Pipfile"].iter().any(|f| dir.join(f).is_file()),
95 }
96}
97
98impl Project {
99 pub fn load(root: &Path) -> Project {
100 let root = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());
101 let mut p = Project {
102 root: root.clone(),
103 lockfiles: Vec::new(),
104 deps: Vec::new(),
105 warnings: Vec::new(),
106 };
107 let mut have: HashSet<Eco> = HashSet::new();
108 let home = dirs::home_dir();
111 for (depth, dir) in root.ancestors().take(6).enumerate() {
112 if depth > 0 && Some(dir.to_path_buf()) == home {
113 break;
114 }
115 if depth == 1 {
116 for eco in Eco::all() {
119 if !declares(&root, eco) {
120 have.insert(eco);
121 }
122 }
123 if have.len() == 4 {
124 break;
125 }
126 }
127 p.read_dir(dir, &mut have);
128 if depth == 0 {
129 p.fallbacks(dir, &mut have);
130 }
131 if have.len() == 4 || dir.join(".git").exists() {
132 break;
133 }
134 }
135 let mut seen = HashSet::new();
137 p.deps.retain(|d| seen.insert((d.eco, norm_name(d.eco, &d.name), d.version.clone())));
138 p.deps.sort_by(|a, b| b.direct.cmp(&a.direct).then(a.eco.cmp(&b.eco)).then(a.name.cmp(&b.name)));
140 p
141 }
142
143 fn rel(&self, path: &Path) -> String {
144 path.strip_prefix(&self.root)
145 .map(|r| r.display().to_string())
146 .unwrap_or_else(|_| path.display().to_string())
147 .replace('\\', "/")
148 }
149
150 fn read_dir(&mut self, dir: &Path, have: &mut HashSet<Eco>) {
151 let mut found_here: HashSet<Eco> = HashSet::new();
152 for (name, eco) in LOCKFILES {
153 if have.contains(eco) || found_here.contains(eco) {
154 continue;
155 }
156 let path = dir.join(name);
157 let Ok(text) = std::fs::read_to_string(&path) else { continue };
158 match lockfile::parse(name, &text, dir) {
159 Some(Ok(mut deps)) => {
160 let rel = self.rel(&path);
161 for d in &mut deps {
162 d.from = if d.from.ends_with("(git)") { format!("{rel} (git)") } else { rel.clone() };
163 }
164 self.deps.extend(deps);
165 self.lockfiles.push(rel);
166 found_here.insert(*eco);
167 }
168 Some(Err(e)) => self.warnings.push(format!("could not parse {}: {e}", self.rel(&path))),
169 None => {}
170 }
171 }
172 if !have.contains(&Eco::PyPI) && !found_here.contains(&Eco::PyPI) {
173 let mut reqs: Vec<PathBuf> = std::fs::read_dir(dir)
174 .map(|rd| {
175 rd.flatten()
176 .map(|e| e.path())
177 .filter(|p| p.file_name().is_some_and(|n| lockfile::is_requirements(&n.to_string_lossy())))
178 .collect()
179 })
180 .unwrap_or_default();
181 reqs.sort();
182 for path in reqs {
183 let Ok(text) = std::fs::read_to_string(&path) else { continue };
184 let rel = self.rel(&path);
185 let deps = lockfile::requirements(&text, &rel);
186 if !deps.is_empty() {
187 self.deps.extend(deps);
188 self.lockfiles.push(rel);
189 found_here.insert(Eco::PyPI);
190 }
191 }
192 }
193 have.extend(found_here);
194 }
195
196 fn fallbacks(&mut self, dir: &Path, have: &mut HashSet<Eco>) {
198 if !have.contains(&Eco::Npm) && dir.join("package.json").is_file() {
199 let direct = lockfile::package_json_direct(dir);
200 let mut n = 0;
201 let mut names: Vec<&String> = direct.iter().collect();
202 names.sort();
203 for name in names {
204 if let Some((_, v)) = locate::npm_installed(name, dir) {
205 self.deps.push(Dep {
206 eco: Eco::Npm,
207 name: name.clone(),
208 version: v,
209 direct: true,
210 from: "node_modules".into(),
211 });
212 n += 1;
213 }
214 }
215 if n > 0 {
216 have.insert(Eco::Npm);
217 self.lockfiles.push("package.json + node_modules".into());
218 }
219 }
220 let is_py = ["pyproject.toml", "setup.py", "setup.cfg"].iter().any(|f| dir.join(f).is_file()) || dir.join(".venv").is_dir();
221 if !have.contains(&Eco::PyPI) && is_py {
222 let direct = lockfile::pyproject_direct(dir);
223 let mut n = 0;
224 for sp in locate::site_packages(dir).into_iter().take(1) {
225 for (name, v, _) in locate::dist_infos(&sp) {
226 let is_direct = direct.is_empty() || direct.contains(&name);
227 self.deps.push(Dep {
228 eco: Eco::PyPI,
229 name,
230 version: v,
231 direct: is_direct,
232 from: "site-packages".into(),
233 });
234 n += 1;
235 }
236 }
237 if n > 0 {
238 have.insert(Eco::PyPI);
239 self.lockfiles.push("site-packages".into());
240 }
241 }
242 }
243
244 pub fn direct(&self) -> impl Iterator<Item = &Dep> {
245 self.deps.iter().filter(|d| d.direct)
246 }
247
248 pub fn find(&self, spec: &Spec) -> Vec<&Dep> {
250 let mut v: Vec<&Dep> = self.deps.iter().filter(|d| spec.name_matches(d)).collect();
251 v.sort_by_key(|d| (!spec.version_matches(&d.version), !d.direct));
252 v
253 }
254
255 pub fn similar(&self, query: &str, n: usize) -> Vec<String> {
257 let q = query.to_ascii_lowercase();
258 let mut v: Vec<String> = self
259 .deps
260 .iter()
261 .filter(|d| {
262 let name = d.name.to_ascii_lowercase();
263 name.contains(&q) || (q.len() >= 3 && q.contains(name.rsplit('/').next().unwrap_or(&name)))
264 })
265 .map(|d| format!("{}:{}@{}", d.eco, d.name, d.version))
266 .collect();
267 v.dedup();
268 v.truncate(n);
269 v
270 }
271}
272
273#[cfg(test)]
274mod tests {
275 use super::*;
276
277 #[test]
278 fn specs() {
279 assert_eq!(
280 Spec::parse("zod"),
281 Spec {
282 eco: None,
283 name: "zod".into(),
284 version: None
285 }
286 );
287 assert_eq!(
288 Spec::parse("npm:@tanstack/react-query@5.1.0"),
289 Spec {
290 eco: Some(Eco::Npm),
291 name: "@tanstack/react-query".into(),
292 version: Some("5.1.0".into())
293 }
294 );
295 assert_eq!(Spec::parse("pydantic==2.9.2").version.as_deref(), Some("2.9.2"));
296 assert_eq!(Spec::parse("github.com/gin-gonic/gin@v1.10.0").version.as_deref(), Some("1.10.0"));
297 let s = Spec::parse("zod@3");
298 assert!(s.version_matches("3.23.8") && !s.version_matches("4.0.1") && !s.version_matches("30.0.0"));
299 let d = Dep {
300 eco: Eco::Go,
301 name: "github.com/go-chi/chi/v5".into(),
302 version: "v5.1.0".into(),
303 direct: true,
304 from: "go.mod".into(),
305 };
306 assert!(Spec::parse("chi").name_matches(&d));
307 let d = Dep {
308 eco: Eco::PyPI,
309 name: "Typing_Extensions".into(),
310 version: "4".into(),
311 direct: true,
312 from: "x".into(),
313 };
314 assert!(Spec::parse("typing-extensions").name_matches(&d));
315 }
316}