From 376dd8a9b36eb4d0c192f3767562ca0bdab60ff9 Mon Sep 17 00:00:00 2001 From: DebugSteven Date: Mon, 2 Jan 2023 19:31:18 -0700 Subject: [PATCH] use cargo_metadata to get x version --- Cargo.lock | 1 - src/tools/tidy/Cargo.toml | 1 - src/tools/tidy/src/main.rs | 2 +- src/tools/tidy/src/x_version.rs | 36 +++++++++------------------------ 4 files changed, 11 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 784aca237f0..4cb64882cb7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5310,7 +5310,6 @@ dependencies = [ "miropt-test-tools", "regex", "semver", - "serde_json", "termcolor", "walkdir", ] diff --git a/src/tools/tidy/Cargo.toml b/src/tools/tidy/Cargo.toml index a13ecbe955a..5f5ae3a65ef 100644 --- a/src/tools/tidy/Cargo.toml +++ b/src/tools/tidy/Cargo.toml @@ -12,7 +12,6 @@ lazy_static = "1" walkdir = "2" ignore = "0.4.18" semver = "1.0.14" -serde_json = "1.0.91" termcolor = "1.1.3" [[bin]] diff --git a/src/tools/tidy/src/main.rs b/src/tools/tidy/src/main.rs index 56fcc561a3f..7bb8ddc6949 100644 --- a/src/tools/tidy/src/main.rs +++ b/src/tools/tidy/src/main.rs @@ -107,7 +107,7 @@ fn main() { check!(alphabetical, &compiler_path); check!(alphabetical, &library_path); - check!(x_version); + check!(x_version, &root_path, &cargo); let collected = { drain_handles(&mut handles); diff --git a/src/tools/tidy/src/x_version.rs b/src/tools/tidy/src/x_version.rs index 6cadc18fd3e..070a751b051 100644 --- a/src/tools/tidy/src/x_version.rs +++ b/src/tools/tidy/src/x_version.rs @@ -1,9 +1,9 @@ use semver::Version; -use serde_json::Value; use std::io::ErrorKind; +use std::path::Path; use std::process::{Command, Stdio}; -pub fn check(bad: &mut bool) { +pub fn check(root: &Path, cargo: &Path, bad: &mut bool) { let result = Command::new("x").arg("--wrapper-version").stdout(Stdio::piped()).spawn(); // This runs the command inside a temporary directory. // This allows us to compare output of result to see if `--wrapper-version` is not a recognized argument to x. @@ -35,7 +35,7 @@ pub fn check(bad: &mut bool) { let version = String::from_utf8_lossy(&output.stdout); let version = Version::parse(version.trim_end()).unwrap(); - if let Some(expected) = get_x_wrapper_version() { + if let Some(expected) = get_x_wrapper_version(root, cargo) { if version < expected { return tidy_error!( bad, @@ -54,27 +54,11 @@ pub fn check(bad: &mut bool) { } // Parse latest version out of `x` Cargo.toml -fn get_x_wrapper_version() -> Option { - 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()) +fn get_x_wrapper_version(root: &Path, cargo: &Path) -> Option { + let mut cmd = cargo_metadata::MetadataCommand::new(); + cmd.cargo_path(cargo) + .manifest_path(root.join("src/tools/x/Cargo.toml")) + .features(cargo_metadata::CargoOpt::AllFeatures); + let mut metadata = t!(cmd.exec()); + metadata.packages.pop().map(|x| x.version) }