use anyhow::{Context, Result};
use std::path::PathBuf;
const WRAPPER_JAR: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/wrapper.jar"));
include!(concat!(env!("OUT_DIR"), "/wrapper_sha8.rs"));
pub fn ensure() -> Result<PathBuf> {
let cache = dirs::cache_dir()
.context("could not determine user cache directory")?
.join("curie");
let path = cache.join(format!(
"wrapper-{}-{}.jar",
env!("CARGO_PKG_VERSION"),
WRAPPER_JAR_SHA8,
));
if path.exists() {
return Ok(path);
}
std::fs::create_dir_all(&cache)
.with_context(|| format!("failed to create {}", cache.display()))?;
let tmp = path.with_extension("jar.part");
std::fs::write(&tmp, WRAPPER_JAR)
.with_context(|| format!("failed to write {}", tmp.display()))?;
std::fs::rename(&tmp, &path)
.with_context(|| format!("failed to rename {} → {}", tmp.display(), path.display()))?;
Ok(path)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wrapper_jar_is_nonempty() {
assert!(WRAPPER_JAR.len() > 256, "embedded wrapper.jar suspiciously small");
assert_eq!(&WRAPPER_JAR[..4], b"PK\x03\x04", "not a valid ZIP/JAR header");
}
#[test]
fn ensure_returns_existing_path_on_second_call() {
let first = ensure().unwrap();
let second = ensure().unwrap();
assert_eq!(first, second);
assert!(first.exists());
}
}