Skip to content

Commit

Permalink
chore(xtask): Add core cargo flags
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Apr 25, 2023
1 parent a675e04 commit 0b20bd9
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions crates/xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ edition = "2021"
publish = false

[dependencies]
anyhow = "1.0.47"
cargo = { version = "0.71.0", path = "../.." }
clap = "4.2.0"
env_logger = "0.10.0"
14 changes: 13 additions & 1 deletion crates/xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
mod xtask;

fn main() {
println!("Hello, world!");
env_logger::init_from_env("CARGO_LOG");
let cli = xtask::cli();
let matches = cli.get_matches();

let mut config = cargo::util::config::Config::default().unwrap_or_else(|e| {
let mut eval = cargo::core::shell::Shell::new();
cargo::exit_with_error(e.into(), &mut eval)
});
if let Err(e) = xtask::exec(&matches, &mut config) {
cargo::exit_with_error(e, &mut config.shell())
}
}
69 changes: 69 additions & 0 deletions crates/xtask/src/xtask.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use cargo::util::command_prelude::*;

pub fn cli() -> clap::Command {
clap::Command::new("xtask")
.arg(
opt(
"verbose",
"Use verbose output (-vv very verbose/build.rs output)",
)
.short('v')
.action(ArgAction::Count)
.global(true),
)
.arg_quiet()
.arg(
opt("color", "Coloring: auto, always, never")
.value_name("WHEN")
.global(true),
)
.arg(flag("frozen", "Require Cargo.lock and cache are up to date").global(true))
.arg(flag("locked", "Require Cargo.lock is up to date").global(true))
.arg(flag("offline", "Run without accessing the network").global(true))
.arg(multi_opt("config", "KEY=VALUE", "Override a configuration value").global(true))
.arg(
Arg::new("unstable-features")
.help("Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details")
.short('Z')
.value_name("FLAG")
.action(ArgAction::Append)
.global(true),
)
}

pub fn exec(args: &clap::ArgMatches, config: &mut cargo::util::Config) -> cargo::CliResult {
config_configure(config, args)?;

Ok(())
}

fn config_configure(config: &mut Config, args: &ArgMatches) -> CliResult {
let verbose = args.verbose();
// quiet is unusual because it is redefined in some subcommands in order
// to provide custom help text.
let quiet = args.flag("quiet");
let color = args.get_one::<String>("color").map(String::as_str);
let frozen = args.flag("frozen");
let locked = args.flag("locked");
let offline = args.flag("offline");
let mut unstable_flags = vec![];
if let Some(values) = args.get_many::<String>("unstable-features") {
unstable_flags.extend(values.cloned());
}
let mut config_args = vec![];
if let Some(values) = args.get_many::<String>("config") {
config_args.extend(values.cloned());
}
config.configure(
verbose,
quiet,
color,
frozen,
locked,
offline,
&None,
&unstable_flags,
&config_args,
)?;
Ok(())
}

0 comments on commit 0b20bd9

Please sign in to comment.