use crate::config::Config;
use crate::git;
use crate::ui;
use std::path::Path;
pub fn run(config: Config, project: &str) {
ui::heading(&format!("Setting up {} development environment", project));
ui::heading("── Step 1: Clone missing repos ──");
let crates_token = std::env::var("CRATES_GIT_TOKEN").ok();
let main_token = std::env::var("MAIN_GIT_TOKEN").ok();
let _ = git::clone(
&config.main_repo.url,
&config.main_repo.dir,
main_token.as_deref(),
);
if let Some(ref upstream) = config.main_repo.upstream {
if git::is_git_repo(&config.main_repo.dir) {
let _ = git::fetch_upstream(&config.main_repo.dir, upstream);
}
}
let token = crates_token.as_deref();
for repo in &config.repos {
let _ = git::clone(&repo.url, &repo.abs_dir, token);
}
ui::heading("── Step 2: Verify path dependencies ──");
let mut all_ok = true;
for repo in &config.repos {
if !repo.abs_dir.join("Cargo.toml").is_file() {
continue; }
let missing = check_path_deps(&repo.abs_dir, &config.crates_dir);
if missing.is_empty() {
ui::info(&format!("{} — all deps resolved", repo.name));
} else {
ui::warn(&format!(
"{} — missing path deps: {}",
repo.name,
missing.join(", ")
));
all_ok = false;
}
}
ui::heading("── Step 3: Pull latest ──");
let _ = git::pull(&config.main_repo.dir);
for repo in &config.repos {
let _ = git::pull(&repo.abs_dir);
}
ui::separator();
if all_ok {
ui::success(&format!(
"✓ {} ready — {}/{} crates available",
project,
count_cloned(&config),
config.repos.len()
));
} else {
ui::warn("Some path dependencies are missing. Run 'pets setup <project>' again or 'pets clone <project>' to fix.");
}
}
fn check_path_deps(crate_dir: &Path, _crates_dir: &Path) -> Vec<String> {
let mut missing = Vec::new();
let cargo_toml = crate_dir.join("Cargo.toml");
if !cargo_toml.is_file() {
return missing;
}
let content = match std::fs::read_to_string(&cargo_toml) {
Ok(c) => c,
Err(_) => return missing,
};
for line in content.lines() {
let trimmed = line.trim();
let Some(path_pos) = trimmed.find("path =") else {
continue;
};
let rest = &trimmed[path_pos + "path =".len()..];
let Some(start) = rest.find('"') else {
continue;
};
let after = &rest[start + 1..];
let Some(end) = after.find('"') else {
continue;
};
let dep_path = &after[..end];
if !dep_path.starts_with("../") {
continue;
}
let abs_dep = crate_dir.join(dep_path);
if !abs_dep.join("Cargo.toml").is_file() {
if let Some(name) = abs_dep.file_name() {
missing.push(name.to_string_lossy().to_string());
}
}
}
missing
}
fn count_cloned(config: &Config) -> usize {
config
.repos
.iter()
.filter(|r| r.abs_dir.join("Cargo.toml").is_file() && git::is_git_repo(&r.abs_dir))
.count()
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
fn temp_dir(tag: &str) -> PathBuf {
let mut dir = std::env::temp_dir();
dir.push(format!(
"pets-setup-test-{}-{}-{}",
tag,
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
std::fs::create_dir_all(&dir).unwrap();
dir
}
#[test]
fn check_path_deps_finds_missing() {
let root = temp_dir("deps");
let crate_dir = root.join("muskitty-x");
std::fs::create_dir_all(&crate_dir).unwrap();
std::fs::write(
crate_dir.join("Cargo.toml"),
"[dependencies]\nfoo = { path = \"../foo\" }\nbar = { path = \"../bar\" }\n\n[lib]\npath = \"src/lib.rs\"\n",
)
.unwrap();
std::fs::create_dir_all(root.join("foo")).unwrap();
std::fs::write(root.join("foo").join("Cargo.toml"), "[package]\n").unwrap();
let missing = check_path_deps(&crate_dir, &root);
assert_eq!(missing, vec!["bar".to_string()]);
let _ = std::fs::remove_dir_all(&root);
}
}