From a9f8975ed381a7d5aec41584c840f70a056828cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= <123550+andresilva@users.noreply.github.com> Date: Fri, 31 May 2024 09:20:29 +0100 Subject: [PATCH] allow reading the timestamp for build date from SOURCE_DATE_EPOCH (#95) --- Cargo.lock | 3 +++ cli/Cargo.toml | 3 +++ cli/build.rs | 26 ++++++++++---------------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2fbf1ab..49aec8a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -608,7 +608,9 @@ checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" dependencies = [ "android-tzdata", "iana-time-zone", + "js-sys", "num-traits", + "wasm-bindgen", "windows-targets 0.52.5", ] @@ -4188,6 +4190,7 @@ name = "subwasm" version = "0.21.2" dependencies = [ "assert_cmd", + "chrono", "clap", "color-eyre", "env_logger 0.11.3", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 759eb89..daa22ab 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -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 = [ diff --git a/cli/build.rs b/cli/build.rs index aab38ba..5086e54 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -1,3 +1,5 @@ +use chrono::{DateTime, Utc}; + use std::{borrow::Cow, process::Command}; fn main() { @@ -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::().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() {