Skip to content

Commit

Permalink
xtask: support MSRV
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Dec 27, 2021
1 parent d600a8c commit f344c23
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
7 changes: 5 additions & 2 deletions 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 }
39 changes: 30 additions & 9 deletions 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,
Expand All @@ -20,7 +20,7 @@ impl Subcommand {
}

fn main() -> Result<()> {
Subcommand::parse().execute()
Subcommand::from_args().execute()
}

/// Runs `cargo llvm-cov` for the PyO3 codebase.
Expand All @@ -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
}

Expand Down Expand Up @@ -92,7 +88,32 @@ fn get_coverage_env() -> Result<HashMap<String, String>> {
}

// 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)
}

0 comments on commit f344c23

Please sign in to comment.