~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/atlibs

0
0
main
47 lines · 2.0 KB Raw History
// GIT_SHA: the commit this CLI is built from (`atlibs new` pins the abi SDK at it), set only
// when this crate is that checkout's rust/cli. lexicons.rs: the repo's lexicons/ as (path, json), so
// `atlibs publish` validates records in any repo.

use std::path::{Path, PathBuf};
use std::process::Command;

fn git(args: &[&str]) -> Option<String> {
    let out = Command::new("git").args(args).output().ok()?;
    out.status.success().then(|| String::from_utf8_lossy(&out.stdout).trim().to_string())
}

fn walk(dir: &Path, out: &mut Vec<PathBuf>) {
    for entry in std::fs::read_dir(dir).unwrap_or_else(|e| panic!("{}: {e}", dir.display())) {
        let path = entry.unwrap().path();
        if path.is_dir() {
            walk(&path, out);
        } else if path.extension().is_some_and(|e| e == "json") {
            out.push(path);
        }
    }
}

fn main() {
    let here = std::fs::canonicalize(".").unwrap();
    let top = git(&["rev-parse", "--show-toplevel"]).and_then(|t| std::fs::canonicalize(t).ok());
    if top.is_some_and(|t| t.join("rust/cli") == here) {
        if let Some(sha) = git(&["rev-parse", "HEAD"]) {
            println!("cargo:rustc-env=GIT_SHA={sha}");
        }
        let head = git(&["symbolic-ref", "-q", "HEAD"]);
        for name in ["HEAD", "packed-refs"].into_iter().chain(head.as_deref()) {
            if let Some(path) = git(&["rev-parse", "--git-path", name]).filter(|p| Path::new(p).exists()) {
                println!("cargo:rerun-if-changed={path}");
            }
        }
    }

    let dir = here.join("../../lexicons");
    let mut files = vec![];
    walk(&dir, &mut files);
    files.sort();
    let entries: String = files.iter().map(|f| format!("({:?}, include_str!({:?})),\n", f.strip_prefix(&dir).unwrap(), f)).collect();
    std::fs::write(Path::new(&std::env::var("OUT_DIR").unwrap()).join("lexicons.rs"), format!("&[\n{entries}]\n")).unwrap();
    println!("cargo:rerun-if-changed={}", dir.display());
    println!("cargo:rerun-if-changed=build.rs");
}