Skip to content

Commit

Permalink
chore: fixup some regressions from the config refactoring
Browse files Browse the repository at this point in the history
* Add env-var variants for the build command arguments
* For boolean flags, allow non-value variant
* Other minor cleanups

Closes #803
  • Loading branch information
ctron committed Jun 3, 2024
1 parent c3012a7 commit 265599c
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 22 deletions.
5 changes: 5 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Just a few simple items to keep in mind as you hack.

- Pull request early and often. This helps to let others know what you are working on. **Please use GitHub's Draft PR mechanism** if your PR is not yet ready for review.
- Use [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/), a changelog will automatically be created from such commits
- When making changes to the configuration, be sure to regenate the schema. This can be done by running:

```shell
cargo run -- config generate-schema schemas/config.json
```

## linting
We are using clippy & rustfmt. Clippy is SO GREAT! Rustfmt ... has a lot more growing to do; however, we are using it for uniformity.
Expand Down
2 changes: 1 addition & 1 deletion Trunk.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ignore = []

[serve]
# The address to serve on.
address = "127.0.0.1"
addresses = ["127.0.0.1"]
# The port to serve on.
port = 8080
# Open a browser tab once the initial build is complete.
Expand Down
2 changes: 1 addition & 1 deletion schemas/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@
]
},
"insecure": {
"description": "Configure the proxy to accept insecure certificates.",
"description": "Configure the proxy to accept insecure certificates (danger!).",
"default": false,
"type": "boolean"
},
Expand Down
74 changes: 56 additions & 18 deletions src/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,80 +20,97 @@ pub struct Build {
pub target: Option<PathBuf>,

/// Build in release mode
#[arg(long)]
#[arg(long, env = "TRUNK_BUILD_RELEASE")]
#[arg(default_missing_value="true", num_args=0..=1)]
pub release: Option<bool>,

/// The output dir for all final assets
#[arg(short, long)]
#[arg(short, long, env = "TRUNK_BUILD_DIST")]
pub dist: Option<PathBuf>,

/// Run without accessing the network
#[arg(long)]
#[arg(long, env = "TRUNK_BUILD_OFFLINE")]
#[arg(default_missing_value="true", num_args=0..=1)]
pub offline: Option<bool>,

/// Require Cargo.lock and cache are up to date
#[arg(long)]
#[arg(long, env = "TRUNK_BUILD_FROZEN")]
#[arg(default_missing_value="true", num_args=0..=1)]
pub frozen: Option<bool>,

/// Require Cargo.lock is up to date
#[arg(long)]
#[arg(long, env = "TRUNK_BUILD_LOCKED")]
#[arg(default_missing_value="true", num_args=0..=1)]
pub locked: Option<bool>,

/// The public URL from which assets are to be served
#[arg(long)]
#[arg(long, env = "TRUNK_BUILD_PUBLIC_URL")]
pub public_url: Option<BaseUrl>,

/// Don't add a trailing slash to the public URL if it is missing
#[arg(long)]
#[arg(long, env = "TRUNK_BUILD_PUBLIC_URL_NO_TRAILING_SLASH")]
#[arg(default_missing_value="true", num_args=0..=1)]
pub public_url_no_trailing_slash_fix: Option<bool>,

/// Build without default features
#[arg(long)]
#[arg(long, env = "TRUNK_BUILD_NO_DEFAULT_FEATURES")]
#[arg(default_missing_value="true", num_args=0..=1)]
pub no_default_features: Option<bool>,

/// Build with all features
#[arg(long)]
#[arg(long, env = "TRUNK_BUILD_ALL_FEATURES")]
#[arg(default_missing_value="true", num_args=0..=1)]
pub all_features: Option<bool>,

/// A comma-separated list of features to activate, must not be used with all-features
#[arg(long, conflicts_with = "all_features", value_delimiter = ',')]
#[arg(
long,
conflicts_with = "all_features",
value_delimiter = ',',
env = "TRUNK_BUILD_FEATURES"
)]
pub features: Option<Vec<String>>,

/// Whether to include hash values in the output file names
#[arg(long)]
#[arg(long, env = "TRUNK_BUILD_FILEHASH")]
#[arg(default_missing_value="true", num_args=0..=1)]
pub filehash: Option<bool>,

/// When desired, set a custom root certificate chain (same format as Cargo's config.toml http.cainfo)
#[arg(long)]
#[arg(long, env = "TRUNK_BUILD_ROOT_CERTIFICATE")]
pub root_certificate: Option<String>,

/// Allows request to ignore certificate validation errors.
/// Allows request to ignore certificate validation errors (danger!)
///
/// Can be useful when behind a corporate proxy.
#[arg(long)]
#[arg(long, env = "TRUNK_BUILD_ACCEPT_INVALID_CERTS")]
#[arg(default_missing_value="true", num_args=0..=1)]
pub accept_invalid_certs: Option<bool>,

/// Enable minification.
///
/// This overrides the value from the configuration file.
#[arg(short = 'M', long)]
#[arg(short = 'M', long, env = "TRUNK_BUILD_MINIFY")]
#[arg(default_missing_value="true", num_args=0..=1)]
pub minify: Option<bool>,

/// Allows disabling sub-resource integrity (SRI)
#[arg(long)]
#[arg(long, env = "TRUNK_BUILD_NO_SRI")]
#[arg(default_missing_value="true", num_args=0..=1)]
pub no_sri: Option<bool>,

/// Ignore error's related to self-closing script elements, and instead issue a warning.
///
/// Since this issue can cause the HTML output to be truncated, only enable this in case you
/// are sure it is caused due to a false positive.
#[arg(long)]
#[arg(long, env = "TRUNK_BUILD_ALLOW_SELF_CLOSING_SCRIPT")]
#[arg(default_missing_value="true", num_args=0..=1)]
pub allow_self_closing_script: Option<bool>,

// NOTE: flattened structures come last
#[command(flatten)]
pub core: super::core::Core,

// NOTE: flattened structures come last
#[command(flatten)]
pub tools: Tools,
}
Expand Down Expand Up @@ -177,3 +194,24 @@ impl Build {
Ok(())
}
}

#[cfg(test)]
mod test {
use crate::{Trunk, TrunkSubcommands};
use clap::Parser;
use rstest::rstest;

#[rstest]
#[case(&["trunk", "build"], None)]
#[case(&["trunk", "build", "--no-default-features"], Some(true))]
#[case(&["trunk", "build", "--no-default-features", "true"], Some(true))]
#[case(&["trunk", "build", "--no-default-features", "false"], Some(false))]
fn test_bool_no_arg(#[case] input: &[&str], #[case] expected: Option<bool>) {
let cli = Trunk::parse_from(input);
let TrunkSubcommands::Build(build) = cli.action else {
panic!("must be a build command");
};

assert_eq!(build.no_default_features, expected);
}
}
1 change: 1 addition & 0 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Trunk's subcommands

pub mod build;
pub mod clean;
pub mod config;
Expand Down
6 changes: 5 additions & 1 deletion src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ pub struct Serve {
/// Open a browser tab once the initial build is complete [default: false]
#[arg(long, env = "TRUNK_SERVE_OPEN")]
pub open: bool,
/// Disable auto-reload of the web app [default: false]
/// Disable auto-reload of the web app
#[arg(long, env = "TRUNK_SERVE_NO_AUTORELOAD")]
#[arg(default_missing_value="true", num_args=0..=1)]
pub no_autoreload: Option<bool>,
/// Disable error reporting in the browser [default: false]
#[arg(long, env = "TRUNK_SERVE_NO_ERROR_REPORTING")]
#[arg(default_missing_value="true", num_args=0..=1)]
pub no_error_reporting: Option<bool>,
/// Disable fallback to index.html for missing files [default: false]
#[arg(long, env = "TRUNK_SERVE_NO_SPA")]
#[arg(default_missing_value="true", num_args=0..=1)]
pub no_spa: Option<bool>,
/// Protocol used for the auto-reload WebSockets connection [enum: ws, wss]
#[arg(long, env = "TRUNK_SERVE_WS_PROTOCOL")]
Expand All @@ -55,6 +58,7 @@ pub struct Serve {
#[arg(long, env = "TRUNK_SERVE_SERVE_BASE")]
pub serve_base: Option<String>,

// NOTE: flattened structures come last
#[command(flatten)]
pub proxy: ProxyArgs,

Expand Down
1 change: 1 addition & 0 deletions src/cmd/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct Watch {
#[arg(long, env = "TRUNK_WATCH_ENABLE_COOLDOWN")]
pub enable_cooldown: bool,

// NOTE: flattened structures come last
#[command(flatten)]
pub build: super::build::Build,
}
Expand Down
2 changes: 1 addition & 1 deletion src/config/models/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct Proxy {
/// Configure the proxy for handling WebSockets.
#[serde(default)]
pub ws: bool,
/// Configure the proxy to accept insecure certificates.
/// Configure the proxy to accept insecure certificates (danger!).
#[serde(default)]
pub insecure: bool,
/// Configure the proxy to bypass the system proxy.
Expand Down

0 comments on commit 265599c

Please sign in to comment.