]> git.lizzy.rs Git - rust.git/blob - src/tools/tidy/src/x_version.rs
868b3d925d3c0dbf1b552f9c839cb23ea4c6aff5
[rust.git] / src / tools / tidy / src / x_version.rs
1 use semver::{BuildMetadata, Prerelease, Version};
2 use std::io::ErrorKind;
3 use std::process::{Command, Stdio};
4
5 pub fn check(bad: &mut bool) {
6     let result = Command::new("x").arg("--wrapper-version").stdout(Stdio::piped()).spawn();
7     // This runs the command inside a temporary directory.
8     // This allows us to compare output of result to see if `--wrapper-version` is not a recognized argument to x.
9     let temp_result = Command::new("x")
10         .arg("--wrapper-version")
11         .current_dir(std::env::temp_dir())
12         .stdout(Stdio::piped())
13         .spawn();
14
15     let (child, temp_child) = match (result, temp_result) {
16         (Ok(child), Ok(temp_child)) => (child, temp_child),
17         (Err(e), _) | (_, Err(e)) => match e.kind() {
18             ErrorKind::NotFound => return,
19             _ => return tidy_error!(bad, "failed to run `x`: {}", e),
20         },
21     };
22
23     let output = child.wait_with_output().unwrap();
24     let temp_output = temp_child.wait_with_output().unwrap();
25
26     if output != temp_output {
27         return tidy_error!(
28             bad,
29             "Current version of x does not support the `--wrapper-version` argument\nConsider updating to the newer version of x by running `cargo install --path src/tools/x`"
30         );
31     }
32
33     if output.status.success() {
34         let version = String::from_utf8_lossy(&output.stdout);
35         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 {
44             return tidy_error!(
45                 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`"
47             );
48         }
49     } else {
50         return tidy_error!(bad, "failed to check version of `x`: {}", output.status);
51     }
52 }