use std::path::PathBuf;
use color_eyre::eyre;
use crate::{
brew::Brew,
toolchain::{Installation, Toolchain, ToolchainError},
utils::which,
};
#[derive(Debug, Clone, Default)]
pub struct Meson;
impl Meson {
pub async fn path(&self) -> eyre::Result<PathBuf> {
which("meson").await.map_err(|e| eyre::eyre!(e))
}
}
impl Toolchain for Meson {
type Installation = MesonInstallation;
async fn check(&self) -> Result<(), ToolchainError<Self::Installation>> {
if which("meson").await.is_ok() {
Ok(())
} else {
Err(ToolchainError::fixable(MesonInstallation))
}
}
}
#[derive(Debug, Clone)]
pub struct MesonInstallation;
#[derive(Debug, thiserror::Error)]
pub enum FailToInstallMeson {
#[error("Homebrew not found. Please install Homebrew to proceed.")]
BrewNotFound,
#[error("Failed to install meson via Homebrew: {0}")]
Other(eyre::Report),
#[error(
"Automatic installation of meson is not supported on this platform. Please install meson manually."
)]
UnsupportedPlatform,
}
impl Installation for MesonInstallation {
type Error = FailToInstallMeson;
async fn install(&self) -> Result<(), Self::Error> {
if cfg!(target_os = "macos") {
let brew = Brew::default();
brew.check()
.await
.map_err(|_| FailToInstallMeson::BrewNotFound)?;
brew.install("meson")
.await
.map_err(FailToInstallMeson::Other)?;
Ok(())
} else {
Err(FailToInstallMeson::UnsupportedPlatform)
}
}
}