use std::process::Command;
use std::sync::{Mutex, OnceLock};
use openmoat_hosts::Host;
use super::host_version::{Version, parse, run_version};
pub fn blocks_on_failure(host: Host) -> bool {
static ASKED: OnceLock<Mutex<Vec<(Host, bool)>>> = OnceLock::new();
let Some(since) = since(host) else {
return false;
};
let asked = ASKED.get_or_init(|| Mutex::new(Vec::new()));
if let Some((_, known)) = asked
.lock()
.ok()
.and_then(|a| a.iter().find(|(h, _)| *h == host).copied())
{
return known;
}
let mut versions: Vec<Option<Version>> = super::bundled::copies(host)
.iter()
.map(|c| Some(c.version))
.collect();
on_path(host, &mut versions);
let blocks = all_read(&versions, since);
if let Ok(mut asked) = asked.lock() {
asked.push((host, blocks));
}
blocks
}
pub fn since(host: Host) -> Option<Version> {
let spec = host.spec();
spec.install
.as_ref()
.is_some_and(|i| i.hooks.iter().any(|h| h.block_on_failure))
.then_some(spec.hook_failure.flag_since())
.flatten()
}
pub fn since_text(host: Host) -> String {
since(host).map_or_else(String::new, |(major, minor, patch)| {
format!("{major}.{minor}.{patch}")
})
}
pub fn reads_flag(host: Host, version: Version) -> bool {
since(host).is_some_and(|since| version >= since)
}
fn all_read(versions: &[Option<Version>], since: Version) -> bool {
!versions.is_empty() && versions.iter().all(|v| v.is_some_and(|v| v >= since))
}
#[cfg(debug_assertions)]
pub fn version_variable(host: Host) -> String {
format!(
"MOAT_{}_VERSION",
host.id().to_ascii_uppercase().replace('-', "_")
)
}
fn on_path(host: Host, found: &mut Vec<Option<Version>>) {
#[cfg(debug_assertions)]
if let Some(value) = std::env::var_os(version_variable(host)) {
let value = value.to_string_lossy();
if !value.trim().is_empty() {
found.push(parse(&value));
}
return;
}
for name in host.spec().process.names {
if let Some(program) = crate::environment::find_on_path(name) {
found.push(run_version(Command::new(program)).and_then(|text| parse(&text)));
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn flagged() -> Host {
Host::ALL.into_iter().find(|h| since(*h).is_some()).unwrap()
}
#[test]
fn only_the_version_the_spec_names_and_later_read_the_flag() {
let host = flagged();
let (major, minor, patch) = since(host).unwrap();
assert!(reads_flag(host, (major, minor, patch)));
assert!(reads_flag(host, (major, minor + 1, 0)));
assert!(reads_flag(host, (major + 1, 0, 0)));
assert!(!reads_flag(host, (major, minor, patch - 1)));
assert_eq!(since_text(host), format!("{major}.{minor}.{patch}"));
}
#[test]
fn every_copy_found_must_read_the_flag() {
let since = (2, 1, 295);
let new = Some((2, 1, 318));
assert!(all_read(&[new], since));
assert!(all_read(&[new, Some((2, 1, 295))], since));
assert!(!all_read(&[new, Some((2, 1, 200))], since));
assert!(!all_read(&[new, None], since));
assert!(!all_read(&[], since));
}
#[test]
fn a_host_without_the_flag_never_blocks_through_it() {
for host in Host::ALL.into_iter().filter(|h| since(*h).is_none()) {
assert!(!blocks_on_failure(host), "{host:?}");
assert!(!reads_flag(host, (999, 0, 0)), "{host:?}");
assert_eq!(since_text(host), "");
}
}
#[test]
fn the_flagged_host_has_copies_to_ask_and_a_test_variable() {
let host = flagged();
assert!(!host.spec().bundled.is_empty());
assert_ne!(host.spec().process.names, &[] as &[&str]);
assert_eq!(version_variable(host), "MOAT_CLAUDE_CODE_VERSION");
}
}