Skip to content

Commit 376dd8a

Browse files
committed
use cargo_metadata to get x version
1 parent e9ca663 commit 376dd8a

File tree

4 files changed

+11
-29
lines changed

4 files changed

+11
-29
lines changed

Cargo.lock

-1
Original file line numberDiff line numberDiff line change
@@ -5310,7 +5310,6 @@ dependencies = [
53105310
"miropt-test-tools",
53115311
"regex",
53125312
"semver",
5313-
"serde_json",
53145313
"termcolor",
53155314
"walkdir",
53165315
]

src/tools/tidy/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ lazy_static = "1"
1212
walkdir = "2"
1313
ignore = "0.4.18"
1414
semver = "1.0.14"
15-
serde_json = "1.0.91"
1615
termcolor = "1.1.3"
1716

1817
[[bin]]

src/tools/tidy/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fn main() {
107107
check!(alphabetical, &compiler_path);
108108
check!(alphabetical, &library_path);
109109

110-
check!(x_version);
110+
check!(x_version, &root_path, &cargo);
111111

112112
let collected = {
113113
drain_handles(&mut handles);

src/tools/tidy/src/x_version.rs

+10-26
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use semver::Version;
2-
use serde_json::Value;
32
use std::io::ErrorKind;
3+
use std::path::Path;
44
use std::process::{Command, Stdio};
55

6-
pub fn check(bad: &mut bool) {
6+
pub fn check(root: &Path, cargo: &Path, bad: &mut bool) {
77
let result = Command::new("x").arg("--wrapper-version").stdout(Stdio::piped()).spawn();
88
// This runs the command inside a temporary directory.
99
// 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) {
3535
let version = String::from_utf8_lossy(&output.stdout);
3636
let version = Version::parse(version.trim_end()).unwrap();
3737

38-
if let Some(expected) = get_x_wrapper_version() {
38+
if let Some(expected) = get_x_wrapper_version(root, cargo) {
3939
if version < expected {
4040
return tidy_error!(
4141
bad,
@@ -54,27 +54,11 @@ pub fn check(bad: &mut bool) {
5454
}
5555

5656
// Parse latest version out of `x` Cargo.toml
57-
fn get_x_wrapper_version() -> Option<Version> {
58-
let cmd = Command::new("cargo")
59-
.arg("metadata")
60-
.args(["--no-deps", "--format-version", "1", "--manifest-path", "src/tools/x/Cargo.toml"])
61-
.stdout(Stdio::piped())
62-
.spawn();
63-
64-
let child = match cmd {
65-
Ok(child) => child,
66-
Err(e) => {
67-
println!("failed to get version of `x`: {}", e);
68-
return None;
69-
}
70-
};
71-
72-
let cargo_output = child.wait_with_output().unwrap();
73-
let cargo_output_str =
74-
String::from_utf8(cargo_output.stdout).expect("Unable to parse `src/tools/x/Cargo.toml`");
75-
76-
let v: Value = serde_json::from_str(&cargo_output_str).unwrap();
77-
let vesrion_str = &v["packages"][0]["version"].as_str()?;
78-
79-
Some(Version::parse(vesrion_str).unwrap())
57+
fn get_x_wrapper_version(root: &Path, cargo: &Path) -> Option<Version> {
58+
let mut cmd = cargo_metadata::MetadataCommand::new();
59+
cmd.cargo_path(cargo)
60+
.manifest_path(root.join("src/tools/x/Cargo.toml"))
61+
.features(cargo_metadata::CargoOpt::AllFeatures);
62+
let mut metadata = t!(cmd.exec());
63+
metadata.packages.pop().map(|x| x.version)
8064
}

0 commit comments

Comments
 (0)