Add cargo xtask msrv (#3189)

* added cargo xtask msrv X.Y placeholder function

* add placeholder function cargo xtask msrv (X.Y)

* implement CLI arg vec, graceful errors, formatting

* impl. xtask msrv insert,upsert,graceful failures

* misc fixes - still need to update write method

* interim commit before removing comments

* updated per @djmitche suggestions. 2 fixes todo

* impl unwrap error checks, file truncating, etc

* removed test paths

* updated toml with specific regex version

* updated cargo toml and lock for regex version

* updated cargo toml and lock for regex version

* fixed clippy complaints

* impl replace file content, not comments

* accept djmitche suggestion to change comment documentation

Co-authored-by: Dustin J. Mitchell <dustin@v.igoro.us>

* refactored, need to fix Cow return

* removed debug prints, unncessary if, etc.

* fix non-idiomatic rust, arg.len error handling

* add #MSRV to config files and test

---------

Co-authored-by: iwyatt <sayhello+git@isaacwyatt.com>
Co-authored-by: Dustin J. Mitchell <dustin@v.igoro.us>
This commit is contained in:
Isaac Wyatt 2023-11-11 14:54:38 -08:00 committed by GitHub
parent 1583e56cfc
commit 8f327db5b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 111 additions and 19 deletions

View file

@ -29,9 +29,7 @@ jobs:
- uses: actions-rs/toolchain@v1 - uses: actions-rs/toolchain@v1
with: with:
# Fixed version for clippy lints. Bump this as necesary. It must not toolchain: "1.64" # MSRV
# be older than the MSRV in tests.yml.
toolchain: "1.64"
override: true override: true
- uses: actions-rs/cargo@v1.0.3 - uses: actions-rs/cargo@v1.0.3
@ -100,8 +98,7 @@ jobs:
- uses: actions-rs/toolchain@v1 - uses: actions-rs/toolchain@v1
with: with:
# Same as the MSRV in rust-tests.yml toolchain: "1.64" # MSRV
toolchain: "1.64"
override: true override: true
- uses: actions-rs/cargo@v1.0.3 - uses: actions-rs/cargo@v1.0.3

View file

@ -15,8 +15,7 @@ jobs:
strategy: strategy:
matrix: matrix:
rust: rust:
# MSRV; must not be higher than the clippy rust version in checks.yml - "1.64" # MSRV
- "1.64"
- "stable" - "stable"
os: os:
- ubuntu-latest - ubuntu-latest

1
Cargo.lock generated
View file

@ -2189,6 +2189,7 @@ name = "xtask"
version = "0.4.1" version = "0.4.1"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"regex",
"taskchampion-lib", "taskchampion-lib",
] ]

View file

@ -60,9 +60,9 @@ checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535"
[[package]] [[package]]
name = "byteorder" name = "byteorder"
version = "1.4.3" version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]] [[package]]
name = "cc" name = "cc"

View file

@ -2,8 +2,11 @@
name = "xtask" name = "xtask"
version = "0.4.1" version = "0.4.1"
edition = "2018" edition = "2018"
# rust-version = "1.65" # Used for testing xtask MSRV functionality
[dependencies] [dependencies]
anyhow.workspace = true anyhow.workspace = true
taskchampion-lib = { path = "../lib" } taskchampion-lib = { path = "../lib" }
regex = "^1.5.6"

View file

@ -3,29 +3,121 @@
//! At the moment it is very simple, but if this grows more subcommands then //! At the moment it is very simple, but if this grows more subcommands then
//! it will be sensible to use `clap` or another similar library. //! it will be sensible to use `clap` or another similar library.
use regex::Regex;
use std::env; use std::env;
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::{BufRead, BufReader, Seek, Write};
use std::path::PathBuf; use std::path::{Path, PathBuf};
/// Tuples of the form (PATH, REGEX) where PATH and REGEX are literals where PATH is a file that
/// conains the Minimum Supported Rust Version and REGEX is the pattern to find the appropriate
/// line in the file. PATH is relative to the `taskchampion/` directory in the repo.
const MSRV_PATH_REGEX: &[(&str, &str)] = &[
(
"../.github/workflows/checks.yml",
r#"toolchain: "[0-9.]+*" # MSRV"#,
),
("../.github/workflows/rust-tests.yml", r#""[0-9.]+" # MSRV"#),
(
"taskchampion/src/lib.rs",
r#"Rust version [0-9.]* and higher"#,
),
];
pub fn main() -> anyhow::Result<()> { pub fn main() -> anyhow::Result<()> {
let arg = env::args().nth(1); let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
match arg.as_deref() { let workspace_dir = manifest_dir.parent().unwrap();
Some("codegen") => codegen(), let arguments: Vec<String> = env::args().collect();
Some(arg) => anyhow::bail!("unknown xtask {}", arg),
_ => anyhow::bail!("unknown xtask"), if arguments.len() < 2 {
anyhow::bail!("xtask: Valid arguments are: `codegen`, `msrv <version x.y>`");
}
match arguments[1].as_str() {
"codegen" => codegen(workspace_dir),
"msrv" => msrv(arguments, workspace_dir),
_ => anyhow::bail!("xtask: unknown xtask"),
} }
} }
/// `cargo xtask codegen` /// `cargo xtask codegen`
/// ///
/// This uses ffizz-header to generate `lib/taskchampion.h`. /// This uses ffizz-header to generate `lib/taskchampion.h`.
fn codegen() -> anyhow::Result<()> { fn codegen(workspace_dir: &Path) -> anyhow::Result<()> {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let workspace_dir = manifest_dir.parent().unwrap();
let lib_crate_dir = workspace_dir.join("lib"); let lib_crate_dir = workspace_dir.join("lib");
let mut file = File::create(lib_crate_dir.join("taskchampion.h")).unwrap(); let mut file = File::create(lib_crate_dir.join("taskchampion.h")).unwrap();
write!(&mut file, "{}", ::taskchampion_lib::generate_header()).unwrap(); write!(&mut file, "{}", ::taskchampion_lib::generate_header()).unwrap();
Ok(()) Ok(())
} }
/// `cargo xtask msrv (X.Y)`
///
/// This checks and updates the Minimum Supported Rust Version for all files specified in MSRV_PATH_REGEX`.
/// Each line where the regex matches will have all values of the form `#.##` replaced with the given MSRV.
fn msrv(args: Vec<String>, workspace_dir: &Path) -> anyhow::Result<()> {
// check that (X.Y) argument is (mostly) valid:
if args.len() < 3 || !args[2].chars().all(|c| c.is_numeric() || c == '.') {
anyhow::bail!("xtask: Invalid argument format. Xtask MSRV argument takes the form \"X.Y(y)\", where XYy are numbers. eg: `cargo run xtask MSRV 1.68`");
}
let version_replacement_string = &args[2];
// set regex for replacing version number only within the pattern found within a line
let re_msrv_version = Regex::new(r"([0-9]+(\.|[0-9]+|))+")?;
// for each file in const paths tuple
for msrv_file in MSRV_PATH_REGEX {
let mut is_pattern_in_file = false;
let path = workspace_dir.join(msrv_file.0);
let path = Path::new(&path);
if !path.exists() {
anyhow::bail!("xtask: path does not exist {}", &path.display());
};
let mut file = File::options().read(true).write(true).open(path)?;
let reader = BufReader::new(&file);
// set search string and the replacement string for version number content
let re_msrv_pattern = Regex::new(msrv_file.1)?;
// for each line in file
let mut file_string = String::new();
for line in reader.lines() {
let line = &line?;
// if rust version pattern is found and is different, update it
if let Some(pattern_offset) = re_msrv_pattern.find(line) {
if !pattern_offset.as_str().contains(version_replacement_string) {
file_string += &re_msrv_version.replace(line, version_replacement_string);
file_string += "\n";
is_pattern_in_file = true;
continue;
}
}
file_string += line;
file_string += "\n";
}
// if pattern was found and updated, write to disk
if is_pattern_in_file {
// Set the file length to the file_string length
file.set_len(file_string.len() as u64)?;
// set the cursor to the beginning of the file and write
file.seek(std::io::SeekFrom::Start(0))?;
file.write_all(file_string.as_bytes())?;
// notify user this file was updated
println!(
"xtask: Updated MSRV in {}",
re_msrv_version.replace(msrv_file.0, version_replacement_string)
);
}
}
Ok(())
}