|
| 1 | +use semver::Version; |
| 2 | +use std::path::Path; |
| 3 | +use std::process::{Command, Stdio}; |
| 4 | + |
| 5 | +pub fn check(root: &Path, cargo: &Path, bad: &mut bool) { |
| 6 | + let cargo_list = Command::new(cargo).args(["install", "--list"]).stdout(Stdio::piped()).spawn(); |
| 7 | + |
| 8 | + let child = match cargo_list { |
| 9 | + Ok(child) => child, |
| 10 | + Err(e) => return tidy_error!(bad, "failed to run `cargo`: {}", e), |
| 11 | + }; |
| 12 | + |
| 13 | + let cargo_list = child.wait_with_output().unwrap(); |
| 14 | + |
| 15 | + if cargo_list.status.success() { |
| 16 | + let exe_list = String::from_utf8_lossy(&cargo_list.stdout); |
| 17 | + let exe_list = exe_list.lines(); |
| 18 | + |
| 19 | + let mut installed: Option<Version> = None; |
| 20 | + |
| 21 | + for line in exe_list { |
| 22 | + let mut iter = line.split_whitespace(); |
| 23 | + if iter.next() == Some("x") { |
| 24 | + if let Some(version) = iter.next() { |
| 25 | + // Check this is the rust-lang/rust x tool installation since it should be |
| 26 | + // installed at a path containing `src/tools/x`. |
| 27 | + if let Some(path) = iter.next() { |
| 28 | + if path.contains(&"src/tools/x") { |
| 29 | + let version = version.strip_prefix("v").unwrap(); |
| 30 | + installed = Some(Version::parse(version).unwrap()); |
| 31 | + break; |
| 32 | + } |
| 33 | + }; |
| 34 | + } |
| 35 | + } else { |
| 36 | + continue; |
| 37 | + } |
| 38 | + } |
| 39 | + // Unwrap the some if x is installed, otherwise return because it's fine if x isn't installed. |
| 40 | + let installed = if let Some(i) = installed { i } else { return }; |
| 41 | + |
| 42 | + if let Some(expected) = get_x_wrapper_version(root, cargo) { |
| 43 | + if installed < expected { |
| 44 | + return println!( |
| 45 | + "Current version of x is {installed}, but the latest version is {expected}\nConsider updating to the newer version of x by running `cargo install --path src/tools/x`" |
| 46 | + ); |
| 47 | + } |
| 48 | + } else { |
| 49 | + return tidy_error!( |
| 50 | + bad, |
| 51 | + "Unable to parse the latest version of `x` at `src/tools/x/Cargo.toml`" |
| 52 | + ); |
| 53 | + } |
| 54 | + } else { |
| 55 | + return tidy_error!(bad, "failed to check version of `x`: {}", cargo_list.status); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// Parse latest version out of `x` Cargo.toml |
| 60 | +fn get_x_wrapper_version(root: &Path, cargo: &Path) -> Option<Version> { |
| 61 | + let mut cmd = cargo_metadata::MetadataCommand::new(); |
| 62 | + cmd.cargo_path(cargo) |
| 63 | + .manifest_path(root.join("src/tools/x/Cargo.toml")) |
| 64 | + .no_deps() |
| 65 | + .features(cargo_metadata::CargoOpt::AllFeatures); |
| 66 | + let mut metadata = t!(cmd.exec()); |
| 67 | + metadata.packages.pop().map(|x| x.version) |
| 68 | +} |
0 commit comments