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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright (c) 2016, 2018 vergen developers
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

//! Defines the `vergen` function.
//!
//! `vergen`, when used in conjunction with the
//! [Build Scripts] support in
//! cargo, generates a file in `OUT_DIR` (defined by cargo) with up to 7 build
//! time constants.  This file can then be use with `include!` to pull the
//! constants into your source for use.
//!
//! [Build Scripts]: https://doc.rust-lang.org/cargo/reference/build-scripts.html
//!
//! # Example Cargo.toml
//! ```toml
//! [package]
//! #..
//! build = "build.rs"
//!
//! [dependencies]
//! #..
//!
//! [build-dependencies]
//! vergen = "1"
//! ```
//!
//! # Example `build.rs`
//! ```
//! extern crate vergen;
//!
//! # use std::env;
//! use vergen::{ConstantsFlags, COMPILE_TIME, Result, vergen};
//!
//! fn main() {
//! #   env::set_var("OUT_DIR", "target");
//!     let mut flags = ConstantsFlags::all();
//!     flags.toggle(COMPILE_TIME);
//!     vergen(flags).expect("Unable to generate constants!");
//! }
//! ```
//!
//! # Example `version.rs` (All Flags Enabled)
//! ```
//! /// Compile Time (UTC)
//! const COMPILE_TIME: &str = "2018-08-09T15:15:57.282334589+00:00";
//!
//! /// Compile Time - Short (UTC)
//! const COMPILE_TIME_SHORT: &str = "2018-08-09";
//!
//! /// Commit SHA
//! const SHA: &str = "75b390dc6c05a6a4aa2791cc7b3934591803bc22";
//!
//! /// Commit SHA - Short
//! const SHA_SHORT: &str = "75b390d";
//!
//! /// Commit Date
//! const COMMIT_DATE: &str = "'2018-08-08'";
//!
//! /// Target Triple
//! const TARGET_TRIPLE: &str = "x86_64-unknown-linux-gnu";
//!
//! /// Semver
//! const SEMVER: &str = "v0.1.0-pre.0";
//!
//! /// Semver (Lightweight)
//! const SEMVER_LIGHTWEIGHT: &str = "v0.1.0-pre.0";
//! ```
//!
//! # Include the constants in your code
//! ```ignore
//! include!(concat!(env!("OUT_DIR"), "/version.rs"));
//!
//! format!("{} {} blah {}", COMMIT_TIME, SHA, SEMVER)
//! # }
//! ```
#![deny(
    missing_docs,
    missing_debug_implementations,
    missing_copy_implementations,
    trivial_casts,
    trivial_numeric_casts,
    unsafe_code,
    unused_import_braces,
    unused_qualifications
)]
#[macro_use]
extern crate bitflags;
#[macro_use]
extern crate error_chain;

extern crate chrono;

mod error;

pub use error::Result;

use chrono::{DateTime, Utc};
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;

bitflags!(
    /// Constants Flags
    ///
    /// Use these to toggle off the generation of constants you won't use.
    ///
    /// ```
    /// # extern crate vergen;
    /// #
    /// # use vergen::*;
    /// #
    /// # fn foo() {
    /// let mut flags = ConstantsFlags::all();
    /// flags.toggle(SHA_SHORT);
    /// flags.toggle(COMMIT_DATE);
    ///
    /// assert_eq!(
    ///   flags,
    ///   COMPILE_TIME &
    ///   COMPILE_TIME_SHORT &
    ///   SHA &
    ///   TARGET_TRIPLE &
    ///   SEMVER &
    ///   SEMVER_LIGHTWEIGHT
    /// )
    /// # }
    /// ```
    pub struct ConstantsFlags: u32 {
        /// Generate the compile time constant.
        ///
        /// "2018-08-09T15:15:57.282334589+00:00"
        const COMPILE_TIME       = 0x0000_0001;
        /// Generate the compile date constant.
        ///
        /// "2018-08-09"
        const COMPILE_TIME_SHORT = 0x0000_0010;
        /// Generate the SHA constant.
        ///
        /// "75b390dc6c05a6a4aa2791cc7b3934591803bc22"
        const SHA                = 0x0000_0100;
        /// Generate the short SHA constant.
        ///
        /// "75b390d"
        const SHA_SHORT          = 0x0000_1000;
        /// Generate the commit date constant.
        ///
        /// "2018-08-08"
        const COMMIT_DATE        = 0x0001_0000;
        /// Generate the target triple constant.
        ///
        /// "x86_64-unknown-linux-gnu"
        const TARGET_TRIPLE      = 0x0010_0000;
        /// Generate the semver constant.
        ///
        /// This defaults to the output of `git describe`.  If that output is
        /// empty, the the `CARGO_PKG_VERSION` environment variable is used.
        ///
        /// "v0.1.0-pre.0"
        const SEMVER             = 0x0100_0000;
        /// Generate the semver constant, including lightweight tags.
        ///
        /// This defaults to the output of `git describe`.  If that output is
        /// empty, the the `CARGO_PKG_VERSION` environment variable is used.
        ///
        /// "v0.1.0-pre.0"
        const SEMVER_LIGHTWEIGHT = 0x0200_0000;
    }
);

const CONST_PREFIX: &str = "pub const ";
const CONST_TYPE: &str = ": &str = ";
const COMPILE_TIME_NAME: &str = "COMPILE_TIME";
const COMPILE_TIME_COMMENT: &str = "/// Compile Time (UTC)";
const COMPILE_TIME_SHORT_NAME: &str = "COMPILE_TIME_SHORT";
const COMPILE_TIME_SHORT_COMMENT: &str = "/// Compile Time - Short (UTC)";
const SHA_NAME: &str = "SHA";
const SHA_COMMENT: &str = "/// Commit SHA";
const SHA_SHORT_NAME: &str = "SHA_SHORT";
const SHA_SHORT_COMMENT: &str = "/// Commit SHA - Short";
const COMMIT_DATE_NAME: &str = "COMMIT_DATE";
const COMMIT_DATE_COMMENT: &str = "/// Commit Date";
const TARGET_TRIPLE_NAME: &str = "TARGET_TRIPLE";
const TARGET_TRIPLE_COMMENT: &str = "/// Target Triple";
const SEMVER_NAME: &str = "SEMVER";
const SEMVER_COMMENT: &str = "/// Semver";
const SEMVER_TAGS_NAME: &str = "SEMVER_LIGHTWEIGHT";
const SEMVER_TAGS_COMMENT: &str = "/// Semver (Lightweight)";

fn gen_const(f: &mut File, comment: &str, name: &str, value: &str) -> Result<()> {
    writeln!(
        f,
        "{}\n{}{}{}\"{}\";",
        comment, CONST_PREFIX, name, CONST_TYPE, value
    )?;
    Ok(())
}

fn run_command(command: &mut Command) -> String {
    let raw_output = if let Ok(o) = command.output() {
        String::from_utf8_lossy(&o.stdout).into_owned()
    } else {
        "Unknown".to_string()
    };
    raw_output.trim().to_string()
}

fn gen_compile_time(f: &mut File, now: DateTime<Utc>) -> Result<()> {
    gen_const(
        f,
        COMPILE_TIME_COMMENT,
        COMPILE_TIME_NAME,
        &now.to_rfc3339().to_string(),
    )
}

fn gen_compile_time_short(f: &mut File, now: DateTime<Utc>) -> Result<()> {
    gen_const(
        f,
        COMPILE_TIME_SHORT_COMMENT,
        COMPILE_TIME_SHORT_NAME,
        &now.format("%Y-%m-%d").to_string(),
    )
}

fn gen_sha(f: &mut File) -> Result<()> {
    let sha = run_command(Command::new("git").args(&["rev-parse", "HEAD"]));
    gen_const(f, SHA_COMMENT, SHA_NAME, &sha)
}

fn gen_short_sha(f: &mut File) -> Result<()> {
    let sha = run_command(Command::new("git").args(&["rev-parse", "--short", "HEAD"]));
    gen_const(f, SHA_SHORT_COMMENT, SHA_SHORT_NAME, &sha)
}

fn gen_commit_date(f: &mut File) -> Result<()> {
    let commit_date = run_command(Command::new("git").args(&[
        "log",
        "--pretty=format:'%ad'",
        "-n1",
        "--date=short",
    ]));
    gen_const(f, COMMIT_DATE_COMMENT, COMMIT_DATE_NAME, &commit_date)
}

fn gen_target(f: &mut File) -> Result<()> {
    gen_const(
        f,
        TARGET_TRIPLE_COMMENT,
        TARGET_TRIPLE_NAME,
        &env::var("TARGET").unwrap_or_else(|_| "UNKNOWN".to_string()),
    )
}

fn gen_semver(f: &mut File) -> Result<()> {
    let describe = run_command(Command::new("git").args(&["describe"]));

    let semver = if describe.is_empty() {
        env::var("CARGO_PKG_VERSION")?
    } else {
        describe
    };
    gen_const(f, SEMVER_COMMENT, SEMVER_NAME, &semver)
}

fn gen_semver_tags(f: &mut File) -> Result<()> {
    let describe = run_command(Command::new("git").args(&["describe", "--tags"]));

    let semver = if describe.is_empty() {
        env::var("CARGO_PKG_VERSION")?
    } else {
        describe
    };
    gen_const(f, SEMVER_TAGS_COMMENT, SEMVER_TAGS_NAME, &semver)
}
/// Create a `version.rs` file in `OUT_DIR`, and write up to 7 constants into
/// it.
///
/// # Example build.rs
/// ```
/// # extern crate vergen;
/// #
/// # use std::env;
/// # use vergen::{ConstantsFlags, COMPILE_TIME, Result, vergen};
/// #
/// fn main() {
/// #   env::set_var("OUT_DIR", "target");
///     let mut flags = ConstantsFlags::all();
///     flags.toggle(COMPILE_TIME);
///     vergen(flags).expect("Unable to generate constants!");
/// }
/// ```
///
/// # Example Output (All Flags Enabled)
/// ```
/// /// Compile Time (UTC)
/// const COMPILE_TIME: &str = "2018-08-09T15:15:57.282334589+00:00";
///
/// /// Compile Time - Short (UTC)
/// const COMPILE_TIME_SHORT: &str = "2018-08-09";
///
/// /// Commit SHA
/// const SHA: &str = "75b390dc6c05a6a4aa2791cc7b3934591803bc22";
///
/// /// Commit SHA - Short
/// const SHA_SHORT: &str = "75b390d";
///
/// /// Commit Date
/// const COMMIT_DATE: &str = "'2018-08-08'";
///
/// /// Target Triple
/// const TARGET_TRIPLE: &str = "x86_64-unknown-linux-gnu";
///
/// /// Semver
/// const SEMVER: &str = "v0.1.0-pre.0";
///
/// /// Semver (Lightweight)
/// pub const SEMVER_LIGHTWEIGHT: &str = "v0.1.0-pre.0";
/// ```
pub fn vergen(flags: ConstantsFlags) -> Result<()> {
    let dst = PathBuf::from(env::var("OUT_DIR")?);
    let mut f = File::create(&dst.join("version.rs"))?;
    let now = Utc::now();
    let mut first = true;

    if flags.contains(COMPILE_TIME) {
        gen_compile_time(&mut f, now)?;
        first = false
    }

    if flags.contains(COMPILE_TIME_SHORT) {
        if !first {
            writeln!(f);
        }
        gen_compile_time_short(&mut f, now)?;
        first = false;
    }

    if flags.contains(SHA) {
        if !first {
            writeln!(f);
        }
        gen_sha(&mut f)?;
        first = false;
    }

    if flags.contains(SHA_SHORT) {
        if !first {
            writeln!(f);
        }
        gen_short_sha(&mut f)?;
        first = false;
    }

    if flags.contains(COMMIT_DATE) {
        if !first {
            writeln!(f);
        }
        gen_commit_date(&mut f)?;
        first = false;
    }

    if flags.contains(TARGET_TRIPLE) {
        if !first {
            writeln!(f);
        }
        gen_target(&mut f)?;
        first = false;
    }

    if flags.contains(SEMVER) {
        if !first {
            writeln!(f);
        }
        gen_semver(&mut f)?;
    }

    if flags.contains(SEMVER_LIGHTWEIGHT) {
        if !first {
            writeln!(f);
        }
        gen_semver_tags(&mut f)?;
    }

    Ok(())
}