From f5fc4f00a64c5e1e833437b3cab9187182a4ea62 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Wed, 3 Jan 2024 19:13:18 +0100 Subject: [PATCH] cli: new: support multiple internal templates --- src/cli.rs | 37 +++++++++++++++++++++++++++---------- src/new.rs | 19 ++++++++++++++----- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 78a0e0f..0aae852 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -56,15 +56,6 @@ pub fn clap() -> clap::Command { .value_delimiter(',') } - fn verbose() -> Arg { - Arg::new("verbose") - .help("be verbose (e.g., show command lines)") - .short('v') - .long("verbose") - .global(true) - .action(ArgAction::Count) - } - fn partition() -> Arg { use std::str::FromStr; use task_partitioner::PartitionerBuilder; @@ -93,6 +84,23 @@ pub fn clap() -> clap::Command { .value_hint(ValueHint::DirPath) .num_args(1), ) + .arg( + Arg::new("verbose") + .help("be verbose (e.g., show command lines)") + .short('v') + .long("verbose") + .global(true) + .action(ArgAction::Count), + ) + .arg( + Arg::new("quiet") + .help("do not print laze log messages") + .short('q') + .long("quiet") + .global(true) + .action(ArgAction::Count) + .hide(true), // (not really supported, yet) + ) .arg( Arg::new("global") .short('g') @@ -102,7 +110,6 @@ pub fn clap() -> clap::Command { .env("LAZE_GLOBAL") .action(ArgAction::SetTrue), ) - .arg(verbose()) .subcommand( Command::new("build") .about("generate build files and build") @@ -174,6 +181,16 @@ pub fn clap() -> clap::Command { .value_parser(value_parser!(Utf8PathBuf)) .value_hint(ValueHint::DirPath) .required(true), + ) + .arg( + Arg::new("template") + .short('t') + .long("template") + .help("template to use for new project") + .required(false) + .default_value("default") + .value_parser(clap::value_parser!(String)) + .num_args(1), ), ) .subcommand( diff --git a/src/new.rs b/src/new.rs index ce28edb..f1de4c5 100644 --- a/src/new.rs +++ b/src/new.rs @@ -30,6 +30,14 @@ impl PathEmpty for camino::Utf8Path { pub fn from_matches(matches: &ArgMatches) -> Result<(), Error> { let path = matches.get_one::("path").unwrap(); + let template_name = matches.get_one::("template").unwrap(); + let _verbose = matches.get_count("verbose"); + let quiet = matches.get_count("quiet"); + + let prefix = format!("{template_name}/"); + if !TemplateFiles::iter().any(|x| x.starts_with(&prefix)) { + bail!("no internal template with name \"{template_name}\" available"); + } if path.exists() { if !path.is_empty()? { @@ -43,10 +51,6 @@ pub fn from_matches(matches: &ArgMatches) -> Result<(), Error> { .canonicalize_utf8() .with_context(|| format!("canonicalizing {path}"))?; - let template_name = "default"; - - let prefix = format!("{template_name}/"); - let context = Context { project_name: path.file_name().unwrap().to_string(), }; @@ -74,11 +78,16 @@ pub fn from_matches(matches: &ArgMatches) -> Result<(), Error> { .with_context(|| format!("rendering \"{in_filename}\""))?; file_data = Cow::from(rendered.as_bytes()); - std::fs::write(&outfile, file_data).with_context(|| format!("creating {filename}"))?; + std::fs::write(&filename, file_data).with_context(|| format!("creating {filename}"))?; } else { std::fs::write(&filename, embedded_file.data) .with_context(|| format!("creating {filename}"))?; }; } + + if quiet == 0 { + println!("laze: created '{}' project", context.project_name); + } + Ok(()) }