mirror of
https://github.com/scurest/apicula.git
synced 2025-06-18 22:55:33 -04:00
Simplify access to commit/version info at compile time
This commit is contained in:
parent
3c003d3c54
commit
74db2cbc91
2
.github/workflows/cd.yaml
vendored
2
.github/workflows/cd.yaml
vendored
@ -19,6 +19,8 @@ jobs:
|
||||
|
||||
- name: Build
|
||||
run: |
|
||||
set APICULA_BUILD_COMMIT_HASH=(git log --pretty=format:'%h' -n 1)
|
||||
set APICULA_BUILD_COMMIT_DATE=(git log --pretty=format:'%cs' -n 1)
|
||||
cargo b --release
|
||||
target/release/apicula.exe -V
|
||||
|
||||
|
@ -3,7 +3,6 @@ name = "apicula"
|
||||
version = "0.1.1-dev"
|
||||
authors = ["scurest <scurest@users.noreply.github.com>"]
|
||||
license = "0BSD"
|
||||
build = "build.rs"
|
||||
|
||||
[dependencies]
|
||||
atty = "0.2"
|
||||
@ -16,8 +15,5 @@ termcolor = "0.3.5"
|
||||
time = "0.1.36"
|
||||
wild = "2.0.2"
|
||||
|
||||
[build-dependencies]
|
||||
time = "0.1.36"
|
||||
|
||||
[profile.release]
|
||||
panic = "abort"
|
||||
|
59
build.rs
59
build.rs
@ -1,59 +0,0 @@
|
||||
extern crate time;
|
||||
|
||||
use std::process::Command;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::path::Path;
|
||||
use std::io::Write;
|
||||
|
||||
fn main() {
|
||||
write_git_rev();
|
||||
write_compile_date();
|
||||
}
|
||||
|
||||
/// Write the current git hash to ${OUT_DIR}/git-commit
|
||||
/// so it's available to main.rs
|
||||
fn write_git_rev() {
|
||||
let out_dir = env::var("OUT_DIR").unwrap();
|
||||
let dst_path = Path::new(&out_dir).join("git-commit");
|
||||
let mut f = File::create(&dst_path).unwrap();
|
||||
|
||||
let commit_hash =
|
||||
Command::new("git")
|
||||
.args(&["rev-parse", "--short", "HEAD"])
|
||||
.output().unwrap();
|
||||
let changes_in_working_dir =
|
||||
Command::new("git")
|
||||
.args(&["status", "--porcelain"])
|
||||
.output().unwrap();
|
||||
|
||||
let was_error =
|
||||
!commit_hash.status.success() ||
|
||||
!changes_in_working_dir.status.success();
|
||||
|
||||
if was_error {
|
||||
f.write_all(b"unknown commit").unwrap();
|
||||
} else {
|
||||
let wip = !changes_in_working_dir.stdout.is_empty();
|
||||
|
||||
// Drop the trailing newline
|
||||
let hash = commit_hash.stdout.as_slice()
|
||||
.split_last().unwrap().1;
|
||||
|
||||
if wip {
|
||||
f.write_all(b"WIP ").unwrap();
|
||||
}
|
||||
f.write_all(hash).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
fn write_compile_date() {
|
||||
let out_dir = env::var("OUT_DIR").unwrap();
|
||||
let dst_path = Path::new(&out_dir).join("compile-date");
|
||||
let mut f = File::create(&dst_path).unwrap();
|
||||
|
||||
let now = time::now_utc();
|
||||
let date = time::strftime("%Y-%m-%d", &now).unwrap();
|
||||
|
||||
f.write_all(date.as_bytes()).unwrap();
|
||||
}
|
@ -6,6 +6,7 @@ use wild;
|
||||
mod parse;
|
||||
use self::parse::*;
|
||||
pub use self::parse::Args;
|
||||
use crate::version::print_version_info;
|
||||
|
||||
|
||||
pub fn parse_cli_args() -> Args {
|
||||
@ -96,7 +97,7 @@ static FORMAT_OPT: Opt = Opt {
|
||||
|
||||
|
||||
fn version() -> ! {
|
||||
println!("apicula {}", ::VERSION);
|
||||
print_version_info();
|
||||
exit(0)
|
||||
}
|
||||
|
||||
|
11
src/main.rs
11
src/main.rs
@ -32,19 +32,10 @@ mod primitives;
|
||||
mod skeleton;
|
||||
mod logger;
|
||||
mod connection;
|
||||
mod version;
|
||||
|
||||
use errors::Result;
|
||||
|
||||
// See build.rs.
|
||||
pub static VERSION: &'static str = concat!(
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
" (",
|
||||
include_str!(concat!(env!("OUT_DIR"), "/git-commit")),
|
||||
" ",
|
||||
include_str!(concat!(env!("OUT_DIR"), "/compile-date")),
|
||||
")",
|
||||
);
|
||||
|
||||
fn main() {
|
||||
let ret_code = match main2() {
|
||||
Ok(()) => 0,
|
||||
|
11
src/version.rs
Normal file
11
src/version.rs
Normal file
@ -0,0 +1,11 @@
|
||||
pub fn print_version_info() {
|
||||
println!("apicula {}", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
// These variables can optionally be set during the build process
|
||||
if let Some(info) = option_env!("APICULA_BUILD_COMMIT_HASH") {
|
||||
println!("build commit: {}", info);
|
||||
}
|
||||
if let Some(info) = option_env!("APICULA_BUILD_COMMIT_DATE") {
|
||||
println!("build commit date: {}", info);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user