Skip to content

Commit 1232d00

Browse files
OttoAllmendingerllm-git
andcommitted
feat(wasm-utxo): add build script to embed version info at compile time
Add a build script that extracts the version from package.json and captures the git commit hash during build. These values are made available as environment variables in the Rust code, providing better traceability for deployments. Also update GitHub workflows to use fetch-depth: 0 to ensure git history is available for the build script. Issue: BTC-2992 Co-authored-by: llm-git <llm-git@ttll.de>
1 parent 50a5152 commit 1232d00

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

.github/workflows/build-and-test.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ jobs:
2727
- uses: actions/checkout@v4
2828
with:
2929
ref: ${{ github.event.pull_request.head.sha || github.sha }}
30+
fetch-depth: 0
3031

3132
- name: Install Rust
3233
uses: dtolnay/rust-toolchain@e97e2d8cc328f1b50210efc529dca0028893a2d9 # v1
@@ -122,6 +123,7 @@ jobs:
122123
- uses: actions/checkout@v4
123124
with:
124125
ref: ${{ github.event.pull_request.head.sha || github.sha }}
126+
fetch-depth: 0
125127

126128
- name: Install Rust
127129
uses: dtolnay/rust-toolchain@e97e2d8cc328f1b50210efc529dca0028893a2d9 # v1
@@ -186,6 +188,7 @@ jobs:
186188
- uses: actions/checkout@v4
187189
with:
188190
ref: ${{ github.event.pull_request.head.sha || github.sha }}
191+
fetch-depth: 0
189192

190193
- name: Setup Node
191194
uses: actions/setup-node@v4

packages/wasm-utxo/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ pastey = "0.1"
3434
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
3535
zebra-chain = { version = "3.1", default-features = false }
3636

37+
[build-dependencies]
38+
serde_json = "1.0"
39+
3740
[profile.release]
3841
# this is required to make webpack happy
3942
# https://github.com/webpack/webpack/issues/15566#issuecomment-2558347645

packages/wasm-utxo/build.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::process::Command;
2+
3+
fn main() {
4+
// Extract version from package.json using proper JSON parsing
5+
let package_json =
6+
std::fs::read_to_string("package.json").expect("Failed to read package.json");
7+
8+
let package: serde_json::Value =
9+
serde_json::from_str(&package_json).expect("Failed to parse package.json as JSON");
10+
11+
let version = package
12+
.get("version")
13+
.and_then(|v| v.as_str())
14+
.expect("Failed to find 'version' field in package.json");
15+
16+
println!("cargo:rustc-env=WASM_UTXO_VERSION={}", version);
17+
18+
// Capture git commit hash
19+
let git_hash = Command::new("git")
20+
.args(["rev-parse", "HEAD"])
21+
.output()
22+
.ok()
23+
.and_then(|output| {
24+
if output.status.success() {
25+
String::from_utf8(output.stdout).ok()
26+
} else {
27+
None
28+
}
29+
})
30+
.map(|s| s.trim().to_string())
31+
.unwrap_or_else(|| "unknown".to_string());
32+
33+
println!("cargo:rustc-env=WASM_UTXO_GIT_HASH={}", git_hash);
34+
35+
// Rerun if package.json changes
36+
println!("cargo:rerun-if-changed=package.json");
37+
}

0 commit comments

Comments
 (0)