intelli_shell/utils/
installation.rs

1use std::{env, path::Path};
2
3#[derive(Debug, PartialEq, Eq)]
4pub enum InstallationMethod {
5    /// Installed via the installer script
6    Installer,
7    /// Installed via `cargo install`
8    Cargo,
9    /// Installed via Nix
10    Nix,
11    /// Likely compiled directly from a source checkout
12    Source,
13    /// Could not determine the installation method
14    Unknown(Option<String>),
15}
16
17/// Detects how `intelli-shell` was installed by inspecting the executable's path
18pub fn detect_installation_method(data_dir: impl AsRef<Path>) -> InstallationMethod {
19    let current_exe = match env::current_exe() {
20        Ok(path) => path,
21        Err(_) => return InstallationMethod::Unknown(None),
22    };
23
24    // Check if the executable is located within the official data directory's `bin` subfolder.
25    // This is the strongest indicator of an installation via the script.
26    let installer_bin_path = data_dir.as_ref().join("bin");
27    if current_exe.starts_with(installer_bin_path) {
28        return InstallationMethod::Installer;
29    }
30
31    // Fallback to checking for common package manager and development paths
32    let path_str = current_exe.to_string_lossy();
33    if path_str.starts_with("/nix/store/") {
34        // Nix/NixOS path (Linux, macOS)
35        InstallationMethod::Nix
36    } else if path_str.contains(".cargo/bin") {
37        // Cargo path (Linux, macOS, Windows)
38        InstallationMethod::Cargo
39    } else if path_str.contains("target/debug") || path_str.contains("target/release") {
40        // Running from a local build directory
41        InstallationMethod::Source
42    } else {
43        InstallationMethod::Unknown(Some(path_str.to_string()))
44    }
45}