From f344c2392885741907619bd96ce0ecc75be43234 Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Sun, 26 Dec 2021 23:29:26 +0000 Subject: [PATCH] xtask: support MSRV --- xtask/Cargo.toml | 7 +++++-- xtask/src/main.rs | 39 ++++++++++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index f147d00064c..f600841250c 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -1,8 +1,11 @@ [package] name = "xtask" version = "0.1.0" -edition = "2021" +edition = "2018" [dependencies] anyhow = "1.0.51" -clap = { version = "3.0.0-rc.7", features = ["derive"] } + +# Clap 3 requires MSRV 1.54 +rustversion = "1.0" +structopt = { version = "0.3", default-features = false } diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 56630c5551b..12814b14fb3 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -1,8 +1,8 @@ use anyhow::{Context, Result}; -use clap::Parser; use std::{collections::HashMap, process::Command}; +use structopt::StructOpt; -#[derive(Parser)] +#[derive(StructOpt)] enum Subcommand { /// Runs `cargo llvm-cov` for the PyO3 codebase. Coverage, @@ -20,7 +20,7 @@ impl Subcommand { } fn main() -> Result<()> { - Subcommand::parse().execute() + Subcommand::from_args().execute() } /// Runs `cargo llvm-cov` for the PyO3 codebase. @@ -42,18 +42,14 @@ fn subcommand_coverage() -> Result<()> { } fn run(command: &mut Command) -> Result<()> { - print!("running: {}", command.get_program().to_string_lossy()); - for arg in command.get_args() { - print!(" {}", arg.to_string_lossy()); - } - println!(); + println!("running: {}", format_command(command)); command.spawn()?.wait()?; Ok(()) } fn llvm_cov_command(args: &[&str]) -> Command { let mut command = Command::new("cargo"); - command.args(["llvm-cov", "--package=pyo3"]).args(args); + command.args(&["llvm-cov", "--package=pyo3"]).args(args); command } @@ -92,7 +88,32 @@ fn get_coverage_env() -> Result> { } // Replacement for str.split_once() on Rust older than 1.52 +#[rustversion::before(1.52)] fn split_once(s: &str, pat: char) -> Option<(&str, &str)> { let mut iter = s.splitn(2, pat); Some((iter.next()?, iter.next()?)) } + +#[rustversion::since(1.52)] +fn split_once(s: &str, pat: char) -> Option<(&str, &str)> { + s.split_once(pat) +} + +#[rustversion::since(1.57)] +fn format_command(command: &Command) -> String { + let mut buf = String::new(); + buf.push('`'); + buf.push_str(&command.get_program().to_string_lossy()); + for arg in command.get_args() { + buf.push(' '); + buf.push_str(&arg.to_string_lossy()); + } + buf.push('`'); + buf +} + +#[rustversion::before(1.57)] +fn format_command(command: &Command) -> String { + // Debug impl isn't as nice as the above, but will do on < 1.57 + format!("{:?}", command) +}