feat: matcha new --template-url via shallow git clone
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
1 parent
c382602ca7
commit
8d31faa27e
2 files changed
+51
-5
No files matched your search
+36
-2
@@ -15,9 +15,43 @@ pub fn run(name: &str, template_url: Option<&str>) -> std::io::Result<()> {
|
||||
std::fs::create_dir_all(dest)?;
|
||||
template::write_starter(dest, name)?;
|
||||
}
|
||||
Some(_) => return Err(Error::new(ErrorKind::Unsupported,
|
||||
"--template-url not implemented yet")),
|
||||
Some(url) => clone_template(url, dest)?,
|
||||
}
|
||||
println!("✓ created {name}\n cd {name} && matcha run");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn resolve_url(spec: &str) -> String {
|
||||
if let Some(rest) = spec.strip_prefix("gh:") {
|
||||
format!("https://github.com/{rest}")
|
||||
} else {
|
||||
spec.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
fn clone_template(url: &str, dest: &Path) -> std::io::Result<()> {
|
||||
let status = std::process::Command::new("git")
|
||||
.args(["clone", "--depth", "1", &resolve_url(url)])
|
||||
.arg(dest)
|
||||
.status()?;
|
||||
if !status.success() {
|
||||
return Err(Error::other("git clone failed"));
|
||||
}
|
||||
std::fs::remove_dir_all(dest.join(".git")).ok();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn gh_shorthand_expands() {
|
||||
assert_eq!(resolve_url("gh:green-tea/tmpl"),
|
||||
"https://github.com/green-tea/tmpl");
|
||||
}
|
||||
#[test]
|
||||
fn full_url_passes_through() {
|
||||
assert_eq!(resolve_url("https://example.com/x.git"),
|
||||
"https://example.com/x.git");
|
||||
}
|
||||
}
|
||||
+15
-3
@@ -1,4 +1,5 @@
|
||||
use assert_cmd::Command;
|
||||
use std::process::Command as Proc;
|
||||
use tempfile::tempdir;
|
||||
|
||||
#[test]
|
||||
@@ -22,10 +23,21 @@ fn new_refuses_existing_dir() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_with_template_url_stub_leaves_no_dir() {
|
||||
fn new_clones_local_template() {
|
||||
let d = tempdir().unwrap();
|
||||
// build a bare-ish source repo to clone
|
||||
let src = d.path().join("src-tmpl");
|
||||
std::fs::create_dir_all(src.join("public")).unwrap();
|
||||
std::fs::write(src.join("public/marker.txt"), "hi").unwrap();
|
||||
Proc::new("git").args(["init", "-q"]).current_dir(&src).status().unwrap();
|
||||
Proc::new("git").args(["add", "."]).current_dir(&src).status().unwrap();
|
||||
Proc::new("git").args(["-c","user.email=t@t","-c","user.name=t",
|
||||
"commit","-qm","init"]).current_dir(&src).status().unwrap();
|
||||
|
||||
let url = format!("file://{}", src.display());
|
||||
Command::cargo_bin("matcha").unwrap()
|
||||
.current_dir(d.path())
|
||||
.args(["new", "demo", "--template-url", "gh:x/y"]).assert().failure();
|
||||
assert!(!d.path().join("demo").exists());
|
||||
.args(["new", "cloned", "--template-url", &url]).assert().success();
|
||||
assert!(d.path().join("cloned/public/marker.txt").exists());
|
||||
assert!(!d.path().join("cloned/.git").exists()); // .git stripped
|
||||
}
|
||||
Reference in new issue
Block a user