|
1 |
| -use semver::{BuildMetadata, Prerelease, Version}; |
| 1 | +use semver::Version; |
| 2 | +use serde_json::Value; |
2 | 3 | use std::io::ErrorKind;
|
3 | 4 | use std::process::{Command, Stdio};
|
4 | 5 |
|
@@ -33,20 +34,47 @@ pub fn check(bad: &mut bool) {
|
33 | 34 | if output.status.success() {
|
34 | 35 | let version = String::from_utf8_lossy(&output.stdout);
|
35 | 36 | let version = Version::parse(version.trim_end()).unwrap();
|
36 |
| - let expected = Version { |
37 |
| - major: 0, |
38 |
| - minor: 1, |
39 |
| - patch: 0, |
40 |
| - pre: Prerelease::new("").unwrap(), |
41 |
| - build: BuildMetadata::EMPTY, |
42 |
| - }; |
43 |
| - if version < expected { |
| 37 | + |
| 38 | + if let Some(expected) = get_x_wrapper_version() { |
| 39 | + if version < expected { |
| 40 | + return tidy_error!( |
| 41 | + bad, |
| 42 | + "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`" |
| 43 | + ); |
| 44 | + } |
| 45 | + } else { |
44 | 46 | return tidy_error!(
|
45 | 47 | bad,
|
46 |
| - "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`" |
| 48 | + "Unable to parse the latest version of `x` at `src/tools/x/Cargo.toml`" |
47 | 49 | );
|
48 | 50 | }
|
49 | 51 | } else {
|
50 | 52 | return tidy_error!(bad, "failed to check version of `x`: {}", output.status);
|
51 | 53 | }
|
52 | 54 | }
|
| 55 | + |
| 56 | +// 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()) |
| 80 | +} |
0 commit comments