use std::{fs, path::Path};
use super::WizardConfig;
pub fn generate(name: &str, root: &Path, _config: &WizardConfig) -> anyhow::Result<()> {
let repo_url = "https://github.com/ateeq1999/rok-api-starter.git";
if root.exists() {
fs::remove_dir_all(root)?;
}
println!(" Cloning template from {repo_url}...");
let status = std::process::Command::new("git")
.args(["clone", repo_url, name])
.status()
.map_err(|e| anyhow::anyhow!("Failed to run git: {e}"))?;
if !status.success() {
anyhow::bail!("git clone failed. Is git installed and accessible?");
}
let git_dir = root.join(".git");
if git_dir.exists() {
fs::remove_dir_all(&git_dir)?;
}
configure_project(name, root)?;
println!(" ✓ API starter cloned and configured");
Ok(())
}
fn configure_project(name: &str, root: &Path) -> anyhow::Result<()> {
let pkg_name = root.file_name().and_then(|n| n.to_str()).unwrap_or(name);
let snake_name = pkg_name.replace('-', "_");
for filename in &[
"Cargo.toml",
"README.md",
"Dockerfile",
"docker-compose.yml",
] {
let path = root.join(filename);
if !path.exists() {
continue;
}
let content = fs::read_to_string(&path)?;
let updated = content
.replace("rok-api-test", pkg_name)
.replace("rok_api_test", &snake_name);
if content != updated {
fs::write(&path, updated)?;
}
println!(" wrote {filename}");
}
Ok(())
}