Allow xtasks to have more dependencies (#4268)

This commit is contained in:
Connor Fitzgerald 2023-10-23 15:18:28 -04:00 committed by GitHub
parent 92d237fd93
commit dd84765e38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 95 additions and 127 deletions

View File

@ -1,5 +1,5 @@
[alias] [alias]
xtask = "run --manifest-path xtask/Cargo.toml --" xtask = "run --manifest-path xtask/Cargo.toml"
[build] [build]
rustflags = [ rustflags = [

94
xtask/Cargo.lock generated
View File

@ -2,15 +2,6 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "aho-corasick"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43f6cb1bf222025340178f382c426f13757b2960e89779dfcb319c32542a5a41"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.71" version = "1.0.71"
@ -74,11 +65,7 @@ version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85cdab6a89accf66733ad5a1693a4dcced6aeff64602b634530dd73c1f3ee9f0" checksum = "85cdab6a89accf66733ad5a1693a4dcced6aeff64602b634530dd73c1f3ee9f0"
dependencies = [ dependencies = [
"humantime",
"is-terminal",
"log", "log",
"regex",
"termcolor",
] ]
[[package]] [[package]]
@ -126,12 +113,6 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]] [[package]]
name = "id-arena" name = "id-arena"
version = "2.2.1" version = "2.2.1"
@ -158,18 +139,6 @@ dependencies = [
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]
[[package]]
name = "is-terminal"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adcf93614601c8129ddf72e2d5633df827ba6551541c6d8c59520a371475be1f"
dependencies = [
"hermit-abi",
"io-lifetimes",
"rustix",
"windows-sys 0.48.0",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.6" version = "1.0.6"
@ -200,12 +169,6 @@ version = "0.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "518ef76f2f87365916b142844c16d8fefd85039bc5699050210a7778ee1cd1de" checksum = "518ef76f2f87365916b142844c16d8fefd85039bc5699050210a7778ee1cd1de"
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]] [[package]]
name = "pico-args" name = "pico-args"
version = "0.5.0" version = "0.5.0"
@ -239,23 +202,6 @@ dependencies = [
"bitflags", "bitflags",
] ]
[[package]]
name = "regex"
version = "1.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0ab3ca65655bb1e41f2a8c8cd662eb4fb035e67c3f78da1d61dffe89d07300f"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78"
[[package]] [[package]]
name = "rustc-demangle" name = "rustc-demangle"
version = "0.1.23" version = "0.1.23"
@ -329,15 +275,6 @@ dependencies = [
"windows-sys 0.45.0", "windows-sys 0.45.0",
] ]
[[package]]
name = "termcolor"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6"
dependencies = [
"winapi-util",
]
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.9" version = "1.0.9"
@ -463,37 +400,6 @@ version = "0.77.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fe3d5405e9ea6c1317a656d6e0820912d8b7b3607823a7596117c8f666daf6f" checksum = "5fe3d5405e9ea6c1317a656d6e0820912d8b7b3607823a7596117c8f666daf6f"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]] [[package]]
name = "windows-sys" name = "windows-sys"
version = "0.45.0" version = "0.45.0"

View File

@ -4,14 +4,25 @@ version = "0.1.0"
edition = "2021" edition = "2021"
publish = false publish = false
[features]
run-wasm = ["cargo-run-wasm"]
[dependencies] [dependencies]
# The dependencies in this config have no transitive dependencies.
anyhow = "1.0.71" anyhow = "1.0.71"
# Current contents filed as a PR here: env_logger = { version = "0.10.0", default-features = false }
# <https://github.com/rukai/cargo-run-wasm/pull/37>
cargo-run-wasm = { version = "0.3.2", git = "https://github.com/ErichDonGubler/cargo-run-wasm", branch = "expose-args" }
env_logger = "0.10.0"
log = "0.4.18" log = "0.4.18"
pico-args = { version = "0.5.0", features = ["eq-separator", "short-space-opt", "combined-flags"] } pico-args = { version = "0.5.0", features = [
"eq-separator",
"short-space-opt",
"combined-flags",
] }
xshell = "0.2.3" xshell = "0.2.3"
# Feature: run-wasm
# Current contents filed as a PR here:
# <https://github.com/rukai/cargo-run-wasm/pull/37>
cargo-run-wasm = { version = "0.3.2", git = "https://github.com/ErichDonGubler/cargo-run-wasm", branch = "expose-args", optional = true }
[workspace] [workspace]

View File

@ -15,14 +15,14 @@ Options:
-h, --help Print help -h, --help Print help
"; ";
pub(crate) struct Args { pub struct Args {
pub(crate) subcommand: Subcommand, pub subcommand: Subcommand,
pub command_args: Arguments,
} }
impl Args { impl Args {
pub fn parse() -> Self { pub fn parse() -> Self {
let mut args = Arguments::from_env(); let mut args = Arguments::from_env();
log::debug!("parsing args: {args:?}");
if args.contains("--help") { if args.contains("--help") {
eprint!("{HELP}"); eprint!("{HELP}");
// Emulate Cargo exit status: // Emulate Cargo exit status:
@ -30,8 +30,11 @@ impl Args {
let cargo_like_exit_code = 101; let cargo_like_exit_code = 101;
exit(cargo_like_exit_code); exit(cargo_like_exit_code);
} }
match Subcommand::parse(args).map(|subcommand| Self { subcommand }) { match Subcommand::parse(&mut args) {
Ok(this) => this, Ok(subcommand) => Self {
subcommand,
command_args: args,
},
Err(e) => { Err(e) => {
eprintln!("{:?}", anyhow!(e)); eprintln!("{:?}", anyhow!(e));
exit(1) exit(1)
@ -40,20 +43,47 @@ impl Args {
} }
} }
pub(crate) enum Subcommand { pub enum Subcommand {
RunWasm { args: Arguments }, RunWasm,
Test { args: Arguments }, Test,
} }
impl Subcommand { impl Subcommand {
fn parse(mut args: Arguments) -> anyhow::Result<Subcommand> { /// Returns the name of the subcommand as a string.
///
/// Opposite of [`Self::parse`].
pub fn to_str(&self) -> &'static str {
match self {
Self::RunWasm => "run-wasm",
Self::Test => "test",
}
}
/// Returns true if all required features are enabled for this subcommand.
pub fn required_features_enabled(&self) -> bool {
match self {
Self::RunWasm => cfg!(feature = "run-wasm"),
Self::Test => true,
}
}
/// Comma separated list of features required by this subcommand.
pub fn features(&self) -> &'static str {
match self {
Self::RunWasm => "run-wasm",
// We will never ask for the features if required_features_enabled always returns true.
Self::Test => unreachable!(),
}
}
fn parse(args: &mut Arguments) -> anyhow::Result<Subcommand> {
let subcmd = args let subcmd = args
.subcommand() .subcommand()
.context("failed to parse subcommand")? .context("failed to parse subcommand")?
.context("no subcommand specified; see `--help` for more details")?; .context("no subcommand specified; see `--help` for more details")?;
match &*subcmd { match &*subcmd {
"run-wasm" => Ok(Self::RunWasm { args }), "run-wasm" => Ok(Self::RunWasm),
"test" => Ok(Self::Test { args }), "test" => Ok(Self::Test),
other => { other => {
bail!("unrecognized subcommand {other:?}; see `--help` for more details") bail!("unrecognized subcommand {other:?}; see `--help` for more details")
} }

View File

@ -1,13 +1,12 @@
use std::process::ExitCode; use std::process::Command;
use anyhow::Context; use anyhow::Context;
use cli::{Args, Subcommand}; use cli::Args;
use pico_args::Arguments;
mod cli; mod cli;
mod test; mod test;
fn main() -> ExitCode { fn main() -> anyhow::Result<()> {
env_logger::builder() env_logger::builder()
.filter_level(log::LevelFilter::Info) .filter_level(log::LevelFilter::Info)
.parse_default_env() .parse_default_env()
@ -16,27 +15,48 @@ fn main() -> ExitCode {
let args = Args::parse(); let args = Args::parse();
match run(args) { if !args.subcommand.required_features_enabled() {
Ok(()) => ExitCode::SUCCESS, let features = args.subcommand.features();
Err(e) => { log::info!(
log::error!("{e:?}"); "Required features \"{features}\" are not enabled, recursing with features enabled"
ExitCode::FAILURE );
let subcommand_args = args.command_args.finish();
let iter = subcommand_args
.iter()
.map(|os| os.as_os_str().to_str().unwrap());
let status = Command::new("cargo")
.args(["xtask", "--features", features, args.subcommand.to_str()])
.args(iter)
.status()
.context("Failed to execute recursive cargo xtask")?;
if status.success() {
return Ok(());
} else {
return Err(anyhow::anyhow!("subcommand failed"));
} }
} }
run(args)
} }
fn run(args: Args) -> anyhow::Result<()> { #[allow(unused_mut, unreachable_patterns)]
let Args { subcommand } = args; fn run(mut args: Args) -> anyhow::Result<()> {
match subcommand { match args.subcommand {
Subcommand::RunWasm { mut args } => { #[cfg(feature = "run-wasm")]
cli::Subcommand::RunWasm => {
log::info!("Running wasm example");
// Use top-level Cargo.toml instead of xtask/Cargo.toml by default // Use top-level Cargo.toml instead of xtask/Cargo.toml by default
let manifest_path = args let manifest_path = args
.command_args
.value_from_str("--manifest-path") .value_from_str("--manifest-path")
.unwrap_or_else(|_| "../Cargo.toml".to_string()); .unwrap_or_else(|_| "../Cargo.toml".to_string());
let mut arg_vec = args.finish(); let mut arg_vec = args.command_args.finish();
arg_vec.push("--manifest-path".into()); arg_vec.push("--manifest-path".into());
arg_vec.push(manifest_path.into()); arg_vec.push(manifest_path.into());
let args = Arguments::from_vec(arg_vec); let args = pico_args::Arguments::from_vec(arg_vec);
cargo_run_wasm::run_wasm_with_css_and_args( cargo_run_wasm::run_wasm_with_css_and_args(
"body { margin: 0px; }", "body { margin: 0px; }",
@ -46,6 +66,7 @@ fn run(args: Args) -> anyhow::Result<()> {
); );
Ok(()) Ok(())
} }
Subcommand::Test { args } => test::run_tests(args), cli::Subcommand::Test => test::run_tests(args.command_args),
_ => unreachable!(),
} }
} }