use crate::ghc::{GhcSource, ResolutionConfig, ToolchainManifest, resolve_ghc};
use hx_cache::toolchain_dir;
use hx_core::{CommandRunner, Version};
use std::path::PathBuf;
use tracing::{debug, instrument};
#[derive(Debug, Clone)]
pub enum ToolStatus {
Found { version: Version, path: PathBuf },
NotFound,
VersionUnknown { path: PathBuf },
}
impl ToolStatus {
pub fn is_found(&self) -> bool {
matches!(
self,
ToolStatus::Found { .. } | ToolStatus::VersionUnknown { .. }
)
}
pub fn version(&self) -> Option<&Version> {
match self {
ToolStatus::Found { version, .. } => Some(version),
_ => None,
}
}
pub fn path(&self) -> Option<&PathBuf> {
match self {
ToolStatus::Found { path, .. } | ToolStatus::VersionUnknown { path } => Some(path),
ToolStatus::NotFound => None,
}
}
}
#[derive(Debug, Clone)]
pub struct DetectedTool {
pub name: String,
pub status: ToolStatus,
pub source: Option<GhcSource>,
}
#[derive(Debug, Clone)]
pub struct Toolchain {
pub ghc: DetectedTool,
pub cabal: DetectedTool,
pub ghcup: DetectedTool,
pub hls: DetectedTool,
}
impl Toolchain {
#[instrument]
pub async fn detect() -> Self {
let runner = CommandRunner::new();
let (ghc, cabal, ghcup, hls) = tokio::join!(
detect_ghc(&runner),
detect_cabal(&runner),
detect_ghcup(&runner),
detect_hls(&runner),
);
Self {
ghc,
cabal,
ghcup,
hls,
}
}
pub fn is_complete(&self) -> bool {
self.ghc.status.is_found() && self.cabal.status.is_found()
}
pub fn has_ghcup(&self) -> bool {
self.ghcup.status.is_found()
}
}
async fn detect_ghc(runner: &CommandRunner) -> DetectedTool {
let config = ResolutionConfig::default();
if let Ok(Some(resolved)) = resolve_ghc(&config) {
debug!(
"Found hx-managed GHC {} at {}",
resolved.version,
resolved.bin_dir.display()
);
let ghc_path = resolved.ghc_path();
if ghc_path.exists()
&& let Ok(version) = resolved.version.parse()
{
return DetectedTool {
name: "ghc".to_string(),
status: ToolStatus::Found {
version,
path: ghc_path,
},
source: Some(resolved.source),
};
}
}
detect_tool(runner, "ghc", &["--version"]).await
}
async fn detect_cabal(runner: &CommandRunner) -> DetectedTool {
if let Ok(tc_dir) = toolchain_dir()
&& let Ok(manifest) = ToolchainManifest::load(&tc_dir)
&& let Some(active) = manifest.active_cabal()
{
debug!(
"Found hx-managed Cabal {} at {}",
active.version,
active.install_path.display()
);
let cabal_path = active.cabal_path();
if cabal_path.exists()
&& let Ok(version) = active.version.parse()
{
return DetectedTool {
name: "cabal".to_string(),
status: ToolStatus::Found {
version,
path: cabal_path,
},
source: Some(GhcSource::HxManaged),
};
}
}
detect_tool(runner, "cabal", &["--version"]).await
}
async fn detect_ghcup(runner: &CommandRunner) -> DetectedTool {
detect_tool(runner, "ghcup", &["--version"]).await
}
async fn detect_hls(runner: &CommandRunner) -> DetectedTool {
detect_tool(runner, "haskell-language-server-wrapper", &["--version"]).await
}
async fn detect_tool(runner: &CommandRunner, name: &str, args: &[&str]) -> DetectedTool {
debug!("Detecting {}", name);
let path = match which::which(name) {
Ok(p) => p,
Err(_) => {
debug!("{} not found in PATH", name);
return DetectedTool {
name: name.to_string(),
status: ToolStatus::NotFound,
source: None,
};
}
};
let source = if path.to_string_lossy().contains(".ghcup") {
Some(GhcSource::Ghcup)
} else {
Some(GhcSource::System)
};
match runner.run(name, args.iter().copied()).await {
Ok(output) if output.success() => {
let version_str = format!("{}{}", output.stdout, output.stderr);
if let Some(version) = Version::parse_from_output(&version_str) {
debug!("{} version: {}", name, version);
DetectedTool {
name: name.to_string(),
status: ToolStatus::Found { version, path },
source,
}
} else {
debug!(
"{} found but couldn't parse version from: {}",
name,
version_str.trim()
);
DetectedTool {
name: name.to_string(),
status: ToolStatus::VersionUnknown { path },
source,
}
}
}
Ok(output) => {
debug!("{} returned non-zero exit code: {}", name, output.exit_code);
DetectedTool {
name: name.to_string(),
status: ToolStatus::VersionUnknown { path },
source,
}
}
Err(e) => {
debug!("{} error: {}", name, e);
DetectedTool {
name: name.to_string(),
status: ToolStatus::NotFound,
source: None,
}
}
}
}