From 4b4e8ea2a4820a2d1b4eaf1fafb95df4114b2c34 Mon Sep 17 00:00:00 2001 From: Louis Dureuil Date: Tue, 9 Jan 2024 13:24:33 +0100 Subject: [PATCH] Add binary to list features --- Cargo.toml | 1 + xtask/Cargo.toml | 15 +++++++++++++++ xtask/src/main.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 xtask/Cargo.toml create mode 100644 xtask/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index d7b3cca82..bb8d7d787 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ members = [ "json-depth-checker", "benchmarks", "fuzzers", + "xtask", ] [workspace.package] diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml new file mode 100644 index 000000000..af9ecc7b5 --- /dev/null +++ b/xtask/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "xtask" +version.workspace = true +authors.workspace = true +description.workspace = true +homepage.workspace = true +readme.workspace = true +edition.workspace = true +license.workspace = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +cargo_metadata = "0.18.1" +clap = { version = "4.4.14", features = ["derive"] } diff --git a/xtask/src/main.rs b/xtask/src/main.rs new file mode 100644 index 000000000..6570dc67b --- /dev/null +++ b/xtask/src/main.rs @@ -0,0 +1,41 @@ +use std::collections::HashSet; + +use clap::Parser; + +/// List features available in the workspace +#[derive(Parser, Debug)] +struct ListFeaturesDeriveArgs { + /// Feature to exclude from the list. Repeat the argument to exclude multiple features + #[arg(short, long)] + exclude_feature: Vec, +} + +/// Utilitary commands +#[derive(Parser, Debug)] +#[command(author, version, about, long_about)] +#[command(name = "cargo xtask")] +#[command(bin_name = "cargo xtask")] +enum Command { + ListFeatures(ListFeaturesDeriveArgs), +} + +fn main() { + let args = Command::parse(); + match args { + Command::ListFeatures(args) => list_features(args), + } +} + +fn list_features(args: ListFeaturesDeriveArgs) { + let exclude_features: HashSet<_> = args.exclude_feature.into_iter().collect(); + let metadata = cargo_metadata::MetadataCommand::new().no_deps().exec().unwrap(); + let features: Vec = metadata + .packages + .iter() + .flat_map(|package| package.features.keys()) + .filter(|feature| !exclude_features.contains(feature.as_str())) + .map(|s| s.to_owned()) + .collect(); + let features = features.join(" "); + println!("{features}") +}