use std::env;
use std::fs::File;
use std::io::{self, Write};
use std::path::PathBuf;
use datetime::{LocalDateTime, ISO};
fn main() -> io::Result<()> {
#![allow(clippy::write_with_newline)]
let tagline = "exa - list files on the command-line";
let url = "https://the.exa.website/";
let ver =
if is_debug_build() {
format!("{}\nv{} \\1;31m(pre-release debug build!)\\0m\n\\1;4;34m{}\\0m", tagline, version_string(), url)
}
else if is_development_version() {
format!("{}\nv{} [{}] built on {} \\1;31m(pre-release!)\\0m\n\\1;4;34m{}\\0m", tagline, version_string(), git_hash(), build_date(), url)
}
else {
format!("{}\nv{}\n\\1;4;34m{}\\0m", tagline, version_string(), url)
};
let out = PathBuf::from(env::var("OUT_DIR").unwrap());
let mut f = File::create(&out.join("version_string.txt"))?;
writeln!(f, "{}", strip_codes(&ver))?;
Ok(())
}
fn strip_codes(input: &str) -> String {
input.replace("\\0m", "")
.replace("\\1;31m", "")
.replace("\\1;4;34m", "")
}
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 is_development_version() -> bool {
cargo_version().ends_with("-pre") || env::var("PROFILE").unwrap() == "debug"
}
fn is_debug_build() -> bool {
env::var("PROFILE").unwrap() == "debug"
}
fn cargo_version() -> String {
env::var("CARGO_PKG_VERSION").unwrap()
}
fn version_string() -> String {
let mut ver = cargo_version();
let feats = nonstandard_features_string();
if ! feats.is_empty() {
ver.push_str(&format!(" [{}]", &feats));
}
ver
}
fn feature_enabled(name: &str) -> bool {
env::var(&format!("CARGO_FEATURE_{}", name))
.map(|e| ! e.is_empty())
.unwrap_or(false)
}
fn nonstandard_features_string() -> String {
let mut s = Vec::new();
if feature_enabled("GIT") {
s.push("+git");
}
else {
s.push("-git");
}
s.join(", ")
}
fn build_date() -> String {
let now = LocalDateTime::now();
format!("{}", now.date().iso())
}