Skip to content

Commit

Permalink
allow reading the timestamp for build date from SOURCE_DATE_EPOCH (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
andresilva authored May 31, 2024
1 parent da51bd3 commit a9f8975
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ repository = "https://github.com/chevdor/subwasm"
name = "subwasm"
path = "src/main.rs"

[build-dependencies]
chrono = "0.4"

[dependencies]
assert_cmd = "2.0"
clap = { version = "4.0", features = [
Expand Down
26 changes: 10 additions & 16 deletions cli/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use chrono::{DateTime, Utc};

use std::{borrow::Cow, process::Command};

fn main() {
Expand All @@ -11,23 +13,15 @@ pub fn generate_cargo_keys() {
}

pub fn generate_cargo_key_build_date() {
let build_date = match Command::new("date").args(["-u", "+%FT%TZ"]).output() {
Ok(o) if o.status.success() => {
let sha = String::from_utf8_lossy(&o.stdout).trim().to_owned();
Cow::from(sha)
}
Ok(o) => {
let status = o.status;
println!("cargo:warning=Failed fetching the date timestamp: {status}");
Cow::from("unknown")
}
Err(err) => {
println!("cargo:warning=Failed fetching the datge: {err}");
Cow::from("unknown")
}
};
let build_date = std::env::var("SOURCE_DATE_EPOCH")
.ok()
.and_then(|ts| ts.parse::<i64>().ok())
.and_then(|ts| DateTime::from_timestamp(ts, 0))
.unwrap_or_else(|| Utc::now());

let formatted_build_date = build_date.format("%Y-%m-%dT%H:%M:%SZ").to_string();

println!("cargo:rustc-env=SUBWASM_CLI_BUILD_DATE={build_date}");
println!("cargo:rustc-env=SUBWASM_CLI_BUILD_DATE={formatted_build_date}");
}

pub fn generate_cargo_key_git() {
Expand Down

0 comments on commit a9f8975

Please sign in to comment.