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

when creating a new project
This commit is contained in:
Andrew Voynov 2024-05-14 17:39:56 +03:00 committed by GitHub
parent 40df088b7d
commit 720899e19e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 104 additions and 75 deletions

143
Cargo.lock generated
View file

@ -1023,15 +1023,6 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "btoi"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9dd6407f73a9b8b6162d8a2ef999fe6afd7cc15902ebf42c5cd296addf17e0ad"
dependencies = [
"num-traits",
]
[[package]] [[package]]
name = "built" name = "built"
version = "0.7.2" version = "0.7.2"
@ -1147,20 +1138,20 @@ dependencies = [
[[package]] [[package]]
name = "cargo-generate" name = "cargo-generate"
version = "0.19.0" version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92c1b6f44358912a9538fa3b6ac8d3aa3f585444f9dc32f12ed85d1545a9df9f" checksum = "aa7015584550945f11fdfb7af113d30e2727468ec281c1d7f28cc1019196c25d"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"auth-git2", "auth-git2",
"clap 4.4.18", "clap 4.5.4",
"console", "console",
"dialoguer", "dialoguer",
"env_logger 0.10.2", "env_logger 0.11.3",
"fs-err", "fs-err",
"git2", "git2",
"gix-config", "gix-config",
"heck 0.4.1", "heck 0.5.0",
"home", "home",
"ignore", "ignore",
"indexmap 2.2.6", "indexmap 2.2.6",
@ -1376,9 +1367,9 @@ dependencies = [
[[package]] [[package]]
name = "clap" name = "clap"
version = "4.4.18" version = "4.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e578d6ec4194633722ccf9544794b71b1385c3c027efe0c55db226fc880865c" checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0"
dependencies = [ dependencies = [
"clap_builder", "clap_builder",
"clap_derive", "clap_derive",
@ -1386,23 +1377,23 @@ dependencies = [
[[package]] [[package]]
name = "clap_builder" name = "clap_builder"
version = "4.4.18" version = "4.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4df4df40ec50c46000231c914968278b1eb05098cf8f1b3a518a95030e71d1c7" checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4"
dependencies = [ dependencies = [
"anstream", "anstream",
"anstyle", "anstyle",
"clap_lex", "clap_lex",
"strsim", "strsim 0.11.1",
] ]
[[package]] [[package]]
name = "clap_derive" name = "clap_derive"
version = "4.4.7" version = "4.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64"
dependencies = [ dependencies = [
"heck 0.4.1", "heck 0.5.0",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.60", "syn 2.0.60",
@ -1410,9 +1401,9 @@ dependencies = [
[[package]] [[package]]
name = "clap_lex" name = "clap_lex"
version = "0.6.0" version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce"
[[package]] [[package]]
name = "cocoa" name = "cocoa"
@ -1741,7 +1732,7 @@ dependencies = [
"anes", "anes",
"cast", "cast",
"ciborium", "ciborium",
"clap 4.4.18", "clap 4.5.4",
"criterion-plot 0.5.0", "criterion-plot 0.5.0",
"futures", "futures",
"is-terminal", "is-terminal",
@ -1968,7 +1959,7 @@ dependencies = [
"ident_case", "ident_case",
"proc-macro2", "proc-macro2",
"quote", "quote",
"strsim", "strsim 0.10.0",
"syn 2.0.60", "syn 2.0.60",
] ]
@ -2154,7 +2145,7 @@ dependencies = [
"cargo_metadata 0.18.1", "cargo_metadata 0.18.1",
"cargo_toml 0.18.0", "cargo_toml 0.18.0",
"chrono", "chrono",
"clap 4.4.18", "clap 4.5.4",
"colored 2.1.0", "colored 2.1.0",
"ctrlc", "ctrlc",
"dioxus-autofmt", "dioxus-autofmt",
@ -2215,7 +2206,7 @@ name = "dioxus-cli-config"
version = "0.5.2" version = "0.5.2"
dependencies = [ dependencies = [
"cargo_toml 0.18.0", "cargo_toml 0.18.0",
"clap 4.4.18", "clap 4.5.4",
"once_cell", "once_cell",
"serde", "serde",
"serde_json", "serde_json",
@ -3109,9 +3100,6 @@ name = "faster-hex"
version = "0.9.0" version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2a2b11eda1d40935b26cf18f6833c526845ae8c41e58d09af6adeb6f0269183" checksum = "a2a2b11eda1d40935b26cf18f6833c526845ae8c41e58d09af6adeb6f0269183"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "fastrand" name = "fastrand"
@ -3682,23 +3670,23 @@ dependencies = [
[[package]] [[package]]
name = "gix-actor" name = "gix-actor"
version = "0.28.1" version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2eadca029ef716b4378f7afb19f7ee101fde9e58ba1f1445971315ac866db417" checksum = "45c3a3bde455ad2ee8ba8a195745241ce0b770a8a26faae59fcf409d01b28c46"
dependencies = [ dependencies = [
"bstr 1.9.1", "bstr 1.9.1",
"btoi",
"gix-date", "gix-date",
"gix-utils",
"itoa 1.0.11", "itoa 1.0.11",
"thiserror", "thiserror",
"winnow 0.5.40", "winnow 0.6.7",
] ]
[[package]] [[package]]
name = "gix-config" name = "gix-config"
version = "0.32.1" version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0341471d55d8676e98b88e121d7065dfa4c9c5acea4b6d6ecdd2846e85cce0c3" checksum = "7580e05996e893347ad04e1eaceb92e1c0e6a3ffe517171af99bf6b6df0ca6e5"
dependencies = [ dependencies = [
"bstr 1.9.1", "bstr 1.9.1",
"gix-config-value", "gix-config-value",
@ -3712,7 +3700,7 @@ dependencies = [
"smallvec", "smallvec",
"thiserror", "thiserror",
"unicode-bom", "unicode-bom",
"winnow 0.5.40", "winnow 0.6.7",
] ]
[[package]] [[package]]
@ -3742,12 +3730,13 @@ dependencies = [
[[package]] [[package]]
name = "gix-features" name = "gix-features"
version = "0.36.1" version = "0.38.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d46a4a5c6bb5bebec9c0d18b65ada20e6517dbd7cf855b87dd4bbdce3a771b2" checksum = "db4254037d20a247a0367aa79333750146a369719f0c6617fec4f5752cc62b37"
dependencies = [ dependencies = [
"gix-hash", "gix-hash",
"gix-trace", "gix-trace",
"gix-utils",
"libc", "libc",
"prodash", "prodash",
"sha1_smol", "sha1_smol",
@ -3756,18 +3745,19 @@ dependencies = [
[[package]] [[package]]
name = "gix-fs" name = "gix-fs"
version = "0.8.1" version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20e86eb040f5776a5ade092282e51cdcad398adb77d948b88d17583c2ae4e107" checksum = "e2184c40e7910529677831c8b481acf788ffd92427ed21fad65b6aa637e631b8"
dependencies = [ dependencies = [
"gix-features", "gix-features",
"gix-utils",
] ]
[[package]] [[package]]
name = "gix-glob" name = "gix-glob"
version = "0.14.1" version = "0.16.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5db19298c5eeea2961e5b3bf190767a2d1f09b8802aeb5f258e42276350aff19" checksum = "682bdc43cb3c00dbedfcc366de2a849b582efd8d886215dbad2ea662ec156bb5"
dependencies = [ dependencies = [
"bitflags 2.5.0", "bitflags 2.5.0",
"bstr 1.9.1", "bstr 1.9.1",
@ -3777,9 +3767,9 @@ dependencies = [
[[package]] [[package]]
name = "gix-hash" name = "gix-hash"
version = "0.13.3" version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f8cf8c2266f63e582b7eb206799b63aa5fa68ee510ad349f637dfe2d0653de0" checksum = "f93d7df7366121b5018f947a04d37f034717e113dcf9ccd85c34b58e57a74d5e"
dependencies = [ dependencies = [
"faster-hex", "faster-hex",
"thiserror", "thiserror",
@ -3787,9 +3777,9 @@ dependencies = [
[[package]] [[package]]
name = "gix-lock" name = "gix-lock"
version = "11.0.1" version = "13.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e5c65e6a29830a435664891ced3f3c1af010f14900226019590ee0971a22f37" checksum = "e7c359f81f01b8352063319bcb39789b7ea0887b406406381106e38c4a34d049"
dependencies = [ dependencies = [
"gix-tempfile", "gix-tempfile",
"gix-utils", "gix-utils",
@ -3798,21 +3788,21 @@ dependencies = [
[[package]] [[package]]
name = "gix-object" name = "gix-object"
version = "0.39.0" version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "febf79c5825720c1c63fe974c7bbe695d0cb54aabad73f45671c60ce0e501e33" checksum = "3d4f8efae72030df1c4a81d02dbe2348e748d9b9a11e108ed6efbd846326e051"
dependencies = [ dependencies = [
"bstr 1.9.1", "bstr 1.9.1",
"btoi",
"gix-actor", "gix-actor",
"gix-date", "gix-date",
"gix-features", "gix-features",
"gix-hash", "gix-hash",
"gix-utils",
"gix-validate", "gix-validate",
"itoa 1.0.11", "itoa 1.0.11",
"smallvec", "smallvec",
"thiserror", "thiserror",
"winnow 0.5.40", "winnow 0.6.7",
] ]
[[package]] [[package]]
@ -3830,9 +3820,9 @@ dependencies = [
[[package]] [[package]]
name = "gix-ref" name = "gix-ref"
version = "0.39.1" version = "0.43.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b2069adc212cf7f3317ef55f6444abd06c50f28479dbbac5a86acf3b05cbbfe" checksum = "fd4aba68b925101cb45d6df328979af0681364579db889098a0de75b36c77b65"
dependencies = [ dependencies = [
"gix-actor", "gix-actor",
"gix-date", "gix-date",
@ -3843,10 +3833,11 @@ dependencies = [
"gix-object", "gix-object",
"gix-path", "gix-path",
"gix-tempfile", "gix-tempfile",
"gix-utils",
"gix-validate", "gix-validate",
"memmap2", "memmap2",
"thiserror", "thiserror",
"winnow 0.5.40", "winnow 0.6.7",
] ]
[[package]] [[package]]
@ -3863,9 +3854,9 @@ dependencies = [
[[package]] [[package]]
name = "gix-tempfile" name = "gix-tempfile"
version = "11.0.1" version = "13.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "388dd29114a86ec69b28d1e26d6d63a662300ecf61ab3f4cc578f7d7dc9e7e23" checksum = "a761d76594f4443b675e85928e4902dec333273836bd386906f01e7e346a0d11"
dependencies = [ dependencies = [
"gix-fs", "gix-fs",
"libc", "libc",
@ -7048,9 +7039,9 @@ dependencies = [
[[package]] [[package]]
name = "prodash" name = "prodash"
version = "26.2.2" version = "28.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "794b5bf8e2d19b53dcdcec3e4bba628e20f5b6062503ba89281fa7037dd7bbcf" checksum = "744a264d26b88a6a7e37cbad97953fa233b94d585236310bcbc88474b4092d79"
[[package]] [[package]]
name = "profiling" name = "profiling"
@ -7505,9 +7496,9 @@ dependencies = [
[[package]] [[package]]
name = "rhai" name = "rhai"
version = "1.16.3" version = "1.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3625f343d89990133d013e39c46e350915178cf94f1bec9f49b0cbef98a3e3c" checksum = "f6273372244d04a8a4b0bec080ea1e710403e88c5d9d83f9808b2bfa64f0982a"
dependencies = [ dependencies = [
"ahash 0.8.11", "ahash 0.8.11",
"bitflags 2.5.0", "bitflags 2.5.0",
@ -7517,13 +7508,14 @@ dependencies = [
"rhai_codegen", "rhai_codegen",
"smallvec", "smallvec",
"smartstring", "smartstring",
"thin-vec",
] ]
[[package]] [[package]]
name = "rhai_codegen" name = "rhai_codegen"
version = "1.6.0" version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "853977598f084a492323fe2f7896b4100a86284ee8473612de60021ea341310f" checksum = "59aecf17969c04b9c0c5d21f6bc9da9fec9dd4980e64d1871443a476589d8c86"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -8675,6 +8667,12 @@ version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]] [[package]]
name = "subprocess" name = "subprocess"
version = "0.2.9" version = "0.2.9"
@ -8880,7 +8878,7 @@ dependencies = [
"serde_json", "serde_json",
"sha1", "sha1",
"sha2", "sha2",
"strsim", "strsim 0.10.0",
"tar", "tar",
"tauri-icns", "tauri-icns",
"tauri-utils", "tauri-utils",
@ -8934,15 +8932,14 @@ dependencies = [
[[package]] [[package]]
name = "tempfile" name = "tempfile"
version = "3.8.1" version = "3.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ef1adac450ad7f4b3c28589471ade84f25f731a7a0fe30d71dfa9f60fd808e5" checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"fastrand", "fastrand",
"redox_syscall 0.4.1",
"rustix", "rustix",
"windows-sys 0.48.0", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -8990,6 +8987,12 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8eaa81235c7058867fa8c0e7314f33dcce9c215f535d1913822a2b3f5e289f3c" checksum = "8eaa81235c7058867fa8c0e7314f33dcce9c215f535d1913822a2b3f5e289f3c"
[[package]]
name = "thin-vec"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a38c90d48152c236a3ab59271da4f4ae63d678c5d7ad6b7714d7cb9760be5e4b"
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.59" version = "1.0.59"
@ -9696,9 +9699,9 @@ checksum = "65dd7eed29412da847b0f78bcec0ac98588165988a8cfe41d4ea1d429f8ccfff"
[[package]] [[package]]
name = "walkdir" name = "walkdir"
version = "2.4.0" version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee" checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
dependencies = [ dependencies = [
"same-file", "same-file",
"winapi-util", "winapi-util",

View file

@ -73,7 +73,7 @@ mlua = { version = "0.8.1", features = [
], optional = true } ], optional = true }
ctrlc = "3.2.3" ctrlc = "3.2.3"
open = "5.0.1" open = "5.0.1"
cargo-generate = "0.19.0" cargo-generate = "0.20.0"
toml_edit = "0.21.0" toml_edit = "0.21.0"
# bundling # bundling

View file

@ -1,26 +1,52 @@
use super::*; use super::*;
use cargo_generate::{GenerateArgs, TemplatePath}; use cargo_generate::{GenerateArgs, TemplatePath};
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")]
pub struct Create { pub struct Create {
/// Project name (required when `--yes` is used)
name: Option<String>,
/// 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 Create { impl Create {
pub fn create(self) -> Result<()> { pub fn create(self) -> Result<()> {
let args = GenerateArgs { let mut 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()
}, },
..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)?; let path = cargo_generate::generate(args)?;
post_create(&path) post_create(&path)
} }
} }