use anyhow::{Context, Result};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use tracing::{info, warn};
#[derive(Debug, Clone, PartialEq)]
pub enum OllamaInstallType {
AlreadyInstalled,
SystemWide,
Portable { path: PathBuf, process: Option<u32> },
}
pub struct OllamaInstaller {
install_type: Option<OllamaInstallType>,
temp_dir: Option<PathBuf>,
}
impl Default for OllamaInstaller {
fn default() -> Self {
Self::new()
}
}
impl OllamaInstaller {
pub fn new() -> Self {
Self {
install_type: None,
temp_dir: None,
}
}
pub fn is_available() -> bool {
if let Ok(output) = Command::new("ollama").arg("--version").output() {
return output.status.success();
}
false
}
pub fn prompt_installation(&mut self) -> Result<OllamaInstallType> {
println!();
println!("๐ Ollama not found on your system.");
println!();
println!("bindings-generat uses AI to enhance generated bindings.");
println!("This requires Ollama (local LLM runtime).");
println!();
println!("Choose installation option:");
println!(" 1. System-wide install (requires admin/sudo, persists after run)");
println!(" 2. Portable install in temp directory (~1.3GB, auto-cleanup available)");
println!(" 3. Skip LLM features (generates basic bindings only)");
println!();
loop {
print!("Your choice [1/2/3]: ");
io::stdout().flush()?;
let mut input = String::new();
io::stdin().read_line(&mut input)?;
match input.trim() {
"1" => {
return Ok(OllamaInstallType::SystemWide);
}
"2" => {
let temp_path = self.create_temp_directory()?;
return Ok(OllamaInstallType::Portable {
path: temp_path,
process: None,
});
}
"3" => {
println!();
println!("โ Skipping LLM features. Bindings will have minimal documentation.");
return Err(anyhow::anyhow!("User skipped LLM installation"));
}
_ => {
println!("Invalid choice. Please enter 1, 2, or 3.");
continue;
}
}
}
}
fn create_temp_directory(&mut self) -> Result<PathBuf> {
let temp_base = std::env::temp_dir();
let uuid = uuid::Uuid::new_v4();
let temp_path = temp_base.join(format!("bindings-generat-ollama-{}", uuid));
std::fs::create_dir_all(&temp_path)
.context("Failed to create temporary directory for Ollama")?;
info!("Created temp directory: {}", temp_path.display());
self.temp_dir = Some(temp_path.clone());
Ok(temp_path)
}
pub fn install_system_wide(&mut self) -> Result<()> {
println!();
println!("๐ฆ Installing Ollama system-wide...");
println!(" This may require administrator/sudo permissions.");
println!();
let install_cmd = if cfg!(target_os = "windows") {
"winget install Ollama.Ollama"
} else if cfg!(target_os = "macos") {
"curl https://ollama.ai/install.sh | sh"
} else {
"curl https://ollama.ai/install.sh | sh"
};
println!("Running: {}", install_cmd);
let status = if cfg!(target_os = "windows") {
Command::new("cmd")
.args(["/C", install_cmd])
.status()
.context("Failed to run Ollama installer")?
} else {
Command::new("sh")
.args(["-c", install_cmd])
.status()
.context("Failed to run Ollama installer")?
};
if !status.success() {
anyhow::bail!("Ollama installation failed");
}
println!("โ Ollama installed successfully");
self.install_type = Some(OllamaInstallType::SystemWide);
Ok(())
}
pub fn install_portable(
&mut self,
temp_path: PathBuf,
model: &super::models::LlmModel,
) -> Result<Child> {
println!();
println!("๐ฆ Setting up portable Ollama...");
let binary_path = self.download_ollama_binary(&temp_path)?;
let models_dir = temp_path.join("models");
std::fs::create_dir_all(&models_dir)?;
println!(" โข Starting Ollama server...");
let mut child = Command::new(&binary_path)
.arg("serve")
.env("OLLAMA_MODELS", &models_dir)
.env("OLLAMA_HOST", "127.0.0.1:11435") .stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.context("Failed to start Ollama server")?;
std::thread::sleep(std::time::Duration::from_secs(3));
if let Ok(Some(_)) = child.try_wait() {
anyhow::bail!("Ollama server failed to start");
}
let pid = child.id();
println!(" โข Ollama server started (PID: {})", pid);
self.download_model(&binary_path, &models_dir, model)?;
println!("โ Portable Ollama ready at {}", temp_path.display());
println!();
self.install_type = Some(OllamaInstallType::Portable {
path: temp_path,
process: Some(pid),
});
Ok(child)
}
fn download_ollama_binary(&self, dest_dir: &Path) -> Result<PathBuf> {
let (url, filename) = if cfg!(target_os = "windows") {
(
"https://github.com/ollama/ollama/releases/latest/download/ollama-windows-amd64.exe",
"ollama.exe",
)
} else if cfg!(target_os = "macos") {
(
"https://github.com/ollama/ollama/releases/latest/download/ollama-darwin",
"ollama",
)
} else {
(
"https://github.com/ollama/ollama/releases/latest/download/ollama-linux-amd64",
"ollama",
)
};
let binary_path = dest_dir.join(filename);
println!(" โข Downloading Ollama binary (~300MB)...");
let config = super::network::DownloadConfig::new(url, &binary_path)
.with_expected_size(300_000_000) .with_description("Ollama binary");
super::network::download_with_retry(&config).context("Failed to download Ollama binary")?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mut perms = std::fs::metadata(&binary_path)?.permissions();
perms.set_mode(0o755);
std::fs::set_permissions(&binary_path, perms)?;
}
Ok(binary_path)
}
fn download_model(
&self,
ollama_binary: &PathBuf,
models_dir: &PathBuf,
model: &super::models::LlmModel,
) -> Result<()> {
println!(
" โข Downloading model {} ({})...",
model.name(),
model.size()
);
let status = Command::new(ollama_binary)
.arg("pull")
.arg(model.name())
.env("OLLAMA_MODELS", models_dir)
.env("OLLAMA_HOST", "127.0.0.1:11435")
.stdout(Stdio::inherit()) .stderr(Stdio::inherit())
.status()
.context("Failed to download model")?;
if !status.success() {
anyhow::bail!("Model download failed");
}
println!(" โ Model downloaded");
Ok(())
}
pub fn prompt_cleanup(&mut self) -> Result<()> {
if let Some(OllamaInstallType::Portable { ref path, process }) = self.install_type {
println!();
print!("๐งน Clean up portable Ollama install? (~1.3GB freed) [Y/n]: ");
io::stdout().flush()?;
let mut input = String::new();
io::stdin().read_line(&mut input)?;
match input.trim().to_lowercase().as_str() {
"" | "y" | "yes" => {
self.cleanup_portable(path.clone(), process)?;
}
_ => {
println!(" Keeping portable install at: {}", path.display());
println!(" (You can manually delete this directory later)");
}
}
}
Ok(())
}
fn cleanup_portable(&mut self, path: PathBuf, process_id: Option<u32>) -> Result<()> {
println!(" โข Stopping Ollama server...");
if let Some(pid) = process_id {
#[cfg(unix)]
{
let _ = Command::new("kill").arg(pid.to_string()).status();
}
#[cfg(windows)]
{
let _ = Command::new("taskkill")
.args(["/PID", &pid.to_string(), "/F"])
.status();
}
}
println!(" โข Removing {}...", path.display());
std::fs::remove_dir_all(&path).context("Failed to remove temp directory")?;
println!("โ Cleanup complete");
self.install_type = None;
Ok(())
}
pub fn install_type(&self) -> Option<&OllamaInstallType> {
self.install_type.as_ref()
}
pub fn ollama_host(&self) -> String {
if let Some(OllamaInstallType::Portable { .. }) = self.install_type {
"http://127.0.0.1:11435".to_string()
} else {
"http://localhost:11434".to_string()
}
}
}
impl Drop for OllamaInstaller {
fn drop(&mut self) {
if let Some(ref temp_dir) = self.temp_dir
&& temp_dir.exists()
{
warn!(
"Temp directory still exists at drop: {}",
temp_dir.display()
);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_installer_creation() {
let installer = OllamaInstaller::new();
assert!(installer.install_type.is_none());
assert!(installer.temp_dir.is_none());
}
#[test]
fn test_ollama_host_default() {
let installer = OllamaInstaller::new();
assert_eq!(installer.ollama_host(), "http://localhost:11434");
}
#[test]
fn test_ollama_host_portable() {
let mut installer = OllamaInstaller::new();
installer.install_type = Some(OllamaInstallType::Portable {
path: PathBuf::from("/tmp/test"),
process: Some(12345),
});
assert_eq!(installer.ollama_host(), "http://127.0.0.1:11435");
}
}