Skip to content

Commit

Permalink
use cargo_metadata to get x version
Browse files Browse the repository at this point in the history
  • Loading branch information
DebugSteven committed Jan 3, 2023
1 parent e9ca663 commit 376dd8a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 29 deletions.
1 change: 0 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5310,7 +5310,6 @@ dependencies = [
"miropt-test-tools",
"regex",
"semver",
"serde_json",
"termcolor",
"walkdir",
]
Expand Down
1 change: 0 additions & 1 deletion src/tools/tidy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
2 changes: 1 addition & 1 deletion src/tools/tidy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 10 additions & 26 deletions src/tools/tidy/src/x_version.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -54,27 +54,11 @@ pub fn check(bad: &mut bool) {
}

// 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())
fn get_x_wrapper_version(root: &Path, cargo: &Path) -> Option<Version> {
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)
}

0 comments on commit 376dd8a

Please sign in to comment.