diff --git a/Cargo.lock b/Cargo.lock index e19764bb31a5..e000fada0e29 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4828,7 +4828,6 @@ dependencies = [ "web-sys", "web-time", "wgpu", - "xshell", ] [[package]] diff --git a/crates/re_build_tools/src/git.rs b/crates/re_build_tools/src/git.rs index 03031a73ff15..cc7ae4a8854d 100644 --- a/crates/re_build_tools/src/git.rs +++ b/crates/re_build_tools/src/git.rs @@ -43,7 +43,8 @@ pub fn rebuild_if_branch_or_commit_changes() { } } -pub fn commit_hash() -> anyhow::Result { +/// Get the full commit hash +pub fn git_commit_hash() -> anyhow::Result { let git_hash = run_command("git", &["rev-parse", "HEAD"])?; if git_hash.is_empty() { anyhow::bail!("empty commit hash"); @@ -51,7 +52,13 @@ pub fn commit_hash() -> anyhow::Result { Ok(git_hash) } -pub fn branch() -> anyhow::Result { +/// Get the first 7 characters of the commit hash +pub fn git_commit_short_hash() -> anyhow::Result { + Ok(git_commit_hash()?[0..7].to_string()) +} + +/// Get the current git branch name +pub fn git_branch() -> anyhow::Result { run_command("git", &["symbolic-ref", "--short", "HEAD"]) } diff --git a/crates/re_build_tools/src/lib.rs b/crates/re_build_tools/src/lib.rs index e929d18fee0a..0fc26a56d0be 100644 --- a/crates/re_build_tools/src/lib.rs +++ b/crates/re_build_tools/src/lib.rs @@ -14,6 +14,7 @@ mod rebuild_detector; pub(crate) use self::rebuild_detector::Packages; +pub use self::git::{git_branch, git_commit_hash, git_commit_short_hash}; pub use self::hashing::{ compute_crate_hash, compute_dir_filtered_hash, compute_dir_hash, compute_file_hash, compute_strings_hash, iter_dir, read_versioning_hash, write_versioning_hash, @@ -142,8 +143,14 @@ pub fn export_build_info_vars_for_crate(crate_name: &str) { } if export_git_info { - set_env("RE_BUILD_GIT_HASH", &git::commit_hash().unwrap_or_default()); - set_env("RE_BUILD_GIT_BRANCH", &git::branch().unwrap_or_default()); + set_env( + "RE_BUILD_GIT_HASH", + &git::git_commit_hash().unwrap_or_default(), + ); + set_env( + "RE_BUILD_GIT_BRANCH", + &git::git_branch().unwrap_or_default(), + ); // Make sure the above are up-to-date git::rebuild_if_branch_or_commit_changes(); @@ -249,3 +256,8 @@ fn rust_llvm_versions() -> anyhow::Result<(String, String)> { llvm_version.unwrap_or_else(|| "unknown".to_owned()), )) } + +/// Returns info parsed from an invocation of the `cargo metadata` command +pub fn cargo_metadata() -> anyhow::Result { + Ok(cargo_metadata::MetadataCommand::new().exec()?) +} diff --git a/crates/re_viewer/Cargo.toml b/crates/re_viewer/Cargo.toml index f4d19503cae0..eed9bb6601d0 100644 --- a/crates/re_viewer/Cargo.toml +++ b/crates/re_viewer/Cargo.toml @@ -104,10 +104,10 @@ wasm-bindgen-futures.workspace = true web-sys = { workspace = true, features = ["Window"] } [build-dependencies] +anyhow.workspace = true re_build_tools.workspace = true # External serde = { workspace = true, features = ["derive"] } serde_json.workspace = true serde_yaml.workspace = true -xshell.workspace = true diff --git a/crates/re_viewer/build.rs b/crates/re_viewer/build.rs index 21fc19a3d19a..9e1b3ebf4a8f 100644 --- a/crates/re_viewer/build.rs +++ b/crates/re_viewer/build.rs @@ -15,44 +15,6 @@ use std::path::Path; -use xshell::cmd; -use xshell::Shell; - -type AnyError = Box; -type Result = std::result::Result; - -#[derive(Debug)] -struct Error(String); - -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str(&self.0) - } -} - -impl std::error::Error for Error {} - -macro_rules! error { - ($lit:literal) => (Error($lit.to_owned())); - ($($tt:tt)*) => (Error(format!($($tt)*))); -} - -macro_rules! bail { - ($lit:literal) => (return Err(error!($lit))); - ($($tt:tt)*) => (return Err(error!($($tt)*).into())); -} - -fn git_branch_name(sh: &Shell) -> Result { - Ok(String::from_utf8( - cmd!(sh, "git rev-parse --abbrev-ref HEAD").output()?.stdout, - )?) -} - -fn git_short_hash(sh: &Shell) -> Result { - let full_hash = String::from_utf8(cmd!(sh, "git rev-parse HEAD").output()?.stdout)?; - Ok(full_hash.trim()[0..7].to_string()) -} - fn parse_release_version(branch: &str) -> Option<&str> { // release-\d+.\d+.\d+(-alpha.\d+)? @@ -136,7 +98,7 @@ struct Example { readme: Frontmatter, } -fn examples() -> Result> { +fn examples() -> anyhow::Result> { let mut examples = vec![]; let dir = "../../examples/python"; assert!(std::path::Path::new(dir).exists(), "Failed to find {dir}"); @@ -168,7 +130,7 @@ fn examples() -> Result> { Ok(examples) } -fn parse_frontmatter>(path: P) -> Result> { +fn parse_frontmatter>(path: P) -> anyhow::Result> { let path = path.as_ref(); let content = std::fs::read_to_string(path)?; let content = content.replace('\r', ""); // Windows, god damn you @@ -177,11 +139,11 @@ fn parse_frontmatter>(path: P) -> Result> { return Ok(None); }; let Some(end) = content.find("---") else { - bail!("{:?} has invalid frontmatter", path); + anyhow::bail!("{:?} has invalid frontmatter", path); }; Ok(Some(serde_yaml::from_str(&content[..end]).map_err( |e| { - error!( + anyhow::anyhow!( "failed to read {:?}: {e}", path.parent().unwrap().file_name().unwrap() ) @@ -189,32 +151,43 @@ fn parse_frontmatter>(path: P) -> Result> { )?)) } -fn get_base_url() -> Result { - let mut base_url = re_build_tools::get_and_track_env_var("EXAMPLES_MANIFEST_BASE_URL") - .unwrap_or_else(|_e| "https://demo.rerun.io/version/nightly".into()); - - if re_build_tools::is_on_ci() { - let sh = Shell::new()?; - let branch = git_branch_name(&sh)?; - // If we are on `main`, leave the base url at `version/nightly` - if branch != "main" { - if let Some(version) = parse_release_version(&branch) { - // In builds on `release-x.y.z` branches, use `version/{x.y.z}`. - base_url = format!("https://demo.rerun.io/version/{version}"); - } else { - // On any other branch, use `commit/{short_sha}`. - let sha = git_short_hash(&sh)?; - base_url = format!("https://demo.rerun.io/commit/{sha}"); - } - } +fn get_base_url() -> anyhow::Result { + if let Ok(base_url) = re_build_tools::get_and_track_env_var("EXAMPLES_MANIFEST_BASE_URL") { + // override via env var + return Ok(base_url); + } + + let branch = re_build_tools::git_branch()?; + if branch == "main" || !re_build_tools::is_on_ci() { + // on `main` and local builds, use `version/nightly` + // this will point to data uploaded by `.github/workflows/reusable_upload_web_demo.yml` + // on every commit to the `main` branch + return Ok("https://demo.rerun.io/version/nightly".into()); + } + + if parse_release_version(&branch).is_some() { + let metadata = re_build_tools::cargo_metadata()?; + let workspace_root = metadata + .root_package() + .ok_or_else(|| anyhow::anyhow!("failed to find workspace root"))?; + + // on `release-x.y.z` builds, use `version/{crate_version}` + // this will point to data uploaded by `.github/workflows/reusable_build_and_publish_web.yml` + return Ok(format!( + "https://demo.rerun.io/version/{}", + workspace_root.version + )); } - Ok(base_url) + // any other branch that is not `main`, use `commit/{sha}` + // this will point to data uploaded by `.github/workflows/reusable_upload_web_demo.yml` + let sha = re_build_tools::git_commit_short_hash()?; + Ok(format!("https://demo.rerun.io/commit/{sha}")) } const MANIFEST_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/data/examples_manifest.json"); -fn write_examples_manifest() -> Result<()> { +fn write_examples_manifest() -> anyhow::Result<()> { let base_url = get_base_url()?; let mut manifest = vec![];