2018-09-06 06:19:47 +00:00
|
|
|
use std::env;
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! get_version_info {
|
|
|
|
() => {{
|
|
|
|
let major = env!("CARGO_PKG_VERSION_MAJOR").parse::<u8>().unwrap();
|
|
|
|
let minor = env!("CARGO_PKG_VERSION_MINOR").parse::<u8>().unwrap();
|
|
|
|
let patch = env!("CARGO_PKG_VERSION_PATCH").parse::<u16>().unwrap();
|
2018-09-07 16:03:03 +00:00
|
|
|
let crate_name = String::from(env!("CARGO_PKG_NAME"));
|
2018-09-06 06:19:47 +00:00
|
|
|
|
2019-04-28 18:07:49 +00:00
|
|
|
let host_compiler = option_env!("RUSTC_RELEASE_CHANNEL").map(str::to_string);
|
2019-02-10 12:35:44 +00:00
|
|
|
let commit_hash = option_env!("GIT_HASH").map(str::to_string);
|
|
|
|
let commit_date = option_env!("COMMIT_DATE").map(str::to_string);
|
2018-09-06 06:19:47 +00:00
|
|
|
|
|
|
|
VersionInfo {
|
|
|
|
major,
|
|
|
|
minor,
|
|
|
|
patch,
|
|
|
|
host_compiler,
|
|
|
|
commit_hash,
|
|
|
|
commit_date,
|
2018-09-07 16:03:03 +00:00
|
|
|
crate_name,
|
2018-09-06 06:19:47 +00:00
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
// some code taken and adapted from RLS and cargo
|
|
|
|
pub struct VersionInfo {
|
|
|
|
pub major: u8,
|
|
|
|
pub minor: u8,
|
|
|
|
pub patch: u16,
|
|
|
|
pub host_compiler: Option<String>,
|
|
|
|
pub commit_hash: Option<String>,
|
|
|
|
pub commit_date: Option<String>,
|
2018-09-07 16:03:03 +00:00
|
|
|
pub crate_name: String,
|
2018-09-06 06:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for VersionInfo {
|
2018-09-13 17:06:04 +00:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2019-01-02 19:12:15 +00:00
|
|
|
let hash = self.commit_hash.clone().unwrap_or_default();
|
|
|
|
let hash_trimmed = hash.trim();
|
|
|
|
|
|
|
|
let date = self.commit_date.clone().unwrap_or_default();
|
|
|
|
let date_trimmed = date.trim();
|
|
|
|
|
|
|
|
if (hash_trimmed.len() + date_trimmed.len()) > 0 {
|
2018-09-13 17:06:04 +00:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{} {}.{}.{} ({} {})",
|
2019-01-02 19:12:15 +00:00
|
|
|
self.crate_name, self.major, self.minor, self.patch, hash_trimmed, date_trimmed,
|
2018-09-13 17:06:04 +00:00
|
|
|
)?;
|
|
|
|
} else {
|
|
|
|
write!(f, "{} {}.{}.{}", self.crate_name, self.major, self.minor, self.patch)?;
|
|
|
|
}
|
|
|
|
|
2018-09-06 06:19:47 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-09 21:40:26 +00:00
|
|
|
impl std::fmt::Debug for VersionInfo {
|
2018-09-13 17:06:04 +00:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2018-09-09 21:40:26 +00:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"VersionInfo {{ crate_name: \"{}\", major: {}, minor: {}, patch: {}",
|
|
|
|
self.crate_name, self.major, self.minor, self.patch,
|
|
|
|
)?;
|
2018-09-13 17:06:04 +00:00
|
|
|
if self.commit_hash.is_some() {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
", commit_hash: \"{}\", commit_date: \"{}\" }}",
|
|
|
|
self.commit_hash.clone().unwrap_or_default().trim(),
|
|
|
|
self.commit_date.clone().unwrap_or_default().trim()
|
|
|
|
)?;
|
|
|
|
} else {
|
|
|
|
write!(f, " }}")?;
|
2018-09-09 21:40:26 +00:00
|
|
|
}
|
2018-09-13 17:06:04 +00:00
|
|
|
|
2018-09-09 21:40:26 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-06 06:19:47 +00:00
|
|
|
pub fn get_commit_hash() -> Option<String> {
|
|
|
|
std::process::Command::new("git")
|
|
|
|
.args(&["rev-parse", "--short", "HEAD"])
|
|
|
|
.output()
|
|
|
|
.ok()
|
|
|
|
.and_then(|r| String::from_utf8(r.stdout).ok())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_commit_date() -> Option<String> {
|
|
|
|
std::process::Command::new("git")
|
|
|
|
.args(&["log", "-1", "--date=short", "--pretty=format:%cd"])
|
|
|
|
.output()
|
|
|
|
.ok()
|
|
|
|
.and_then(|r| String::from_utf8(r.stdout).ok())
|
|
|
|
}
|
2018-09-07 17:06:02 +00:00
|
|
|
|
2019-04-28 18:07:49 +00:00
|
|
|
pub fn get_channel() -> Option<String> {
|
|
|
|
match env::var("CFG_RELEASE_CHANNEL") {
|
|
|
|
Ok(channel) => Some(channel),
|
|
|
|
Err(_) => {
|
|
|
|
// if that failed, try to ask rustc -V, do some parsing and find out
|
|
|
|
match std::process::Command::new("rustc")
|
|
|
|
.arg("-V")
|
|
|
|
.output()
|
|
|
|
.ok()
|
|
|
|
.and_then(|r| String::from_utf8(r.stdout).ok())
|
|
|
|
{
|
|
|
|
Some(rustc_output) => {
|
|
|
|
if rustc_output.contains("beta") {
|
|
|
|
Some(String::from("beta"))
|
|
|
|
} else if rustc_output.contains("stable") {
|
|
|
|
Some(String::from("stable"))
|
|
|
|
} else {
|
|
|
|
// default to nightly if we fail to parse
|
|
|
|
Some(String::from("nightly"))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// default to nightly
|
|
|
|
None => Some(String::from("nightly")),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-07 17:06:02 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_struct_local() {
|
|
|
|
let vi = get_version_info!();
|
|
|
|
assert_eq!(vi.major, 0);
|
2019-05-23 15:30:05 +00:00
|
|
|
assert_eq!(vi.minor, 2);
|
|
|
|
assert_eq!(vi.patch, 0);
|
2018-09-07 17:06:02 +00:00
|
|
|
assert_eq!(vi.crate_name, "rustc_tools_util");
|
|
|
|
// hard to make positive tests for these since they will always change
|
|
|
|
assert!(vi.commit_hash.is_none());
|
|
|
|
assert!(vi.commit_date.is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_display_local() {
|
|
|
|
let vi = get_version_info!();
|
2019-05-23 15:30:05 +00:00
|
|
|
assert_eq!(vi.to_string(), "rustc_tools_util 0.2.0");
|
2018-09-09 21:40:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_debug_local() {
|
|
|
|
let vi = get_version_info!();
|
|
|
|
let s = format!("{:?}", vi);
|
|
|
|
assert_eq!(
|
|
|
|
s,
|
2019-05-23 15:30:05 +00:00
|
|
|
"VersionInfo { crate_name: \"rustc_tools_util\", major: 0, minor: 2, patch: 0 }"
|
2018-09-09 21:40:26 +00:00
|
|
|
);
|
2018-09-07 17:06:02 +00:00
|
|
|
}
|
|
|
|
}
|