rustyhorde / vergen
1
// Copyright (c) 2016, 2018 vergen developers
2
//
3
// Licensed under the Apache License, Version 2.0
4
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6
// option. All files in the project carrying such notice may not be copied,
7
// modified, or distributed except according to those terms.
8

9
//! Geneer
10
//! the `include!` macro within your project.
11
use crate::constants::{ConstantsFlags, CONST_PREFIX, CONST_TYPE};
12
use crate::output::generate_build_info;
13
use std::env;
14
use std::fs::File;
15
use std::io::Write;
16
use std::path::PathBuf;
17

18
use super::Result;
19

20 2
fn gen_const<W: Write>(f: &mut W, comment: &str, name: &str, value: &str) -> Result<()> {
21 2
    writeln!(
22 2
        f,
23 1
        "{}\n{}{}{}\"{}\";",
24 1
        comment, CONST_PREFIX, name, CONST_TYPE, value
25 2
    )?;
26 2
    Ok(())
27 2
}
28

29
/// Create a `version.rs` file in `OUT_DIR` and write the toggled on constants
30
/// to the file.
31
///
32
/// # Example build.rs
33
/// ```
34
/// # extern crate vergen;
35
/// #
36
/// # use std::env;
37
/// # use vergen::{ConstantsFlags, generate_version_rs};
38
/// #
39
/// fn main() {
40
/// #   env::set_var("OUT_DIR", "target");
41
///     let mut flags = ConstantsFlags::all();
42
///     flags.toggle(ConstantsFlags::BUILD_TIMESTAMP);
43
///     generate_version_rs(flags).expect("Unable to generate constants!");
44
/// }
45
/// ```
46
///
47
/// # Example Output (All Flags Enabled)
48
/// ```
49
/// /// Build Timestamp (UTC)
50
/// pub const VERGEN_BUILD_TIMESTAMP: &str = "2018-08-09T15:15:57.282334589+00:00";
51
///
52
/// /// Build Date - Short (UTC)
53
/// pub const VERGEN_BUILD_DATE: &str = "2018-08-09";
54
///
55
/// /// Commit SHA
56
/// pub const VERGEN_SHA: &str = "75b390dc6c05a6a4aa2791cc7b3934591803bc22";
57
///
58
/// /// Commit SHA - Short
59
/// pub const VERGEN_SHA_SHORT: &str = "75b390d";
60
///
61
/// /// Commit Date
62
/// pub const VERGEN_COMMIT_DATE: &str = "'2018-08-08'";
63
///
64
/// /// Target Triple
65
/// pub const VERGEN_TARGET_TRIPLE: &str = "x86_64-unknown-linux-gnu";
66
///
67
/// /// Semver
68
/// pub const VERGEN_SEMVER: &str = "v0.1.0-pre.0";
69
///
70
/// /// Semver (Lightweight)
71
/// pub const VERGEN_SEMVER_LIGHTWEIGHT: &str = "v0.1.0-pre.0";
72
/// ```
73
///
74
/// ## Include the constants in your code (Version 1.x.x only)
75
/// ```ignore
76
/// include!(concat!(env!("OUT_DIR"), "/version.rs"));
77
///
78
/// format!("{} {} blah {}", VERGEN_BUILD_TIMESTAMP, VERGEN_SHA, VERGEN_SEMVER)
79
/// ```
80
#[deprecated(since = "2.0.0", note = "Please use `generate_cargo_keys` instead")]
81
pub fn generate_version_rs(flags: ConstantsFlags) -> Result<()> {
82
    let dst = PathBuf::from(env::var("OUT_DIR")?);
83
    let mut f = File::create(&dst.join("version.rs"))?;
84
    let build_info = generate_build_info(flags)?;
85

86
    for (k, v) in build_info {
87
        gen_const(&mut f, k.comment(), k.name(), &v)?;
88
        writeln!(f)?;
89
    }
90

91
    Ok(())
92
}
93

94
#[cfg(test)]
95
mod test {
96
    use super::gen_const;
97
    use crate::constants::ConstantsFlags;
98
    use crate::output::generate_build_info;
99
    use regex::Regex;
100
    use std::io::Cursor;
101

102
    lazy_static! {
103
        static ref CONST_RE: Regex =
104 2
            Regex::new(r#"^/// .*[\r\n]+pub const [A-Z_]+: \&str = ".*";[\r\n]+$"#)
105
                .expect("Unable to create const regex");
106
    }
107

108
    #[test]
109 2
    fn gen_const_output() {
110 2
        let flags = ConstantsFlags::all();
111 2
        let build_info = generate_build_info(flags).expect("Unable to generate build_info map!");
112

113 2
        for (k, v) in build_info {
114 2
            let buffer = Vec::new();
115 2
            let mut cursor = Cursor::new(buffer);
116 2
            gen_const(&mut cursor, k.comment(), k.name(), &v)
117
                .expect("Unable to generate const string");
118 2
            let const_str = String::from_utf8_lossy(&cursor.get_ref());
119 2
            assert!(CONST_RE.is_match(&const_str));
120 2
        }
121 2
    }
122
}

Read our documentation on viewing source code .

Loading