#![warn(missing_docs)]
extern crate semver;
use semver::{Version, VersionReq, Identifier};
use std::process::Command;
use std::env;
use std::ffi::OsString;
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum Channel {
Dev,
Nightly,
Beta,
Stable,
}
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct VersionMeta {
pub semver: Version,
pub commit_hash: Option<String>,
pub commit_date: Option<String>,
pub channel: Channel,
pub host: String,
pub short_version_string: String,
}
pub fn version() -> Version {
version_meta().semver
}
pub fn version_meta() -> VersionMeta {
let cmd = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc"));
let out = Command::new(&cmd)
.arg("-vV")
.output()
.unwrap_or_else(|e| { panic!("failed to execute `RUSTC -vV`: {}", e) });
let out = String::from_utf8(out.stdout)
.ok()
.expect("non utf8 output from RUSTC -vV");
version_meta_for(&out)
}
pub fn version_meta_for(verbose_version_string: &str) -> VersionMeta {
let out: Vec<_> = verbose_version_string.lines().collect();
const ERR_MSG: &'static str = "unexpected -vV format";
assert!(out.len() == 6, ERR_MSG);
let short_version_string = out[0];
fn expect_prefix<'a>(line: &'a str, prefix: &str) -> &'a str {
assert!(line.starts_with(prefix), ERR_MSG);
&line[prefix.len()..]
}
let commit_hash = match expect_prefix(out[2], "commit-hash: ") {
"unknown" => None,
hash => Some(hash.to_owned()),
};
let commit_date = match expect_prefix(out[3], "commit-date: ") {
"unknown" => None,
hash => Some(hash.to_owned()),
};
let host = expect_prefix(out[4], "host: ");
let release = expect_prefix(out[5], "release: ");
let semver = Version::parse(release).unwrap();
let channel = if semver.pre.is_empty() {
Channel::Stable
} else {
match semver.pre[0] {
Identifier::AlphaNumeric(ref s)
if s == "dev" => Channel::Dev,
Identifier::AlphaNumeric(ref s)
if s == "beta" => Channel::Beta,
Identifier::AlphaNumeric(ref s)
if s == "nightly" => Channel::Nightly,
ref x => panic!("Unknown pre-release tag {}", x),
}
};
VersionMeta {
semver: semver,
commit_hash: commit_hash,
commit_date: commit_date,
channel: channel,
host: host.into(),
short_version_string: short_version_string.into(),
}
}
pub fn version_matches(req: &str) -> bool {
VersionReq::parse(req).unwrap().matches(&version())
}
#[test]
fn smoketest() {
let v = version();
assert!(v.major >= 1);
assert!(v.minor >= 2);
let v = version_meta();
assert!(v.semver.major >= 1);
assert!(v.semver.minor >= 2);
assert!(version_matches(">= 1.2.0"));
}
#[test]
fn parse_unknown() {
let version = version_meta_for(
"rustc 1.3.0
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.3.0");
assert_eq!(version.semver, Version::parse("1.3.0").unwrap());
assert_eq!(version.commit_hash, None);
assert_eq!(version.commit_date, None);
assert_eq!(version.channel, Channel::Stable);
assert_eq!(version.host, "x86_64-unknown-linux-gnu");
assert_eq!(version.short_version_string, "rustc 1.3.0");
}
#[test]
fn parse_nightly() {
let version = version_meta_for(
"rustc 1.5.0-nightly (65d5c0833 2015-09-29)
binary: rustc
commit-hash: 65d5c083377645a115c4ac23a620d3581b9562b6
commit-date: 2015-09-29
host: x86_64-unknown-linux-gnu
release: 1.5.0-nightly");
assert_eq!(version.semver, Version::parse("1.5.0-nightly").unwrap());
assert_eq!(version.commit_hash, Some("65d5c083377645a115c4ac23a620d3581b9562b6".into()));
assert_eq!(version.commit_date, Some("2015-09-29".into()));
assert_eq!(version.channel, Channel::Nightly);
assert_eq!(version.host, "x86_64-unknown-linux-gnu");
assert_eq!(version.short_version_string, "rustc 1.5.0-nightly (65d5c0833 2015-09-29)");
}
#[test]
fn parse_stable() {
let version = version_meta_for(
"rustc 1.3.0 (9a92aaf19 2015-09-15)
binary: rustc
commit-hash: 9a92aaf19a64603b02b4130fe52958cc12488900
commit-date: 2015-09-15
host: x86_64-unknown-linux-gnu
release: 1.3.0");
assert_eq!(version.semver, Version::parse("1.3.0").unwrap());
assert_eq!(version.commit_hash, Some("9a92aaf19a64603b02b4130fe52958cc12488900".into()));
assert_eq!(version.commit_date, Some("2015-09-15".into()));
assert_eq!(version.channel, Channel::Stable);
assert_eq!(version.host, "x86_64-unknown-linux-gnu");
assert_eq!(version.short_version_string, "rustc 1.3.0 (9a92aaf19 2015-09-15)");
}