Skip to content

Commit

Permalink
get latest x version from parsing cargo command
Browse files Browse the repository at this point in the history
  • Loading branch information
DebugSteven committed Jan 2, 2023
1 parent 9aebb1e commit e9ca663
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
17 changes: 9 additions & 8 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4814,9 +4814,9 @@ dependencies = [

[[package]]
name = "serde"
version = "1.0.147"
version = "1.0.152"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d193d69bae983fc11a79df82342761dfbf28a99fc8d203dca4c3c1b590948965"
checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb"
dependencies = [
"serde_derive",
]
Expand All @@ -4833,9 +4833,9 @@ dependencies = [

[[package]]
name = "serde_derive"
version = "1.0.147"
version = "1.0.152"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f1d362ca8fc9c3e3a7484440752472d68a6caa98f1ab81d99b5dfe517cec852"
checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e"
dependencies = [
"proc-macro2",
"quote",
Expand All @@ -4853,9 +4853,9 @@ dependencies = [

[[package]]
name = "serde_json"
version = "1.0.85"
version = "1.0.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44"
checksum = "877c235533714907a8c2464236f5c4b2a17262ef1bd71f38f35ea592c8da6883"
dependencies = [
"indexmap",
"itoa",
Expand Down Expand Up @@ -5133,9 +5133,9 @@ dependencies = [

[[package]]
name = "syn"
version = "1.0.102"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fcd952facd492f9be3ef0d0b7032a6e442ee9b361d4acc2b1d0c4aaa5f613a1"
checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5"
dependencies = [
"proc-macro2",
"quote",
Expand Down Expand Up @@ -5310,6 +5310,7 @@ dependencies = [
"miropt-test-tools",
"regex",
"semver",
"serde_json",
"termcolor",
"walkdir",
]
Expand Down
1 change: 1 addition & 0 deletions src/tools/tidy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ lazy_static = "1"
walkdir = "2"
ignore = "0.4.18"
semver = "1.0.14"
serde_json = "1.0.91"
termcolor = "1.1.3"

[[bin]]
Expand Down
48 changes: 38 additions & 10 deletions src/tools/tidy/src/x_version.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use semver::{BuildMetadata, Prerelease, Version};
use semver::Version;
use serde_json::Value;
use std::io::ErrorKind;
use std::process::{Command, Stdio};

Expand Down Expand Up @@ -33,20 +34,47 @@ pub fn check(bad: &mut bool) {
if output.status.success() {
let version = String::from_utf8_lossy(&output.stdout);
let version = Version::parse(version.trim_end()).unwrap();
let expected = Version {
major: 0,
minor: 1,
patch: 0,
pre: Prerelease::new("").unwrap(),
build: BuildMetadata::EMPTY,
};
if version < expected {

if let Some(expected) = get_x_wrapper_version() {
if version < expected {
return tidy_error!(
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`"
);
}
} else {
return tidy_error!(
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 {
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())
}

0 comments on commit e9ca663

Please sign in to comment.