From 8106989f369edc6054e94fe94aab1beea41c306a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E5=90=91=E5=A4=9C?= <46275354+fu050409@users.noreply.github.com> Date: Tue, 2 Jul 2024 02:29:36 -0400 Subject: [PATCH] refactor(create-farm): rewrite `create-farm` with `clap` (#1493) * refactor(create-farm): rewrite create-farm cli - Use `clap` instead of `pico-args` to parse args - Use `clap` to implement cli helper - Fix all warnings of dead codes - Fix `--manager` takes no effect - Update dependencies and remove `pico-args` Related Issue: #1277 * chore(changeset): add changeset --- .changeset/serious-bears-grin.md | 5 + Cargo.lock | 132 +++++++++++++++++-- crates/create-farm-rs/Cargo.toml | 4 +- crates/create-farm-rs/src/args.rs | 46 ++----- crates/create-farm-rs/src/lib.rs | 17 ++- crates/create-farm-rs/src/package_manager.rs | 7 +- crates/create-farm-rs/src/template.rs | 9 +- 7 files changed, 163 insertions(+), 57 deletions(-) create mode 100644 .changeset/serious-bears-grin.md diff --git a/.changeset/serious-bears-grin.md b/.changeset/serious-bears-grin.md new file mode 100644 index 000000000..9526ba1d4 --- /dev/null +++ b/.changeset/serious-bears-grin.md @@ -0,0 +1,5 @@ +--- +"create-farm": patch +--- + +Refactor create-farm with clap diff --git a/Cargo.lock b/Cargo.lock index 4d5320b85..5b69099c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -132,6 +132,55 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" + +[[package]] +name = "anstyle-parse" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + [[package]] name = "any_ascii" version = "0.1.7" @@ -562,6 +611,46 @@ dependencies = [ "windows-targets 0.52.0", ] +[[package]] +name = "clap" +version = "4.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84b3edb18336f4df585bc9aa31dd99c036dfa5dc5e9a2939a722a188f3a8970d" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1c09dd5ada6c6c78075d6fd0da3f90d8080651e2d6cc8eb2f1aaa4034ced708" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bac35c6dafb060fd4d275d9a4ffae97917c13a6327903a8be2153cd964f7085" +dependencies = [ + "heck 0.5.0", + "proc-macro2", + "quote", + "syn 2.0.60", +] + +[[package]] +name = "clap_lex" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b82cf0babdbd58558212896d1a4272303a57bdb245c2bf1147185fb45640e70" + [[package]] name = "clipboard-win" version = "4.5.0" @@ -619,6 +708,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" +[[package]] +name = "colorchoice" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" + [[package]] name = "combine" version = "4.6.6" @@ -849,11 +944,11 @@ name = "create-farm" version = "0.1.0" dependencies = [ "anyhow", + "clap", "dialoguer", "napi", "napi-build", "napi-derive", - "pico-args", "rust-embed", ] @@ -1066,13 +1161,14 @@ dependencies = [ [[package]] name = "dialoguer" -version = "0.10.4" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59c6f2989294b9a498d3ad5491a79c6deb604617378e1cdc4bfc1c1361fe2f87" +checksum = "658bce805d770f407bc62102fca7c2c64ceef2fbcb2b8bd19d2765ce093980de" dependencies = [ "console", "shell-words", "tempfile", + "thiserror", "zeroize", ] @@ -2209,6 +2305,12 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hermit-abi" version = "0.1.19" @@ -2472,6 +2574,12 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "616cde7c720bb2bb5824a224687d8f77bfd38922027f01d825cd7453be5099fb" +[[package]] +name = "is_terminal_polyfill" +version = "1.70.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" + [[package]] name = "itertools" version = "0.10.5" @@ -3425,12 +3533,6 @@ dependencies = [ "siphasher", ] -[[package]] -name = "pico-args" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315" - [[package]] name = "pin-project" version = "1.1.0" @@ -4570,6 +4672,12 @@ dependencies = [ "syn 2.0.60", ] +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "subtle" version = "2.4.1" @@ -6131,6 +6239,12 @@ version = "2.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8db7427f936968176eaa7cdf81b7f98b980b18495ec28f1b5791ac3bfe3eea9" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "uuid" version = "1.3.3" diff --git a/crates/create-farm-rs/Cargo.toml b/crates/create-farm-rs/Cargo.toml index 9ae0ba1a9..893789761 100644 --- a/crates/create-farm-rs/Cargo.toml +++ b/crates/create-farm-rs/Cargo.toml @@ -15,8 +15,7 @@ path = "src/main.rs" [dependencies] anyhow = "1" -pico-args = "0.5" -dialoguer = "0.10" +dialoguer = "0.11" # Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix napi = { version = "2.15.2", default-features = false, features = [ "napi4", @@ -24,6 +23,7 @@ napi = { version = "2.15.2", default-features = false, features = [ ] } napi-derive = "2.15.2" rust-embed = { version = "8.3", features = [ "compression", "interpolate-folder-path" ] } +clap = { version = "4.5.8", features = ["derive"] } [build-dependencies] napi-build = "2.0.1" diff --git a/crates/create-farm-rs/src/args.rs b/crates/create-farm-rs/src/args.rs index 35f8f756c..cc1a22e9c 100644 --- a/crates/create-farm-rs/src/args.rs +++ b/crates/create-farm-rs/src/args.rs @@ -1,15 +1,22 @@ -use crate::{package_manager::PackageManager, template::Template, utils::colors::*}; -use std::ffi::OsString; -use pico_args::Arguments; - -#[derive(Debug)] +use crate::{package_manager::PackageManager, template::Template}; +use clap::Parser; + +#[derive(Parser, Debug)] +#[command( + name = "create-farm", + about, + long_about = None, + version, +)] pub struct Args { + #[arg(help = "Project name")] pub project_name: Option, + #[arg(short, long, help = "Package manager to use")] pub manager: Option, + #[arg(short, long, help = "Project template to use")] pub template: Option