cargo_tangle/foundry/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! Foundry Utilities.
use std::process::Command;

mod forge;

pub struct FoundryToolchain {
    pub forge: forge::Forge,
}

/// Trait for checking if a command is installed.
trait CommandInstalled {
    /// Returns true if the command is installed.
    fn is_installed(&self) -> bool;
}

impl CommandInstalled for Command {
    fn is_installed(&self) -> bool {
        let cmd = self.get_program();
        if cfg!(target_os = "windows") {
            Command::new("where")
                .arg(cmd)
                .output()
                .is_ok_and(|v| v.status.success())
        } else {
            Command::new("which")
                .arg(cmd)
                .output()
                .is_ok_and(|v| v.status.success())
        }
    }
}

impl Default for FoundryToolchain {
    fn default() -> Self {
        Self::new()
    }
}

impl FoundryToolchain {
    /// Creates a new FoundryToolchain instance.
    pub fn new() -> Self {
        Self {
            forge: forge::Forge::new(),
        }
    }
    pub fn check_installed_or_exit(&self) {
        fn foundry_installation_instructions() {
            eprintln!("Please install Foundry, follow https://getfoundry.sh/ for instructions.");
        }
        if !self.forge.is_installed() {
            eprintln!("Forge is not installed.");
            foundry_installation_instructions();
        }
        // Add more tools here.
    }
}