Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix examples manifest version detection #3859

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

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

11 changes: 9 additions & 2 deletions crates/re_build_tools/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,22 @@ pub fn rebuild_if_branch_or_commit_changes() {
}
}

pub fn commit_hash() -> anyhow::Result<String> {
/// Get the full commit hash
pub fn git_commit_hash() -> anyhow::Result<String> {
let git_hash = run_command("git", &["rev-parse", "HEAD"])?;
if git_hash.is_empty() {
anyhow::bail!("empty commit hash");
}
Ok(git_hash)
}

pub fn branch() -> anyhow::Result<String> {
/// Get the first 7 characters of the commit hash
pub fn git_commit_short_hash() -> anyhow::Result<String> {
Ok(git_commit_hash()?[0..7].to_string())
}

/// Get the current git branch name
pub fn git_branch() -> anyhow::Result<String> {
run_command("git", &["symbolic-ref", "--short", "HEAD"])
}

Expand Down
16 changes: 14 additions & 2 deletions crates/re_build_tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<cargo_metadata::Metadata> {
Ok(cargo_metadata::MetadataCommand::new().exec()?)
}
2 changes: 1 addition & 1 deletion crates/re_viewer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
97 changes: 35 additions & 62 deletions crates/re_viewer/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,6 @@

use std::path::Path;

use xshell::cmd;
use xshell::Shell;

type AnyError = Box<dyn std::error::Error + Send + Sync + 'static>;
type Result<T, E = AnyError> = std::result::Result<T, E>;

#[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<String> {
Ok(String::from_utf8(
cmd!(sh, "git rev-parse --abbrev-ref HEAD").output()?.stdout,
)?)
}

fn git_short_hash(sh: &Shell) -> Result<String> {
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+)?

Expand Down Expand Up @@ -136,7 +98,7 @@ struct Example {
readme: Frontmatter,
}

fn examples() -> Result<Vec<Example>> {
fn examples() -> anyhow::Result<Vec<Example>> {
let mut examples = vec![];
let dir = "../../examples/python";
assert!(std::path::Path::new(dir).exists(), "Failed to find {dir}");
Expand Down Expand Up @@ -168,7 +130,7 @@ fn examples() -> Result<Vec<Example>> {
Ok(examples)
}

fn parse_frontmatter<P: AsRef<Path>>(path: P) -> Result<Option<Frontmatter>> {
fn parse_frontmatter<P: AsRef<Path>>(path: P) -> anyhow::Result<Option<Frontmatter>> {
let path = path.as_ref();
let content = std::fs::read_to_string(path)?;
let content = content.replace('\r', ""); // Windows, god damn you
Expand All @@ -177,44 +139,55 @@ fn parse_frontmatter<P: AsRef<Path>>(path: P) -> Result<Option<Frontmatter>> {
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()
)
},
)?))
}

fn get_base_url() -> Result<String> {
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<String> {
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![];
Expand Down
Loading