cargo_lambda_build/
zig.rs

1use crate::error::BuildError;
2use cargo_lambda_interactive::{
3    choose_option, command::silent_command, is_stdin_tty, progress::Progress,
4};
5use cargo_zigbuild::Zig;
6use miette::{IntoDiagnostic, Result};
7
8/// Print information about the Zig installation.
9pub fn print_install_options(options: &[InstallOption]) {
10    println!("Zig is not installed in your system.");
11    if is_stdin_tty() {
12        println!("Run `cargo lambda system --setup` to install Zig.")
13    }
14
15    if !options.is_empty() {
16        println!("You can use any of the following options to install it:");
17        for option in options {
18            println!("\t* {}: `{}`", option, option.usage());
19        }
20    }
21    println!(
22        "\t* Download Zig 0.9.1 or newer from https://ziglang.org/download/ and add it to your PATH"
23    );
24}
25
26/// Install Zig using a choice prompt.
27pub async fn install_zig(options: Vec<InstallOption>) -> Result<()> {
28    let choice = choose_option(
29        "Zig is not installed in your system.\nHow do you want to install Zig?",
30        options,
31    );
32
33    match choice {
34        Ok(choice) => choice.install().await.map(|_| ()),
35        Err(err) => Err(err).into_diagnostic(),
36    }
37}
38
39pub async fn check_installation() -> Result<()> {
40    if Zig::find_zig().is_ok() {
41        return Ok(());
42    }
43
44    let options = install_options();
45
46    if !is_stdin_tty() || options.is_empty() {
47        print_install_options(&options);
48        return Err(BuildError::ZigMissing.into());
49    }
50
51    install_zig(options).await
52}
53
54pub enum InstallOption {
55    #[cfg(not(windows))]
56    Brew,
57    #[cfg(windows)]
58    Choco,
59    #[cfg(not(windows))]
60    Npm,
61    Pip3,
62    #[cfg(windows)]
63    Scoop,
64}
65
66impl std::fmt::Display for InstallOption {
67    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68        match self {
69            #[cfg(not(windows))]
70            InstallOption::Brew => write!(f, "Install with Homebrew"),
71            #[cfg(windows)]
72            InstallOption::Choco => write!(f, "Install with Chocolatey"),
73            #[cfg(not(windows))]
74            InstallOption::Npm => write!(f, "Install with NPM"),
75            InstallOption::Pip3 => write!(f, "Install with Pip3 (Python 3)"),
76            #[cfg(windows)]
77            InstallOption::Scoop => write!(f, "Install with Scoop"),
78        }
79    }
80}
81
82impl InstallOption {
83    pub fn usage(&self) -> &'static str {
84        match self {
85            #[cfg(not(windows))]
86            InstallOption::Brew => "brew install zig",
87            #[cfg(windows)]
88            InstallOption::Choco => "choco install zig",
89            #[cfg(not(windows))]
90            InstallOption::Npm => "npm install -g @ziglang/cli",
91            InstallOption::Pip3 => "pip3 install ziglang",
92            #[cfg(windows)]
93            InstallOption::Scoop => "scoop install zig",
94        }
95    }
96
97    pub async fn install(self) -> Result<()> {
98        let pb = Progress::start("Installing Zig...");
99        let usage = self.usage().split(' ').collect::<Vec<_>>();
100        let usage = usage.as_slice();
101        let result = silent_command(usage[0], &usage[1..usage.len()]).await;
102
103        let finish = if result.is_ok() {
104            "Zig installed"
105        } else {
106            "Failed to install Zig"
107        };
108        pb.finish(finish);
109
110        result
111    }
112}
113
114pub fn install_options() -> Vec<InstallOption> {
115    let mut options = Vec::new();
116
117    #[cfg(not(windows))]
118    if which::which("brew").is_ok() {
119        options.push(InstallOption::Brew);
120    }
121
122    #[cfg(windows)]
123    if which::which("choco").is_ok() {
124        options.push(InstallOption::Choco);
125    }
126
127    #[cfg(windows)]
128    if which::which("scoop").is_ok() {
129        options.push(InstallOption::Scoop);
130    }
131
132    if which::which("pip3").is_ok() {
133        options.push(InstallOption::Pip3);
134    }
135
136    #[cfg(not(windows))]
137    if which::which("npm").is_ok() {
138        options.push(InstallOption::Npm);
139    }
140    options
141}