|
| 1 | +#![feature(tool_lints)] |
| 2 | + |
| 3 | +use std::env; |
| 4 | + |
| 5 | +#[macro_export] |
| 6 | +macro_rules! get_version_info { |
| 7 | + () => {{ |
| 8 | + let major = env!("CARGO_PKG_VERSION_MAJOR").parse::<u8>().unwrap(); |
| 9 | + let minor = env!("CARGO_PKG_VERSION_MINOR").parse::<u8>().unwrap(); |
| 10 | + let patch = env!("CARGO_PKG_VERSION_PATCH").parse::<u16>().unwrap(); |
| 11 | + |
| 12 | + let host_compiler = $crate::get_channel(); |
| 13 | + let commit_hash = option_env!("GIT_HASH").map(|s| s.to_string()); |
| 14 | + let commit_date = option_env!("COMMIT_DATE").map(|s| s.to_string()); |
| 15 | + |
| 16 | + VersionInfo { |
| 17 | + major, |
| 18 | + minor, |
| 19 | + patch, |
| 20 | + host_compiler, |
| 21 | + commit_hash, |
| 22 | + commit_date, |
| 23 | + } |
| 24 | + }}; |
| 25 | +} |
| 26 | + |
| 27 | +// some code taken and adapted from RLS and cargo |
| 28 | +pub struct VersionInfo { |
| 29 | + pub major: u8, |
| 30 | + pub minor: u8, |
| 31 | + pub patch: u16, |
| 32 | + pub host_compiler: Option<String>, |
| 33 | + pub commit_hash: Option<String>, |
| 34 | + pub commit_date: Option<String>, |
| 35 | +} |
| 36 | + |
| 37 | +impl std::fmt::Display for VersionInfo { |
| 38 | + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 39 | + match self.commit_hash { |
| 40 | + Some(_) => { |
| 41 | + write!( |
| 42 | + f, |
| 43 | + "clippy {}.{}.{} ({} {})", |
| 44 | + self.major, |
| 45 | + self.minor, |
| 46 | + self.patch, |
| 47 | + self.commit_hash.clone().unwrap_or_default().trim(), |
| 48 | + self.commit_date.clone().unwrap_or_default().trim(), |
| 49 | + )?; |
| 50 | + }, |
| 51 | + None => { |
| 52 | + write!(f, "clippy {}.{}.{}", self.major, self.minor, self.patch)?; |
| 53 | + }, |
| 54 | + }; |
| 55 | + Ok(()) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +pub fn get_channel() -> Option<String> { |
| 60 | + if let Ok(channel) = env::var("CFG_RELEASE_CHANNEL") { |
| 61 | + Some(channel) |
| 62 | + } else { |
| 63 | + // we could ask ${RUSTC} -Vv and do some parsing and find out |
| 64 | + Some(String::from("nightly")) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +pub fn get_commit_hash() -> Option<String> { |
| 69 | + std::process::Command::new("git") |
| 70 | + .args(&["rev-parse", "--short", "HEAD"]) |
| 71 | + .output() |
| 72 | + .ok() |
| 73 | + .and_then(|r| String::from_utf8(r.stdout).ok()) |
| 74 | +} |
| 75 | + |
| 76 | +pub fn get_commit_date() -> Option<String> { |
| 77 | + std::process::Command::new("git") |
| 78 | + .args(&["log", "-1", "--date=short", "--pretty=format:%cd"]) |
| 79 | + .output() |
| 80 | + .ok() |
| 81 | + .and_then(|r| String::from_utf8(r.stdout).ok()) |
| 82 | +} |
0 commit comments