Sweep every first-party crate source (1956 .rs files) to the project comment guidelines: delete redundant restatements, decorative banners, change narration, and end-of-line comments; keep and tighten the crucial ones (invariants, bug rationale, SAFETY blocks, ported-source attribution). No functional code changed. Every edit is proven comment-only against the prior tree by a comment-stripping lexer (string/char/raw-string aware) plus a separate doctest-fence check. Where removing a comment made rustfmt or clippy want to re-lay-out adjacent code, the minimal triggering comment is restored so code tokens stay byte-identical. Gates green: cargo fmt --all --check (0 diffs), cargo check and cargo clippy --workspace --all-targets (0 warnings). Adds scripts/check_codegen_comment_guidelines.py — the enforcement gate for these guidelines (flags banners, end-of-line comments, change narration, and commented-out code).
175 lines
6.3 KiB
Rust
175 lines
6.3 KiB
Rust
//! Embeds the search-tool binaries the crate self-extracts at runtime.
|
|
//!
|
|
//! ripgrep is auto-downloaded for release builds; any tool can also be
|
|
//! supplied out-of-band through `KIGI_TOOLS_BUNDLE_<NAME>_PATH`, which forces
|
|
//! bundling regardless of profile or target.
|
|
use std::env;
|
|
use std::fs;
|
|
use std::io;
|
|
use std::path::PathBuf;
|
|
|
|
const RG_VER: &str = "15.0.0";
|
|
const BFS_VER: &str = "4.1";
|
|
const UGREP_VER: &str = "7.7.0";
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
bundle_rg()?;
|
|
// bfs/ugrep back the bash-harness find/grep shadows (embedded_search_tools).
|
|
bundle_search_tool("bfs", "BFS", BFS_VER)?;
|
|
bundle_search_tool("ugrep", "UGREP", UGREP_VER)?;
|
|
Ok(())
|
|
}
|
|
|
|
/// Bundle a prebuilt **static** `bfs`/`ugrep` binary, emitting
|
|
/// `cfg(bundle_<name>)` so the crate's `include_bytes!` + self-extract engages.
|
|
///
|
|
/// No auto-download (unlike ripgrep): bfs/ugrep publish no prebuilt static
|
|
/// release assets, so the release pipeline supplies the path. Unset → not
|
|
/// bundled (the runtime resolver falls back to `~/.kigi/vendor` / `$PATH`);
|
|
/// never a hard failure, so an un-wired build still succeeds.
|
|
fn bundle_search_tool(
|
|
name: &str,
|
|
name_uc: &str,
|
|
ver: &str,
|
|
) -> Result<(), Box<dyn std::error::Error>> {
|
|
let override_env = format!("KIGI_TOOLS_BUNDLE_{name_uc}_PATH");
|
|
println!("cargo:rerun-if-env-changed={override_env}");
|
|
// Declared unconditionally so `#[cfg(bundle_<name>)]` stays lint-clean on
|
|
// the paths that never emit it.
|
|
println!("cargo:rustc-check-cfg=cfg(bundle_{name})");
|
|
|
|
// The consumer (`embedded_search_tools`) is `#[cfg(unix)]`, so embedding on a
|
|
// Windows target is dead weight.
|
|
if env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("windows") {
|
|
return Ok(());
|
|
}
|
|
|
|
let Some(src) = env::var(&override_env).ok().filter(|s| !s.is_empty()) else {
|
|
return Ok(());
|
|
};
|
|
|
|
let gen_dir = PathBuf::from(env::var("OUT_DIR")?).join(format!("bundle-{name}"));
|
|
fs::create_dir_all(&gen_dir)?;
|
|
let dest = gen_dir.join(format!("{name}-{ver}-override.bin"));
|
|
let _ = fs::remove_file(&dest);
|
|
fs::copy(&src, &dest)
|
|
.map_err(|e| format!("copy {override_env} from {src} to {}: {e}", dest.display()))?;
|
|
|
|
println!("cargo:rustc-cfg=bundle_{name}");
|
|
println!("cargo:rustc-env=KIGI_TOOLS_{name_uc}_VER={ver}");
|
|
println!("cargo:rustc-env=KIGI_TOOLS_{name_uc}_TARGET=override");
|
|
Ok(())
|
|
}
|
|
|
|
/// Download + embed ripgrep. Kept out of `main` so its early returns do not
|
|
/// skip the other search tools.
|
|
fn bundle_rg() -> Result<(), Box<dyn std::error::Error>> {
|
|
println!("cargo:rerun-if-env-changed=KIGI_TOOLS_BUNDLE_RG_PATH");
|
|
println!("cargo:rustc-check-cfg=cfg(bundle_rg)");
|
|
|
|
let gen_dir = PathBuf::from(env::var("OUT_DIR")?).join("bundle-rg");
|
|
fs::create_dir_all(&gen_dir)?;
|
|
|
|
// Debug builds skip the download so `cargo check` stays fast.
|
|
let path_override = env::var("KIGI_TOOLS_BUNDLE_RG_PATH").ok();
|
|
let is_release = env::var("PROFILE").as_deref() == Ok("release");
|
|
if path_override.is_none() && !is_release {
|
|
return Ok(());
|
|
}
|
|
|
|
// ripgrep ships .zip on Windows and there is no zip-extraction path here.
|
|
// Returning BEFORE emitting `cargo:rustc-cfg=bundle_rg` compiles out the
|
|
// gated `include_bytes!`, so the runtime falls back to `rg` on PATH
|
|
// (installed separately via winget / scoop).
|
|
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
|
|
if target_os == "windows" && path_override.is_none() {
|
|
return Ok(());
|
|
}
|
|
|
|
println!("cargo:rustc-cfg=bundle_rg");
|
|
println!("cargo:rustc-env=KIGI_TOOLS_RG_VER={}", RG_VER);
|
|
|
|
if let Some(path) = path_override {
|
|
let dest = gen_dir.join(format!("rg-{}-override.bin", RG_VER));
|
|
println!("cargo:rustc-env=KIGI_TOOLS_RG_TARGET=override");
|
|
let _ = fs::remove_file(&dest);
|
|
fs::copy(PathBuf::from(path.clone()), &dest).map_err(|e| {
|
|
format!(
|
|
"Failed copying KIGI_TOOLS_BUNDLE_RG_PATH: {e} from path {path} to dest {}",
|
|
dest.display()
|
|
)
|
|
})?;
|
|
return Ok(());
|
|
}
|
|
|
|
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
|
|
let asset_triple = match (target_os.as_str(), target_arch.as_str()) {
|
|
("macos", "aarch64") => "aarch64-apple-darwin",
|
|
("macos", "x86_64") => "x86_64-apple-darwin",
|
|
("linux", "x86_64") => "x86_64-unknown-linux-musl",
|
|
("linux", "aarch64") => "aarch64-unknown-linux-gnu",
|
|
_ => {
|
|
return Err(format!(
|
|
"Unsupported target for ripgrep bundling: {os}-{arch}. Set KIGI_TOOLS_BUNDLE_RG_PATH to a local rg binary for offline or unsupported builds.",
|
|
os = target_os,
|
|
arch = target_arch
|
|
).into());
|
|
}
|
|
};
|
|
|
|
println!("cargo:rustc-env=KIGI_TOOLS_RG_TARGET={}", asset_triple);
|
|
let dest = gen_dir.join(format!("rg-{}-{}.bin", RG_VER, asset_triple));
|
|
let _ = fs::remove_file(&dest);
|
|
|
|
let url = format!(
|
|
"https://github.com/BurntSushi/ripgrep/releases/download/{v}/ripgrep-{v}-{t}.tar.gz",
|
|
v = RG_VER,
|
|
t = asset_triple
|
|
);
|
|
|
|
let bytes: Vec<u8> = {
|
|
let resp = reqwest::blocking::get(&url).map_err(|e| {
|
|
format!(
|
|
"Failed to download ripgrep: {}\nSet KIGI_TOOLS_BUNDLE_RG_PATH to a local rg for offline builds.",
|
|
e
|
|
)
|
|
})?;
|
|
if !resp.status().is_success() {
|
|
return Err(format!(
|
|
"HTTP {} downloading ripgrep. Set KIGI_TOOLS_BUNDLE_RG_PATH for offline builds.",
|
|
resp.status()
|
|
)
|
|
.into());
|
|
}
|
|
resp.bytes()?.to_vec()
|
|
};
|
|
|
|
let gz = flate2::read::GzDecoder::new(&bytes[..]);
|
|
let mut ar = tar::Archive::new(gz);
|
|
let mut found = false;
|
|
for entry in ar.entries()? {
|
|
let mut e = entry?;
|
|
let p = e.path()?;
|
|
if p.file_name().is_some_and(|n| n == "rg") {
|
|
let data: Vec<u8> = {
|
|
let mut v = Vec::new();
|
|
io::copy(&mut e, &mut v)?;
|
|
v
|
|
};
|
|
fs::write(&dest, &data)?;
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if !found {
|
|
return Err(format!(
|
|
"Could not find 'rg' in ripgrep archive {}. Set KIGI_TOOLS_BUNDLE_RG_PATH for offline builds.",
|
|
url
|
|
)
|
|
.into());
|
|
}
|
|
|
|
Ok(())
|
|
}
|