feat(cli): init cmd can now skip user interaction (#2412)

Previously only `dx new` had this ability, now `dx init` has it, too.
This commit is contained in:
Andrew Voynov authored and GitHub committed 2024-05-15 12:13:36 -05:00
1 parent 720899e19e
commit 16313894da
2 files changed
+25 -9

No files matched your search

+3 -3
View File
@@ -1,7 +1,7 @@
use super::*; use super::*;
use cargo_generate::{GenerateArgs, TemplatePath}; use cargo_generate::{GenerateArgs, TemplatePath};
static DEFAULT_TEMPLATE: &str = "gh:dioxuslabs/dioxus-template"; pub(crate) static DEFAULT_TEMPLATE: &str = "gh:dioxuslabs/dioxus-template";
#[derive(Clone, Debug, Default, Deserialize, Parser)] #[derive(Clone, Debug, Default, Deserialize, Parser)]
#[clap(name = "new")] #[clap(name = "new")]
@@ -34,6 +34,7 @@ impl Create {
subfolder: self.subtemplate, subfolder: self.subtemplate,
..Default::default() ..Default::default()
}, },
define: self.option,
..Default::default() ..Default::default()
}; };
if self.yes { if self.yes {
@@ -43,9 +44,8 @@ impl Create {
); );
} }
args.silent = true; args.silent = true;
}; }
args.name = self.name; args.name = self.name;
args.define = self.option;
let path = cargo_generate::generate(args)?; let path = cargo_generate::generate(args)?;
post_create(&path) post_create(&path)
} }
+22 -6
View File
@@ -1,33 +1,49 @@
use super::*; use super::*;
use crate::cli::create::DEFAULT_TEMPLATE;
use cargo_generate::{GenerateArgs, TemplatePath}; use cargo_generate::{GenerateArgs, TemplatePath};
#[derive(Clone, Debug, Default, Deserialize, Parser)] #[derive(Clone, Debug, Default, Deserialize, Parser)]
#[clap(name = "init")] #[clap(name = "init")]
pub struct Init { pub struct Init {
/// Template path /// Template path
#[clap(default_value = "gh:dioxuslabs/dioxus-template", long)] #[clap(default_value = DEFAULT_TEMPLATE, short, long)]
template: String, 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 Init { impl Init {
pub fn init(self) -> Result<()> { pub fn init(self) -> Result<()> {
// get dir name // Get directory name.
let name = std::env::current_dir()? let name = std::env::current_dir()?
.file_name() .file_name()
.map(|f| f.to_str().unwrap().to_string()); .map(|f| f.to_str().unwrap().to_string());
let mut args = GenerateArgs {
let args = GenerateArgs {
template_path: TemplatePath { template_path: TemplatePath {
auto_path: Some(self.template), auto_path: Some(self.template),
subfolder: self.subtemplate,
..Default::default() ..Default::default()
}, },
name, name,
init: true, init: true,
define: self.option,
..Default::default() ..Default::default()
}; };
if self.yes {
args.silent = true;
}
let path = cargo_generate::generate(args)?; let path = cargo_generate::generate(args)?;
create::post_create(&path) create::post_create(&path)
} }
} }