Navigation Menu

Skip to content

Commit

Permalink
Display more information for non-release releases
Browse files Browse the repository at this point in the history
  • Loading branch information
ogham committed Oct 8, 2017
1 parent e154b58 commit c7497f3
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

8 changes: 7 additions & 1 deletion Cargo.toml
@@ -1,7 +1,8 @@
[package]
name = "exa"
version = "0.8.0"
version = "0.9.0-pre"
authors = [ "ogham@bsago.me" ]
build = "build.rs"

description = "A modern replacement for ls"
homepage = "https://the.exa.website/"
Expand All @@ -13,11 +14,13 @@ categories = ["command-line-utilities"]
keywords = ["ls", "files", "command-line"]
license = "MIT"


[[bin]]
name = "exa"
path = "src/bin/main.rs"
doc = false


[lib]
name = "exa"
path = "src/exa.rs"
Expand All @@ -41,6 +44,9 @@ unicode-width = "0.1.4"
users = "0.5.2"
zoneinfo_compiled = "0.4.5"

[build-dependencies]
datetime = "0.4.5"

[features]
default = [ "git" ]
git = [ "git2" ]
Expand Down
59 changes: 59 additions & 0 deletions build.rs
@@ -0,0 +1,59 @@
/// The version string isn’t the simplest: we want to show the version,
/// current Git hash, and compilation date when building *debug* versions, but
/// just the version for *release* versions so the builds are reproducible.
///
/// This script generates the string from the environment variables that Cargo
/// adds (http://doc.crates.io/environment-variables.html) and runs `git` to
/// get the SHA1 hash. It then writes the string into a file, which exa then
/// includes at build-time.
///
/// - https://stackoverflow.com/q/43753491/3484614
/// - https://crates.io/crates/vergen

extern crate datetime;
use std::io::Result as IOResult;
use std::env;

fn git_hash() -> String {
use std::process::Command;

String::from_utf8_lossy(
&Command::new("git")
.args(&["rev-parse", "--short", "HEAD"])
.output().unwrap()
.stdout).trim().to_string()
}

fn main() {
write_statics().unwrap();
}

fn is_development_version() -> bool {
env::var("PROFILE").unwrap() == "debug"
}

fn cargo_version() -> String {
env::var("CARGO_PKG_VERSION").unwrap()
}

fn build_date() -> String {
use datetime::{LocalDateTime, ISO};

let now = LocalDateTime::now();
format!("{}", now.date().iso())
}

fn write_statics() -> IOResult<()> {
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;

let ver = match is_development_version() {
true => format!("exa v{} ({} built on {})", cargo_version(), git_hash(), build_date()),
false => format!("exa v{}", cargo_version()),
};

let out = PathBuf::from(env::var("OUT_DIR").unwrap());
let mut f = File::create(&out.join("version_string.txt"))?;
write!(f, "{:?}", ver)
}
20 changes: 8 additions & 12 deletions src/options/version.rs
@@ -1,16 +1,16 @@
//! Printing the version string.
//!
//! The code that works out which string to print is done in `build.rs`.

use std::fmt;

use options::flags;
use options::parser::MatchedFlags;


/// All the information needed to display the version information.
#[derive(PartialEq, Debug)]
pub struct VersionString {

/// The version number from cargo.
cargo: &'static str,
}
pub struct VersionString;
// There were options here once, but there aren’t anymore!

impl VersionString {

Expand All @@ -21,7 +21,7 @@ impl VersionString {
/// Like --help, this doesn’t bother checking for errors.
pub fn deduce(matches: &MatchedFlags) -> Result<(), VersionString> {
if matches.count(&flags::VERSION) > 0 {
Err(VersionString { cargo: env!("CARGO_PKG_VERSION") })
Err(VersionString)
}
else {
Ok(()) // no version needs to be shown
Expand All @@ -30,16 +30,12 @@ impl VersionString {
}

impl fmt::Display for VersionString {

/// Format this help options into an actual string of help
/// text to be displayed to the user.
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "exa v{}", self.cargo)
write!(f, "{}", include!(concat!(env!("OUT_DIR"), "/version_string.txt")))
}
}



#[cfg(test)]
mod test {
use options::Options;
Expand Down

0 comments on commit c7497f3

Please sign in to comment.