Skip to content

Commit 0b20bd9

Browse files
committed
chore(xtask): Add core cargo flags
1 parent a675e04 commit 0b20bd9

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-1
lines changed

Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/xtask/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ edition = "2021"
55
publish = false
66

77
[dependencies]
8+
anyhow = "1.0.47"
9+
cargo = { version = "0.71.0", path = "../.." }
10+
clap = "4.2.0"
11+
env_logger = "0.10.0"

crates/xtask/src/main.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
mod xtask;
2+
13
fn main() {
2-
println!("Hello, world!");
4+
env_logger::init_from_env("CARGO_LOG");
5+
let cli = xtask::cli();
6+
let matches = cli.get_matches();
7+
8+
let mut config = cargo::util::config::Config::default().unwrap_or_else(|e| {
9+
let mut eval = cargo::core::shell::Shell::new();
10+
cargo::exit_with_error(e.into(), &mut eval)
11+
});
12+
if let Err(e) = xtask::exec(&matches, &mut config) {
13+
cargo::exit_with_error(e, &mut config.shell())
14+
}
315
}

crates/xtask/src/xtask.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use cargo::util::command_prelude::*;
2+
3+
pub fn cli() -> clap::Command {
4+
clap::Command::new("xtask")
5+
.arg(
6+
opt(
7+
"verbose",
8+
"Use verbose output (-vv very verbose/build.rs output)",
9+
)
10+
.short('v')
11+
.action(ArgAction::Count)
12+
.global(true),
13+
)
14+
.arg_quiet()
15+
.arg(
16+
opt("color", "Coloring: auto, always, never")
17+
.value_name("WHEN")
18+
.global(true),
19+
)
20+
.arg(flag("frozen", "Require Cargo.lock and cache are up to date").global(true))
21+
.arg(flag("locked", "Require Cargo.lock is up to date").global(true))
22+
.arg(flag("offline", "Run without accessing the network").global(true))
23+
.arg(multi_opt("config", "KEY=VALUE", "Override a configuration value").global(true))
24+
.arg(
25+
Arg::new("unstable-features")
26+
.help("Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details")
27+
.short('Z')
28+
.value_name("FLAG")
29+
.action(ArgAction::Append)
30+
.global(true),
31+
)
32+
}
33+
34+
pub fn exec(args: &clap::ArgMatches, config: &mut cargo::util::Config) -> cargo::CliResult {
35+
config_configure(config, args)?;
36+
37+
Ok(())
38+
}
39+
40+
fn config_configure(config: &mut Config, args: &ArgMatches) -> CliResult {
41+
let verbose = args.verbose();
42+
// quiet is unusual because it is redefined in some subcommands in order
43+
// to provide custom help text.
44+
let quiet = args.flag("quiet");
45+
let color = args.get_one::<String>("color").map(String::as_str);
46+
let frozen = args.flag("frozen");
47+
let locked = args.flag("locked");
48+
let offline = args.flag("offline");
49+
let mut unstable_flags = vec![];
50+
if let Some(values) = args.get_many::<String>("unstable-features") {
51+
unstable_flags.extend(values.cloned());
52+
}
53+
let mut config_args = vec![];
54+
if let Some(values) = args.get_many::<String>("config") {
55+
config_args.extend(values.cloned());
56+
}
57+
config.configure(
58+
verbose,
59+
quiet,
60+
color,
61+
frozen,
62+
locked,
63+
offline,
64+
&None,
65+
&unstable_flags,
66+
&config_args,
67+
)?;
68+
Ok(())
69+
}

0 commit comments

Comments
 (0)