Skip to content

Commit

Permalink
print version information when -V or --version is issued
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge Aparicio committed Dec 26, 2016
1 parent 3b801f3 commit 8de4177
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
authors = ["Jorge Aparicio <japaricious@gmail.com>"]
build = "build.rs"
name = "cross"
version = "0.1.0-dev"

Expand Down
55 changes: 55 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::env;
use std::error::Error;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;

struct Some {}

impl<E> From<E> for Some
where E: Error
{
fn from(_: E) -> Some {
Some {}
}
}

fn main() {
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());

File::create(out_dir.join("commit-info.txt"))
.unwrap()
.write_all(commit_info().as_bytes())
.unwrap();
}

fn commit_info() -> String {
match (commit_hash(), commit_date()) {
(Ok(hash), Ok(date)) => format!(" ({} {})", hash.trim(), date.trim()),
_ => String::new(),
}
}

fn commit_hash() -> Result<String, Some> {
let output = Command::new("git").args(&["rev-parse", "--short", "HEAD"])
.output()?;

if output.status.success() {
Ok(String::from_utf8(output.stdout)?)
} else {
Err(Some {})
}
}

fn commit_date() -> Result<String, Some> {
let output = Command::new("git")
.args(&["log", "-1", "--date=short", "--pretty=format:%cd"])
.output()?;

if output.status.success() {
Ok(String::from_utf8(output.stdout)?)
} else {
Err(Some {})
}
}
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ fn main() {
fn run() -> Result<ExitStatus> {
let args = cli::parse();

if args.all.iter().any(|a| a == "--version" || a == "-V") {
println!(concat!("cross ", env!("CARGO_PKG_VERSION"), "{}"),
include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt")));
}

match args.subcommand.as_ref().map(|s| &**s) {
Some("build") | Some("run") | Some("rustc") | Some("test") => {
let host = rustc::host();
Expand Down

0 comments on commit 8de4177

Please sign in to comment.