Skip to content

Commit

Permalink
Update clap to v4 (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaka91 authored Dec 21, 2022
1 parent 385521c commit b41a957
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 58 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Subheadings to categorize changes are `added, changed, deprecated, removed, fixe
- Our website (trunkrs.dev) now only updates on new releases.
- Additional attributes are now passed through script tags (fixes #429)
- Updated internal http stack to axum v0.6.0.
- Updated CLI argument parser to clap v0.4.
### fixed
- Nested WS proxies - if `backend=ws://localhost:8000/ws` is set, queries for `ws://localhost:8080/ws/entityX` will be linked with `ws://localhost:8000/ws/entityX`
- Updated all dependencies in both Trunk and its examples, to fix currently open security advisories for old dependencies.
Expand Down
20 changes: 6 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ axum = { version = "0.6", features = ["ws"] }
bytes = "1"
cargo-lock = "8"
cargo_metadata = "0.15"
clap = { version = "3", features = ["derive", "env"] }
clap = { version = "4", features = ["derive", "env"] }
console = "0.15"
directories = "4"
dunce = "1"
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use crate::config::{ConfigOpts, ConfigOptsBuild};

/// Build the Rust WASM app and all of its assets.
#[derive(Clone, Debug, Args)]
#[clap(name = "build")]
#[command(name = "build")]
pub struct Build {
#[clap(flatten)]
#[command(flatten)]
pub build: ConfigOptsBuild,
}

Expand Down
6 changes: 3 additions & 3 deletions src/cmd/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ use crate::tools::cache_dir;

/// Clean output artifacts.
#[derive(Args)]
#[clap(name = "clean")]
#[command(name = "clean")]
pub struct Clean {
#[clap(flatten)]
#[command(flatten)]
pub clean: ConfigOptsClean,
/// Optionally clean any cached tools used by Trunk
///
/// These tools are cached in a platform dependent "projects" dir. Removing them will cause
/// them to be downloaded by Trunk next time they are needed.
#[clap(short, long)]
#[arg(short, long)]
pub tools: bool,
}

Expand Down
4 changes: 2 additions & 2 deletions src/cmd/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use crate::config::ConfigOpts;

/// Trunk config controls.
#[derive(Clone, Debug, Args)]
#[clap(name = "config")]
#[command(name = "config")]
pub struct Config {
#[clap(subcommand)]
#[command(subcommand)]
action: ConfigSubcommands,
}

Expand Down
8 changes: 4 additions & 4 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use crate::serve::ServeSystem;

/// Build, watch & serve the Rust WASM app and all of its assets.
#[derive(Args)]
#[clap(name = "serve")]
#[command(name = "serve")]
pub struct Serve {
#[clap(flatten)]
#[command(flatten)]
pub build: ConfigOptsBuild,
#[clap(flatten)]
#[command(flatten)]
pub watch: ConfigOptsWatch,
#[clap(flatten)]
#[command(flatten)]
pub serve: ConfigOptsServe,
}

Expand Down
6 changes: 3 additions & 3 deletions src/cmd/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use crate::watch::WatchSystem;

/// Build & watch the Rust WASM app and all of its assets.
#[derive(Args)]
#[clap(name = "watch")]
#[command(name = "watch")]
pub struct Watch {
#[clap(flatten)]
#[command(flatten)]
pub build: ConfigOptsBuild,
#[clap(flatten)]
#[command(flatten)]
pub watch: ConfigOptsWatch,
}

Expand Down
5 changes: 3 additions & 2 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Common functionality and types.
use std::convert::Infallible;
use std::ffi::OsStr;
use std::fmt::Debug;
use std::fs::Metadata;
Expand All @@ -22,10 +23,10 @@ static CWD: Lazy<PathBuf> =
Lazy::new(|| std::env::current_dir().expect("error getting current dir"));

/// Ensure the given value for `--public-url` is formatted correctly.
pub fn parse_public_url(val: &str) -> String {
pub fn parse_public_url(val: &str) -> Result<String, Infallible> {
let prefix = if !val.starts_with('/') { "/" } else { "" };
let suffix = if !val.ends_with('/') { "/" } else { "" };
format!("{}{}{}", prefix, val, suffix)
Ok(format!("{}{}{}", prefix, val, suffix))
}

/// A utility function to recursively copy a directory.
Expand Down
45 changes: 22 additions & 23 deletions src/config/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,31 @@ use crate::pipelines::PipelineStage;
#[derive(Clone, Debug, Default, Deserialize, Args)]
pub struct ConfigOptsBuild {
/// The index HTML file to drive the bundling process [default: index.html]
#[clap(parse(from_os_str))]
pub target: Option<PathBuf>,
/// Build in release mode [default: false]
#[clap(long)]
#[arg(long)]
#[serde(default)]
pub release: bool,
/// The output dir for all final assets [default: dist]
#[clap(short, long, parse(from_os_str))]
#[arg(short, long)]
pub dist: Option<PathBuf>,
/// The public URL from which assets are to be served [default: /]
#[clap(long, parse(from_str=parse_public_url))]
#[arg(long, value_parser = parse_public_url)]
pub public_url: Option<String>,
/// Build without default features [default: false]
#[clap(long)]
#[arg(long)]
#[serde(default)]
pub no_default_features: bool,
/// Build with all features [default: false]
#[clap(long)]
#[arg(long)]
#[serde(default)]
pub all_features: bool,
/// A comma-separated list of features to activate, must not be used with all-features
/// [default: ""]
#[clap(long)]
#[arg(long)]
pub features: Option<String>,
/// Whether to include hash values in the output file names [default: true]
#[clap(long)]
#[arg(long)]
pub filehash: Option<bool>,
/// Optional pattern for the app loader script [default: None]
///
Expand All @@ -51,7 +50,7 @@ pub struct ConfigOptsBuild {
/// to key/value pairs provided in `pattern_params`.
///
/// These values can only be provided via config file.
#[clap(skip)]
#[arg(skip)]
#[serde(default)]
pub pattern_script: Option<String>,
/// Optional pattern for the app preload element [default: None]
Expand All @@ -61,10 +60,10 @@ pub struct ConfigOptsBuild {
/// to key/value pairs provided in `pattern_params`.
///
/// These values can only be provided via config file.
#[clap(skip)]
#[arg(skip)]
#[serde(default)]
pub pattern_preload: Option<String>,
#[clap(skip)]
#[arg(skip)]
#[serde(default)]
/// Optional replacement parameters corresponding to the patterns provided in
/// `pattern_script` and `pattern_preload`.
Expand All @@ -86,45 +85,45 @@ pub struct ConfigOptsBuild {
#[derive(Clone, Debug, Default, Deserialize, Args)]
pub struct ConfigOptsWatch {
/// Watch specific file(s) or folder(s) [default: build target parent folder]
#[clap(short, long, parse(from_os_str), value_name = "path")]
#[arg(short, long, value_name = "path")]
pub watch: Option<Vec<PathBuf>>,
/// Paths to ignore [default: []]
#[clap(short, long, parse(from_os_str), value_name = "path")]
#[arg(short, long, value_name = "path")]
pub ignore: Option<Vec<PathBuf>>,
}

/// Config options for the serve system.
#[derive(Clone, Debug, Default, Deserialize, Args)]
pub struct ConfigOptsServe {
/// The address to serve on [default: 127.0.0.1]
#[clap(long)]
#[arg(long)]
pub address: Option<IpAddr>,
/// The port to serve on [default: 8080]
#[clap(long)]
#[arg(long)]
pub port: Option<u16>,
/// Open a browser tab once the initial build is complete [default: false]
#[clap(long)]
#[arg(long)]
#[serde(default)]
pub open: bool,
/// A URL to which requests will be proxied [default: None]
#[clap(long = "proxy-backend")]
#[arg(long = "proxy-backend")]
#[serde(default, deserialize_with = "deserialize_uri")]
pub proxy_backend: Option<Uri>,
/// The URI on which to accept requests which are to be rewritten and proxied to backend
/// [default: None]
#[clap(long = "proxy-rewrite")]
#[arg(long = "proxy-rewrite")]
#[serde(default)]
pub proxy_rewrite: Option<String>,
/// Configure the proxy for handling WebSockets [default: false]
#[clap(long = "proxy-ws")]
#[arg(long = "proxy-ws")]
#[serde(default)]
pub proxy_ws: bool,
/// Configure the proxy to accept insecure requests [default: false]
#[clap(long = "proxy-insecure")]
#[arg(long = "proxy-insecure")]
#[serde(default)]
pub proxy_insecure: bool,
/// Disable auto-reload of the web app [default: false]
#[clap(long = "no-autoreload")]
#[arg(long = "no-autoreload")]
#[serde(default)]
pub no_autoreload: bool,
}
Expand All @@ -133,10 +132,10 @@ pub struct ConfigOptsServe {
#[derive(Clone, Debug, Default, Deserialize, Args)]
pub struct ConfigOptsClean {
/// The output dir for all final assets [default: dist]
#[clap(short, long, parse(from_os_str))]
#[arg(short, long)]
pub dist: Option<PathBuf>,
/// Optionally perform a cargo clean [default: false]
#[clap(long)]
#[arg(long)]
#[serde(default)]
pub cargo: bool,
}
Expand Down
19 changes: 15 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ async fn main() -> Result<()> {

/// Build, bundle & ship your Rust WASM application to the web.
#[derive(Parser)]
#[clap(about, author, version, name = "trunk")]
#[command(about, author, version, name = "trunk")]
struct Trunk {
#[clap(subcommand)]
#[command(subcommand)]
action: TrunkSubcommands,
/// Path to the Trunk config file [default: Trunk.toml]
#[clap(long, parse(from_os_str), env = "TRUNK_CONFIG")]
#[arg(long, env = "TRUNK_CONFIG")]
pub config: Option<PathBuf>,
/// Enable verbose logging.
#[clap(short)]
#[arg(short)]
pub v: bool,
}

Expand Down Expand Up @@ -87,3 +87,14 @@ enum TrunkSubcommands {
/// Trunk config controls.
Config(cmd::config::Config),
}

#[cfg(test)]
mod tests {
use crate::Trunk;

#[test]
fn verify_cli() {
use clap::CommandFactory;
Trunk::command().debug_assert();
}
}

0 comments on commit b41a957

Please sign in to comment.