get latest x version from parsing cargo command

This commit is contained in:
DebugSteven 2023-01-02 16:36:29 -07:00
parent 9aebb1e099
commit e9ca6636e1
3 changed files with 48 additions and 18 deletions

View File

@ -4814,9 +4814,9 @@ dependencies = [
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.147" version = "1.0.152"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d193d69bae983fc11a79df82342761dfbf28a99fc8d203dca4c3c1b590948965" checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
@ -4833,9 +4833,9 @@ dependencies = [
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.147" version = "1.0.152"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f1d362ca8fc9c3e3a7484440752472d68a6caa98f1ab81d99b5dfe517cec852" checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -4853,9 +4853,9 @@ dependencies = [
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.85" version = "1.0.91"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44" checksum = "877c235533714907a8c2464236f5c4b2a17262ef1bd71f38f35ea592c8da6883"
dependencies = [ dependencies = [
"indexmap", "indexmap",
"itoa", "itoa",
@ -5133,9 +5133,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.102" version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fcd952facd492f9be3ef0d0b7032a6e442ee9b361d4acc2b1d0c4aaa5f613a1" checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -5310,6 +5310,7 @@ dependencies = [
"miropt-test-tools", "miropt-test-tools",
"regex", "regex",
"semver", "semver",
"serde_json",
"termcolor", "termcolor",
"walkdir", "walkdir",
] ]

View File

@ -12,6 +12,7 @@ lazy_static = "1"
walkdir = "2" walkdir = "2"
ignore = "0.4.18" ignore = "0.4.18"
semver = "1.0.14" semver = "1.0.14"
serde_json = "1.0.91"
termcolor = "1.1.3" termcolor = "1.1.3"
[[bin]] [[bin]]

View File

@ -1,4 +1,5 @@
use semver::{BuildMetadata, Prerelease, Version}; use semver::Version;
use serde_json::Value;
use std::io::ErrorKind; use std::io::ErrorKind;
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
@ -33,20 +34,47 @@ pub fn check(bad: &mut bool) {
if output.status.success() { if output.status.success() {
let version = String::from_utf8_lossy(&output.stdout); let version = String::from_utf8_lossy(&output.stdout);
let version = Version::parse(version.trim_end()).unwrap(); let version = Version::parse(version.trim_end()).unwrap();
let expected = Version {
major: 0, if let Some(expected) = get_x_wrapper_version() {
minor: 1, if version < expected {
patch: 0, return tidy_error!(
pre: Prerelease::new("").unwrap(), bad,
build: BuildMetadata::EMPTY, "Current version of x is {version}, but the latest version is {expected}\nConsider updating to the newer version of x by running `cargo install --path src/tools/x`"
}; );
if version < expected { }
} else {
return tidy_error!( return tidy_error!(
bad, bad,
"Current version of x is {version}, but the latest version is {expected}\nConsider updating to the newer version of x by running `cargo install --path src/tools/x`" "Unable to parse the latest version of `x` at `src/tools/x/Cargo.toml`"
); );
} }
} else { } else {
return tidy_error!(bad, "failed to check version of `x`: {}", output.status); return tidy_error!(bad, "failed to check version of `x`: {}", output.status);
} }
} }
// Parse latest version out of `x` Cargo.toml
fn get_x_wrapper_version() -> Option<Version> {
let cmd = Command::new("cargo")
.arg("metadata")
.args(["--no-deps", "--format-version", "1", "--manifest-path", "src/tools/x/Cargo.toml"])
.stdout(Stdio::piped())
.spawn();
let child = match cmd {
Ok(child) => child,
Err(e) => {
println!("failed to get version of `x`: {}", e);
return None;
}
};
let cargo_output = child.wait_with_output().unwrap();
let cargo_output_str =
String::from_utf8(cargo_output.stdout).expect("Unable to parse `src/tools/x/Cargo.toml`");
let v: Value = serde_json::from_str(&cargo_output_str).unwrap();
let vesrion_str = &v["packages"][0]["version"].as_str()?;
Some(Version::parse(vesrion_str).unwrap())
}