use std::{fs, path::Path};
#[derive(Debug, Default, PartialEq)]
pub struct Guess {
pub stack: Vec<String>,
pub commands: Vec<String>,
}
fn has(root: &Path, name: &str) -> bool {
root.join(name).is_file()
}
pub fn guess(root: &Path) -> Guess {
let mut found = Guess::default();
let mut add = |stack: &str, commands: &[&str]| {
found.stack.push(stack.to_owned());
found
.commands
.extend(commands.iter().map(|c| (*c).to_owned()));
};
if has(root, "Cargo.toml") {
add(
"Rust (Cargo)",
&["cargo build", "cargo test", "cargo clippy --all-targets"],
);
}
if let Ok(text) = fs::read_to_string(root.join("package.json")) {
let manager = if has(root, "pnpm-lock.yaml") {
"pnpm"
} else if has(root, "yarn.lock") {
"yarn"
} else {
"npm"
};
let scripts = serde_json::from_str::<serde_json::Value>(&text)
.ok()
.and_then(|json| json.get("scripts").cloned());
let mut commands = vec![format!("{manager} install")];
for name in ["build", "test", "lint"] {
if scripts.as_ref().is_some_and(|s| s.get(name).is_some()) {
commands.push(format!("{manager} run {name}"));
}
}
let refs: Vec<&str> = commands.iter().map(String::as_str).collect();
add(&format!("Node.js ({manager})"), &refs);
}
if has(root, "pyproject.toml") || has(root, "requirements.txt") {
let pytest = ["pyproject.toml", "requirements.txt"]
.iter()
.any(|f| fs::read_to_string(root.join(f)).is_ok_and(|t| t.contains("pytest")));
add(
"Python 3",
&[if pytest {
"python3 -m pytest"
} else {
"python3 -m unittest discover"
}],
);
}
if has(root, "go.mod") {
add("Go", &["go build ./...", "go test ./..."]);
}
found
}
pub fn fill(template: &str, guess: &Guess) -> String {
if guess.stack.is_empty() {
return template.to_owned();
}
let mut out = String::new();
for line in template.lines() {
out.push_str(line);
out.push('\n');
if line.starts_with("<!-- Languages") {
out.push_str("(Detected by `agengo init` from the files here; check and edit.)\n");
for item in &guess.stack {
out.push_str(&format!("- {item}\n"));
}
} else if line.starts_with("<!-- How to install") {
out.push_str("(Detected by `agengo init`; check and edit.)\n");
for item in &guess.commands {
out.push_str(&format!("- `{item}`\n"));
}
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn detects_node_scripts_and_package_manager() {
let dir = tempfile::tempdir().unwrap();
fs::write(
dir.path().join("package.json"),
r#"{"scripts":{"test":"jest","build":"tsc"}}"#,
)
.unwrap();
fs::write(dir.path().join("pnpm-lock.yaml"), "").unwrap();
let found = guess(dir.path());
assert_eq!(found.stack, ["Node.js (pnpm)"]);
assert_eq!(
found.commands,
["pnpm install", "pnpm run build", "pnpm run test"]
);
}
#[test]
fn empty_folder_gives_no_guess_and_leaves_the_template() {
let dir = tempfile::tempdir().unwrap();
let found = guess(dir.path());
assert_eq!(found, Guess::default());
assert_eq!(fill("## Stack\n", &found), "## Stack\n");
}
#[test]
fn fill_adds_marked_lines_after_the_guidance() {
let guess = Guess {
stack: vec!["Go".into()],
commands: vec!["go test ./...".into()],
};
let text = fill(
"## Stack\n\n<!-- Languages -->\n\n## Commands\n\n<!-- How to install -->\n",
&guess,
);
assert!(text.contains("<!-- Languages -->\n(Detected"));
assert!(text.contains("- Go\n"));
assert!(text.contains("- `go test ./...`\n"));
}
}