use crate::detect::{ToolStatus, Toolchain};
use crate::install::{self, SmartCabalInstallOptions, SmartInstallOptions};
use hx_core::{Error, Fix, Result, Version};
use std::io::{self, IsTerminal, Write};
use tracing::info;
#[derive(Debug, Clone)]
pub struct ToolchainCheck {
pub ghc_mismatch: Option<(String, Option<String>)>,
pub cabal_mismatch: Option<(String, Option<String>)>,
pub has_ghcup: bool,
}
impl ToolchainCheck {
pub fn has_mismatch(&self) -> bool {
self.ghc_mismatch.is_some() || self.cabal_mismatch.is_some()
}
pub fn can_auto_fix(&self) -> bool {
self.has_mismatch()
}
pub fn mismatch_summary(&self) -> String {
let mut parts = Vec::new();
if let Some((expected, found)) = &self.ghc_mismatch {
let found_str = found
.as_ref()
.map(|v| v.as_str())
.unwrap_or("not installed");
parts.push(format!("GHC {} (found: {})", expected, found_str));
}
if let Some((expected, found)) = &self.cabal_mismatch {
let found_str = found
.as_ref()
.map(|v| v.as_str())
.unwrap_or("not installed");
parts.push(format!("Cabal {} (found: {})", expected, found_str));
}
parts.join(", ")
}
}
pub fn check_requirements(
toolchain: &Toolchain,
required_ghc: Option<&str>,
required_cabal: Option<&str>,
) -> ToolchainCheck {
let ghc_mismatch = required_ghc.and_then(|required| {
check_version_match(&toolchain.ghc.status, required)
.map(|found| (required.to_string(), found))
});
let cabal_mismatch = required_cabal.and_then(|required| {
check_version_match(&toolchain.cabal.status, required)
.map(|found| (required.to_string(), found))
});
ToolchainCheck {
ghc_mismatch,
cabal_mismatch,
has_ghcup: toolchain.has_ghcup(),
}
}
fn check_version_match(status: &ToolStatus, required: &str) -> Option<Option<String>> {
match status {
ToolStatus::Found { version, .. } => {
let required_ver: Version = required.parse().ok()?;
let patch_matches = if required.matches('.').count() < 2 {
true
} else {
version.patch == required_ver.patch
};
if version.major == required_ver.major
&& version.minor == required_ver.minor
&& patch_matches
{
None } else {
Some(Some(version.to_string())) }
}
ToolStatus::NotFound => Some(None), ToolStatus::VersionUnknown { .. } => None, }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AutoInstallPolicy {
Never,
#[default]
Prompt,
Always,
}
pub async fn ensure_toolchain(
toolchain: &Toolchain,
required_ghc: Option<&str>,
required_cabal: Option<&str>,
policy: AutoInstallPolicy,
) -> Result<()> {
let check = check_requirements(toolchain, required_ghc, required_cabal);
if !check.has_mismatch() {
return Ok(());
}
info!("Toolchain mismatch detected: {}", check.mismatch_summary());
let effective_policy = if required_ghc.is_some() || required_cabal.is_some() {
match policy {
AutoInstallPolicy::Never => AutoInstallPolicy::Never,
_ => AutoInstallPolicy::Always,
}
} else {
policy
};
match effective_policy {
AutoInstallPolicy::Never => {
return Err(Error::ToolchainMismatch {
tool: "toolchain".to_string(),
expected: check.mismatch_summary(),
found: "auto-install disabled".to_string(),
fixes: build_install_fixes(&check),
});
}
AutoInstallPolicy::Prompt => {
if !confirm_install(&check)? {
return Err(Error::ToolchainMismatch {
tool: "toolchain".to_string(),
expected: check.mismatch_summary(),
found: "installation declined".to_string(),
fixes: build_install_fixes(&check),
});
}
}
AutoInstallPolicy::Always => {
}
}
if let Some((version, _)) = &check.ghc_mismatch {
let options = SmartInstallOptions::new(version).with_set_active(true);
install::install_ghc_smart(&options).await?;
}
if let Some((version, _)) = &check.cabal_mismatch {
let options = SmartCabalInstallOptions::new(version).with_set_active(true);
install::install_cabal_smart(&options).await?;
}
Ok(())
}
fn build_install_fixes(check: &ToolchainCheck) -> Vec<Fix> {
let mut fixes = Vec::new();
if let Some((version, _)) = &check.ghc_mismatch {
fixes.push(Fix::with_command(
format!("Install GHC {}", version),
format!("hx toolchain install --ghc {}", version),
));
}
if let Some((version, _)) = &check.cabal_mismatch {
fixes.push(Fix::with_command(
format!("Install Cabal {}", version),
format!("hx toolchain install --cabal {}", version),
));
}
fixes
}
fn confirm_install(check: &ToolchainCheck) -> Result<bool> {
if std::env::var("CI").is_ok() || !io::stdin().is_terminal() {
return Ok(false);
}
eprint!(
"\nToolchain mismatch: {}\nInstall missing toolchain components? [Y/n] ",
check.mismatch_summary()
);
io::stderr().flush().map_err(|e| Error::Io {
message: "failed to flush stderr".to_string(),
path: None,
source: e,
})?;
let mut input = String::new();
io::stdin().read_line(&mut input).map_err(|e| Error::Io {
message: "failed to read input".to_string(),
path: None,
source: e,
})?;
let input = input.trim().to_lowercase();
Ok(input.is_empty() || input == "y" || input == "yes")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::detect::DetectedTool;
use std::path::PathBuf;
fn make_tool(name: &str, version: Option<&str>) -> DetectedTool {
let status = match version {
Some(v) => ToolStatus::Found {
version: v.parse().unwrap(),
path: PathBuf::from("/usr/bin").join(name),
},
None => ToolStatus::NotFound,
};
DetectedTool {
name: name.to_string(),
status,
source: None,
}
}
#[test]
fn test_check_requirements_match() {
let toolchain = Toolchain {
ghc: make_tool("ghc", Some("9.8.2")),
cabal: make_tool("cabal", Some("3.12.1.0")),
ghcup: make_tool("ghcup", Some("0.1.30.0")),
hls: make_tool("hls", Some("2.9.0.0")),
};
let check = check_requirements(&toolchain, Some("9.8.2"), Some("3.12.1.0"));
assert!(!check.has_mismatch());
}
#[test]
fn test_check_requirements_ghc_mismatch() {
let toolchain = Toolchain {
ghc: make_tool("ghc", Some("9.6.4")),
cabal: make_tool("cabal", Some("3.12.1.0")),
ghcup: make_tool("ghcup", Some("0.1.30.0")),
hls: make_tool("hls", Some("2.9.0.0")),
};
let check = check_requirements(&toolchain, Some("9.8.2"), None);
assert!(check.has_mismatch());
assert!(check.ghc_mismatch.is_some());
assert!(check.cabal_mismatch.is_none());
}
#[test]
fn test_check_requirements_not_installed() {
let toolchain = Toolchain {
ghc: make_tool("ghc", None),
cabal: make_tool("cabal", Some("3.12.1.0")),
ghcup: make_tool("ghcup", Some("0.1.30.0")),
hls: make_tool("hls", None),
};
let check = check_requirements(&toolchain, Some("9.8.2"), None);
assert!(check.has_mismatch());
assert!(check.ghc_mismatch.is_some());
let (expected, found) = check.ghc_mismatch.unwrap();
assert_eq!(expected, "9.8.2");
assert!(found.is_none());
}
#[test]
fn test_check_requirements_minor_version_mismatch() {
let toolchain = Toolchain {
ghc: make_tool("ghc", Some("9.8.1")),
cabal: make_tool("cabal", Some("3.12.1.0")),
ghcup: make_tool("ghcup", Some("0.1.30.0")),
hls: make_tool("hls", Some("2.9.0.0")),
};
let check = check_requirements(&toolchain, Some("9.8"), None);
assert!(!check.has_mismatch());
let check = check_requirements(&toolchain, Some("9.8.2"), None);
assert!(check.has_mismatch());
}
}