feat(cli): now user interaction can be skipped (#1872)

when creating a new project
This commit is contained in:
Andrew Voynov authored and GitHub committed 2024-05-14 09:39:56 -05:00
1 parent 40df088b7d
commit 720899e19e
3 files changed
+104 -75

No files matched your search

+30 -4
View File
@@ -1,26 +1,52 @@
use super::*;
use cargo_generate::{GenerateArgs, TemplatePath};
static DEFAULT_TEMPLATE: &str = "gh:dioxuslabs/dioxus-template";
#[derive(Clone, Debug, Default, Deserialize, Parser)]
#[clap(name = "new")]
pub struct Create {
/// Project name (required when `--yes` is used)
name: Option<String>,
/// Template path
#[clap(default_value = "gh:dioxuslabs/dioxus-template", long)]
#[clap(default_value = DEFAULT_TEMPLATE, short, long)]
template: String,
/// Pass <option>=<value> for the used template (e.g., `foo=bar`)
#[clap(short, long)]
option: Vec<String>,
/// Specify a sub-template within the template repository to be used as the actual template
#[clap(long)]
subtemplate: Option<String>,
/// Skip user interaction by using the default values for the used template.
/// Default values can be overridden with `--option`
#[clap(short, long)]
yes: bool,
// TODO: turn on/off cargo-generate's output (now is invisible)
// #[clap(default_value = "false", short, long)]
// silent: bool,
}
impl Create {
pub fn create(self) -> Result<()> {
let args = GenerateArgs {
let mut args = GenerateArgs {
template_path: TemplatePath {
auto_path: Some(self.template),
subfolder: self.subtemplate,
..Default::default()
},
..Default::default()
};
if self.yes {
if self.name.is_none() {
return Err(
"You have to provide the project's name when using `--yes` option.".into(),
);
}
args.silent = true;
};
args.name = self.name;
args.define = self.option;
let path = cargo_generate::generate(args)?;
post_create(&path)
}
}