Skip to content

Commit

Permalink
Print commit hash to console in wasm demo
Browse files Browse the repository at this point in the history
  • Loading branch information
ictrobot committed Oct 28, 2024
1 parent b1558e0 commit 3194823
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions crates/aoc_wasm/web/web.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {Aoc} from "./aoc.mjs";

console.log("Commit", "${GIT_COMMIT}");

const MODULE_PATHS = [
"./aoc-simd128.wasm",
"./aoc.wasm",
Expand Down
36 changes: 35 additions & 1 deletion crates/xtask/src/cmd/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::common::{
use std::error::Error;
use std::fs;
use std::path::Path;
use std::process::Command;
use std::str;
use utils::md5;

Expand All @@ -23,7 +24,14 @@ pub fn main(args: impl Iterator<Item = String>) -> Result<(), Box<dyn Error>> {
run_cargo(&["clean", "--doc"], &[])?;
run_cargo(&["doc", "--no-deps"], &[])?;

let mut rewrites = vec![];
let mut rewrites = vec![(
"\"${GIT_COMMIT}\"".to_string(),
get_commit_commit().unwrap_or_else(|e| {
println!("failed to get git commit: {e}");
"\"unknown\"".to_string()
}),
)];

for (env, extra_args, name) in [
(&[][..], &[][..], "aoc.wasm"),
(
Expand Down Expand Up @@ -123,3 +131,29 @@ fn add_rewrite(rewrites: &mut Vec<(String, String)>, path: &Path) -> Result<(),

Ok(())
}

fn get_commit_commit() -> Result<String, Box<dyn Error>> {
let hash = {
let output = Command::new("git").args(["rev-parse", "HEAD"]).output()?;
if !output.status.success() {
return Err(format!("'git rev-parse HEAD' exited with {}", output.status).into());
}
String::from_utf8(output.stdout)?
};

let modified = {
let output = Command::new("git")
.args(["status", "--porcelain"])
.output()?;
if !output.status.success() {
return Err(format!("'git status' exited with {}", output.status).into());
}
!output.stdout.is_empty()
};

if modified {
Ok(format!("\"{} (modified)\"", hash.trim()))
} else {
Ok(format!("\"{}\"", hash.trim()))
}
}

0 comments on commit 3194823

Please sign in to comment.