Skip to content

Commit

Permalink
cli: new: support multiple internal templates
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspar030 committed Jan 3, 2024
1 parent bf2d9b0 commit f5fc4f0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
37 changes: 27 additions & 10 deletions src/cli.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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')
Expand All @@ -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")
Expand Down Expand Up @@ -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(
Expand Down
19 changes: 14 additions & 5 deletions src/new.rs
Expand Up @@ -30,6 +30,14 @@ impl PathEmpty for camino::Utf8Path {

pub fn from_matches(matches: &ArgMatches) -> Result<(), Error> {
let path = matches.get_one::<Utf8PathBuf>("path").unwrap();
let template_name = matches.get_one::<String>("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()? {
Expand All @@ -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(),
};
Expand Down Expand Up @@ -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(())
}

0 comments on commit f5fc4f0

Please sign in to comment.