|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +//! Subcommand: cargo xtask check-features |
| 6 | +
|
| 7 | +use anyhow::{bail, Context, Result}; |
| 8 | +use clap::Parser; |
| 9 | +use std::process::Command; |
| 10 | + |
| 11 | +/// The default version of `cargo-hack` to install. |
| 12 | +/// We use a patch-floating version to avoid breaking the build when a new |
| 13 | +/// version is released (locally). |
| 14 | +const FLOAT_VERSION: &str = "~0.6.28"; |
| 15 | + |
| 16 | +#[derive(Parser)] |
| 17 | +pub struct Args { |
| 18 | + /// Features to exclude from the check. |
| 19 | + #[clap(long)] |
| 20 | + exclude_features: Option<Vec<String>>, |
| 21 | + /// Depth of the feature powerset to check. |
| 22 | + #[clap(long)] |
| 23 | + depth: Option<usize>, |
| 24 | + /// Error format passed to `cargo hack check`. |
| 25 | + #[clap(long, value_name = "FMT")] |
| 26 | + message_format: Option<String>, |
| 27 | + /// Do not install `cargo-hack` before running the check. |
| 28 | + #[clap(long, default_value_t = false)] |
| 29 | + no_install: bool, |
| 30 | + /// Version of `cargo-hack` to install. |
| 31 | + #[clap(long)] |
| 32 | + version: Option<String>, |
| 33 | +} |
| 34 | + |
| 35 | +/// Run `cargo hack check`. |
| 36 | +pub fn run_cmd(args: Args) -> Result<()> { |
| 37 | + if !args.no_install { |
| 38 | + install_cargo_hack(args.version).unwrap(); |
| 39 | + } |
| 40 | + |
| 41 | + let cargo = |
| 42 | + std::env::var("CARGO").unwrap_or_else(|_| String::from("cargo")); |
| 43 | + let mut command = Command::new(&cargo); |
| 44 | + |
| 45 | + command.args(&["hack", "check"]); |
| 46 | + |
| 47 | + if let Some(features) = args.exclude_features { |
| 48 | + let ex = format!("--exclude-features={}", features.join(",")); |
| 49 | + command.arg(ex); |
| 50 | + } |
| 51 | + |
| 52 | + if let Some(depth) = args.depth { |
| 53 | + let depth = format!("depth={}", depth); |
| 54 | + command.arg(depth); |
| 55 | + } |
| 56 | + |
| 57 | + // Pass along the `--message-format` flag if it was provided. |
| 58 | + if let Some(fmt) = args.message_format { |
| 59 | + command.args(["--message-format", &fmt]); |
| 60 | + } |
| 61 | + |
| 62 | + command |
| 63 | + // Make sure we check everything. |
| 64 | + .arg("--workspace") |
| 65 | + .arg("--bins") |
| 66 | + // We want to check the feature powerset. |
| 67 | + .arg("--feature-powerset") |
| 68 | + .arg("--no-dev-deps") |
| 69 | + .arg("--exclude-no-default-features"); |
| 70 | + |
| 71 | + eprintln!( |
| 72 | + "running: {:?} {}", |
| 73 | + &cargo, |
| 74 | + command |
| 75 | + .get_args() |
| 76 | + .map(|arg| format!("{:?}", arg.to_str().unwrap())) |
| 77 | + .collect::<Vec<_>>() |
| 78 | + .join(" ") |
| 79 | + ); |
| 80 | + |
| 81 | + let exit_status = command |
| 82 | + .spawn() |
| 83 | + .context("failed to spawn child process")? |
| 84 | + .wait() |
| 85 | + .context("failed to wait for child process")?; |
| 86 | + |
| 87 | + if !exit_status.success() { |
| 88 | + bail!("check-features failed: {}", exit_status); |
| 89 | + } |
| 90 | + |
| 91 | + Ok(()) |
| 92 | +} |
| 93 | + |
| 94 | +/// Install `cargo-hack` at the specified version or the default version. |
| 95 | +fn install_cargo_hack(version: Option<String>) -> Result<()> { |
| 96 | + let cargo = |
| 97 | + std::env::var("CARGO").unwrap_or_else(|_| String::from("cargo")); |
| 98 | + |
| 99 | + let mut command = Command::new(&cargo); |
| 100 | + |
| 101 | + if let Some(version) = version { |
| 102 | + command.args(&["install", "cargo-hack", "--version", &version]); |
| 103 | + } else { |
| 104 | + command.args(&[ |
| 105 | + "install", |
| 106 | + "cargo-hack", |
| 107 | + "--locked", |
| 108 | + "--version", |
| 109 | + FLOAT_VERSION, |
| 110 | + ]); |
| 111 | + } |
| 112 | + |
| 113 | + eprintln!( |
| 114 | + "running: {:?} {}", |
| 115 | + &cargo, |
| 116 | + command |
| 117 | + .get_args() |
| 118 | + .map(|arg| format!("{:?}", arg.to_str().unwrap())) |
| 119 | + .collect::<Vec<_>>() |
| 120 | + .join(" ") |
| 121 | + ); |
| 122 | + |
| 123 | + let exit_status = command |
| 124 | + .spawn() |
| 125 | + .expect("failed to spawn child process") |
| 126 | + .wait() |
| 127 | + .expect("failed to wait for child process"); |
| 128 | + |
| 129 | + if !exit_status.success() { |
| 130 | + bail!("cargo-hack install failed: {}", exit_status); |
| 131 | + } |
| 132 | + |
| 133 | + Ok(()) |
| 134 | +} |
0 commit comments