use crate::cabal::{self, CabalDownloadOptions};
use crate::ghc::{self, DownloadOptions, Platform};
use hx_cache::toolchain_dir;
use hx_core::{CommandRunner, Error, Fix, Result};
use hx_ui::Spinner;
use tracing::{info, warn};
pub async fn install_ghc(version: &str) -> Result<()> {
let runner = CommandRunner::new();
let spinner = Spinner::new(format!("Installing GHC {}", version));
info!("Installing GHC {} via ghcup", version);
let output = runner.run("ghcup", ["install", "ghc", version]).await?;
if output.success() {
let _ = runner.run("ghcup", ["set", "ghc", version]).await;
spinner.finish_success(format!("GHC {} installed", version));
Ok(())
} else {
spinner.finish_error(format!("Failed to install GHC {}", version));
Err(Error::CommandFailed {
command: format!("ghcup install ghc {}", version),
exit_code: Some(output.exit_code),
stdout: output.stdout,
stderr: output.stderr,
fixes: vec![],
})
}
}
pub async fn install_cabal(version: &str) -> Result<()> {
let runner = CommandRunner::new();
let spinner = Spinner::new(format!("Installing Cabal {}", version));
info!("Installing Cabal {} via ghcup", version);
let output = runner.run("ghcup", ["install", "cabal", version]).await?;
if output.success() {
let _ = runner.run("ghcup", ["set", "cabal", version]).await;
spinner.finish_success(format!("Cabal {} installed", version));
Ok(())
} else {
spinner.finish_error(format!("Failed to install Cabal {}", version));
Err(Error::CommandFailed {
command: format!("ghcup install cabal {}", version),
exit_code: Some(output.exit_code),
stdout: output.stdout,
stderr: output.stderr,
fixes: vec![],
})
}
}
pub async fn install_hls(version: &str) -> Result<()> {
let runner = CommandRunner::new();
let spinner = Spinner::new(format!("Installing HLS {}", version));
info!("Installing HLS {} via ghcup", version);
let output = runner.run("ghcup", ["install", "hls", version]).await?;
if output.success() {
let _ = runner.run("ghcup", ["set", "hls", version]).await;
spinner.finish_success(format!("HLS {} installed", version));
Ok(())
} else {
spinner.finish_error(format!("Failed to install HLS {}", version));
Err(Error::CommandFailed {
command: format!("ghcup install hls {}", version),
exit_code: Some(output.exit_code),
stdout: output.stdout,
stderr: output.stderr,
fixes: vec![],
})
}
}
pub async fn set_ghc(version: &str) -> Result<()> {
let runner = CommandRunner::new();
info!("Setting GHC to {} via ghcup", version);
let output = runner.run("ghcup", ["set", "ghc", version]).await?;
if output.success() {
Ok(())
} else {
Err(Error::CommandFailed {
command: format!("ghcup set ghc {}", version),
exit_code: Some(output.exit_code),
stdout: output.stdout,
stderr: output.stderr,
fixes: vec![Fix::with_command(
format!("Install GHC {} first", version),
format!("hx toolchain install --ghc {}", version),
)],
})
}
}
pub async fn set_cabal(version: &str) -> Result<()> {
let runner = CommandRunner::new();
info!("Setting Cabal to {} via ghcup", version);
let output = runner.run("ghcup", ["set", "cabal", version]).await?;
if output.success() {
Ok(())
} else {
Err(Error::CommandFailed {
command: format!("ghcup set cabal {}", version),
exit_code: Some(output.exit_code),
stdout: output.stdout,
stderr: output.stderr,
fixes: vec![Fix::with_command(
format!("Install Cabal {} first", version),
format!("hx toolchain install --cabal {}", version),
)],
})
}
}
pub async fn set_hls(version: &str) -> Result<()> {
let runner = CommandRunner::new();
info!("Setting HLS to {} via ghcup", version);
let output = runner.run("ghcup", ["set", "hls", version]).await?;
if output.success() {
Ok(())
} else {
Err(Error::CommandFailed {
command: format!("ghcup set hls {}", version),
exit_code: Some(output.exit_code),
stdout: output.stdout,
stderr: output.stderr,
fixes: vec![Fix::with_command(
format!("Install HLS {} first", version),
format!("hx toolchain install --hls {}", version),
)],
})
}
}
pub fn ghcup_install_command() -> &'static str {
if cfg!(windows) {
"Set-ExecutionPolicy Bypass -Scope Process -Force;[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; try { Invoke-Command -ScriptBlock ([ScriptBlock]::Create((Invoke-WebRequest https://www.haskell.org/ghcup/sh/bootstrap-haskell.ps1 -UseBasicParsing))) -ArgumentList $true } catch { Write-Error $_ }"
} else {
"curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh"
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum InstallStrategy {
#[default]
Smart,
Direct,
Ghcup,
}
#[derive(Debug, Clone)]
pub struct SmartInstallOptions {
pub version: String,
pub strategy: InstallStrategy,
pub set_active: bool,
pub force: bool,
}
impl SmartInstallOptions {
pub fn new(version: impl Into<String>) -> Self {
Self {
version: version.into(),
strategy: InstallStrategy::default(),
set_active: false,
force: false,
}
}
pub fn with_strategy(mut self, strategy: InstallStrategy) -> Self {
self.strategy = strategy;
self
}
pub fn with_set_active(mut self, set_active: bool) -> Self {
self.set_active = set_active;
self
}
pub fn with_force(mut self, force: bool) -> Self {
self.force = force;
self
}
}
pub async fn install_ghc_smart(options: &SmartInstallOptions) -> Result<()> {
match options.strategy {
InstallStrategy::Direct => install_ghc_direct(options).await,
InstallStrategy::Ghcup => install_ghc(&options.version).await,
InstallStrategy::Smart => {
if Platform::current().is_none() {
info!("Platform not supported for direct download, using ghcup");
return install_ghc(&options.version).await;
}
match install_ghc_direct(options).await {
Ok(()) => Ok(()),
Err(e) => {
warn!(
"Direct GHC installation failed, falling back to ghcup: {}",
e
);
install_ghc(&options.version).await
}
}
}
}
}
async fn install_ghc_direct(options: &SmartInstallOptions) -> Result<()> {
let tc_dir = toolchain_dir()?;
let download_options = DownloadOptions {
version: options.version.clone(),
platform: Platform::current(),
toolchain_dir: tc_dir,
set_active: options.set_active,
force: options.force,
..Default::default()
};
let result = ghc::download_and_install_ghc(&download_options).await?;
if result.was_cached {
info!("GHC {} was already installed", options.version);
} else {
info!("GHC {} installed successfully", options.version);
}
Ok(())
}
#[derive(Debug, Clone)]
pub struct SmartCabalInstallOptions {
pub version: String,
pub strategy: InstallStrategy,
pub set_active: bool,
pub force: bool,
}
impl SmartCabalInstallOptions {
pub fn new(version: impl Into<String>) -> Self {
Self {
version: version.into(),
strategy: InstallStrategy::default(),
set_active: false,
force: false,
}
}
pub fn with_strategy(mut self, strategy: InstallStrategy) -> Self {
self.strategy = strategy;
self
}
pub fn with_set_active(mut self, set_active: bool) -> Self {
self.set_active = set_active;
self
}
pub fn with_force(mut self, force: bool) -> Self {
self.force = force;
self
}
}
pub async fn install_cabal_smart(options: &SmartCabalInstallOptions) -> Result<()> {
match options.strategy {
InstallStrategy::Direct => install_cabal_direct(options).await,
InstallStrategy::Ghcup => install_cabal(&options.version).await,
InstallStrategy::Smart => {
if Platform::current().is_none() {
info!("Platform not supported for direct download, using ghcup");
return install_cabal(&options.version).await;
}
match install_cabal_direct(options).await {
Ok(()) => Ok(()),
Err(e) => {
warn!(
"Direct Cabal installation failed, falling back to ghcup: {}",
e
);
install_cabal(&options.version).await
}
}
}
}
}
async fn install_cabal_direct(options: &SmartCabalInstallOptions) -> Result<()> {
let tc_dir = toolchain_dir()?;
let download_options = CabalDownloadOptions {
version: options.version.clone(),
platform: Platform::current(),
toolchain_dir: tc_dir,
set_active: options.set_active,
force: options.force,
..Default::default()
};
let result = cabal::download_and_install_cabal(&download_options).await?;
if result.was_cached {
info!("Cabal {} was already installed", options.version);
} else {
info!("Cabal {} installed successfully", options.version);
}
Ok(())
}