#![allow(clippy::missing_panics_doc, clippy::must_use_candidate)]
use std::path::PathBuf;
use heel::HeelBinary;
use serde_json::Value;
use crate::{HostProgram, ToolDistribution, ToolRuntimeError, ToolTarget};
fn pinned_manifest() -> Value {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../patch/third-party-tools.json");
serde_json::from_slice(&std::fs::read(path).expect("the repository pins its tools"))
.expect("the pinned tools are JSON")
}
pub fn host_programs() -> Vec<Value> {
pinned_manifest()["host_programs"]
.as_array()
.expect("the pinned tools list host programs")
.iter()
.filter(|program| program["tool"] == "heel")
.cloned()
.collect()
}
pub fn distribution() -> ToolDistribution {
let distribution = ToolDistribution::from_json(
&serde_json::to_vec(&serde_json::json!({
"artifacts": [],
"host_programs": host_programs(),
}))
.expect("the manifest serializes"),
)
.expect("the pinned heel is a valid manifest");
match std::env::var("GITHUB_TOKEN") {
Ok(token) => distribution.with_github_token(token),
Err(_) => distribution,
}
}
pub async fn pinned() -> Result<HeelBinary, ToolRuntimeError> {
let target_dir = std::env::var_os("CARGO_TARGET_DIR").map_or_else(
|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../target"),
PathBuf::from,
);
let heel = distribution()
.prepare_host_program(
HostProgram::Heel,
&ToolTarget::native()?,
&target_dir.join("aither-test-tools"),
)
.await?;
let path = heel.path().to_path_buf();
Ok(blocking::unblock(move || HeelBinary::verify(path)).await?)
}