Skip to content

Commit

Permalink
Switch to git-testament rather than old build.rs
Browse files Browse the repository at this point in the history
Rather than the previous not-very-informative version string
which was generated by build.rs and then not updated unless
build.rs was touched, use the new `git-testament` crate which
does a more complete job at compile time via a proc-macro.

Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
  • Loading branch information
kinnison committed Apr 3, 2019
1 parent f07e26f commit 6b95547
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 55 deletions.
48 changes: 48 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ wait-timeout = "0.1.5"
walkdir = "2.0"
xz2 = "0.1.3"
openssl = { version = '0.10.15', optional = true }
git-testament = "0.1"
lazy_static = "1"

[target."cfg(windows)".dependencies]
winapi = { version = "0.3", features = ["combaseapi", "errhandlingapi", "fileapi", "handleapi", "ioapiset", "jobapi", "jobapi2", "minwindef", "processthreadsapi", "psapi", "shlobj", "shtypes", "synchapi", "sysinfoapi", "tlhelp32", "userenv", "winbase", "winerror", "winioctl", "winnt", "winuser"] }
Expand Down
51 changes: 0 additions & 51 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,6 @@
use std::env;
use std::error::Error;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;

struct Ignore;

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

fn main() {
let target = env::var("TARGET").unwrap();
println!("cargo:rustc-env=TARGET={}", target);

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();

println!("cargo:rerun-if-changed=build.rs");
}

// Try to get hash and date of the last commit on a best effort basis. If anything goes wrong
// (git not installed or if this is not a git repository) just return an empty string.
fn commit_info() -> String {
match (commit_hash(), commit_date()) {
(Ok(hash), Ok(date)) => format!(" ({} {})", hash.trim_end(), date),
_ => String::new(),
}
}

fn commit_hash() -> Result<String, Ignore> {
Ok(String::from_utf8(
Command::new("git")
.args(&["rev-parse", "--short=9", "HEAD"])
.output()?
.stdout,
)?)
}

fn commit_date() -> Result<String, Ignore> {
Ok(String::from_utf8(
Command::new("git")
.args(&["log", "-1", "--date=short", "--pretty=format:%cd"])
.output()?
.stdout,
)?)
}
12 changes: 8 additions & 4 deletions src/cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use crate::errors::*;
use crate::self_update;
use crate::term2;
use git_testament::{git_testament, render_testament};
use lazy_static::lazy_static;
use rustup::utils::notify::NotificationLevel;
use rustup::utils::utils;
use rustup::{Cfg, Notification, Toolchain, UpdateStatus};
Expand Down Expand Up @@ -402,11 +404,13 @@ pub fn list_overrides(cfg: &Cfg) -> Result<()> {
Ok(())
}

git_testament!(TESTAMENT);

pub fn version() -> &'static str {
concat!(
env!("CARGO_PKG_VERSION"),
include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt"))
)
lazy_static! {
static ref RENDERED: String = render_testament!(TESTAMENT);
}
&RENDERED
}

pub fn report_error(e: &Error) {
Expand Down

0 comments on commit 6b95547

Please sign in to comment.