1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

use build_env_info as benv;
use std;


fn dirty() -> bool {
    match *benv::BUILD_ENV_INFO.get("GIT_STATUS").unwrap() {
        Some(v) => v.contains("modified"),
        None => true, //because we don't know
    }
}

/// The parent folder to this crate
pub fn crate_parent_folder() -> std::path::PathBuf{
    ::std::path::Path::new(get_build_env_value("CARGO_MANIFEST_DIR").unwrap()).parent().unwrap().to_owned()
}


fn dirty_star() -> &'static str {
    if dirty() { "*" } else { "" }
}

fn commit9_and_date() -> String {
    format!("{}{} {}",
            &benv::GIT_COMMIT[0..9],
            dirty_star(),
            benv::GENERATED_DATE_UTC)
}

fn one_line_suffix() -> String {
    let c9d = commit9_and_date();
    format!("({}) for {}", c9d, benv::TARGET)
}



pub fn get_build_env_value(key: &str) -> &Option<&'static str> {
    static NONE:Option<&'static str>  = None;
     match benv::BUILD_ENV_INFO.get(key){
         Some(v) => v,
         None => &NONE
     }
}

fn built_ago() -> (i64, &'static str){
    let compiled_utc = ::chrono::datetime::DateTime::parse_from_rfc3339(benv::GENERATED_DATETIME_UTC).unwrap();
    let duration = ::chrono::UTC::now() -compiled_utc;
    let (v,u) = if duration.num_days() > 0 {
        (duration.num_days(), "days")
    }else if duration.num_hours() > 0{
        (duration.num_hours(), "hours")
    }else if duration.num_minutes() > 0{
        (duration.num_minutes(), "minutes")
    }else {
        (duration.num_seconds(), "seconds")
    };
    if v < 1{
        (v, &u[0..u.len()-1])
    }else{
        (v,u)
    }

}

pub fn one_line_version() -> String {
    //Create options for branch and release_tag
    let branch = benv::BUILD_ENV_INFO.get("GIT_OPTIONAL_BRANCH").unwrap(); //still needs to be unwrapped
    let release_tag = if let &Some(ref tag) = benv::BUILD_ENV_INFO.get("GIT_OPTIONAL_TAG").unwrap() {
        if tag.starts_with("v") {
            Some(&tag[1..])
        } else { None }
    } else {None};


    let profile_release = get_build_env_value("PROFILE") == &Some("release");
    let ci_job_title = get_build_env_value("CI_JOB_TITLE").unwrap_or("Local ");
    let profile = get_build_env_value("PROFILE").unwrap_or("[profile missing]");
    let ci = benv::BUILT_ON_CI;

    let target_cpu = match get_build_env_value("TARGET_CPU").unwrap_or(get_build_env_value("RUSTFLAGS").unwrap_or("?")){
        "x86-64" | "x86"=> "",
        "native" => "HOST NATIVE CPU (not portable)",
        other => other
    };


    if ci && profile_release && release_tag.is_some(){
        format!("release {} {} {}", release_tag.unwrap(), one_line_suffix(), target_cpu)
    }else if ci && profile_release && branch == &Some("master") && !dirty() {
        format!("nightly {} from master {} {}",
                benv::GIT_DESCRIBE_ALWAYS,
                one_line_suffix(), target_cpu)
    }else{

        let (v, unit) = built_ago();

        let source = if ci_job_title.starts_with("Travis 88888"){
            "simulation CI"
        }else if ci_job_title.starts_with("Travis "){
            "unofficial CI"
        }else if ci_job_title.starts_with("AppVeyor ") {
            "unofficial CI"
        }else if ci_job_title.starts_with("Local ") {
            "user-compiled"
        }else  {
            "SOURCE UNKNOWN"
        };


        format!("built {} {} ago - {} {} build of {}{} ({}) for {} {} ({})",
                v, unit, source, profile,
                benv::GIT_DESCRIBE_ALWAYS, dirty_star(), branch.as_ref().unwrap_or(&"unknown branch"), benv::TARGET, target_cpu, benv::GENERATED_DATE_UTC)
    }
}

pub fn all_build_info_pairs() -> String {
    // channel matters
    // tagged status matters

    let mut pairs: Vec<(&&'static str, &Option<&'static str>)> = benv::BUILD_ENV_INFO.iter()
        .collect();
    pairs.sort_by(|a, b| {
        let a_lines = match *a.1 {
            Some(text) => text.lines().count(),
            None => 0,
        };
        let b_lines = match *b.1 {
            Some(text) => text.lines().count(),
            None => 0,
        };
        if a_lines > 1 || b_lines > 1 {
            match a_lines.cmp(&b_lines) {
                std::cmp::Ordering::Equal => a.0.cmp(b.0),
                other => other,
            }
        } else {
            a.0.cmp(b.0)
        }
    });

    let mut s = String::new();

    for (k, v) in pairs {
        let line = match *v {
            Some(ref s) => format!("{}={}\n", k, s),
            None => format!("{} (None)\n", k),
        };
        s += &line;
    }
    s
}