cargo_tangle/foundry/
mod.rs

1//! Foundry Utilities.
2use std::process::Command;
3
4mod forge;
5
6pub struct FoundryToolchain {
7    pub forge: forge::Forge,
8}
9
10/// Trait for checking if a command is installed.
11trait CommandInstalled {
12    /// Returns true if the command is installed.
13    fn is_installed(&self) -> bool;
14}
15
16impl CommandInstalled for Command {
17    fn is_installed(&self) -> bool {
18        let cmd = self.get_program();
19        if cfg!(target_os = "windows") {
20            Command::new("where")
21                .arg(cmd)
22                .output()
23                .is_ok_and(|v| v.status.success())
24        } else {
25            Command::new("which")
26                .arg(cmd)
27                .output()
28                .is_ok_and(|v| v.status.success())
29        }
30    }
31}
32
33impl Default for FoundryToolchain {
34    fn default() -> Self {
35        Self::new()
36    }
37}
38
39impl FoundryToolchain {
40    /// Creates a new FoundryToolchain instance.
41    pub fn new() -> Self {
42        Self {
43            forge: forge::Forge::new(),
44        }
45    }
46    pub fn check_installed_or_exit(&self) {
47        fn foundry_installation_instructions() {
48            eprintln!("Please install Foundry, follow https://getfoundry.sh/ for instructions.");
49        }
50        if !self.forge.is_installed() {
51            eprintln!("Forge is not installed.");
52            foundry_installation_instructions();
53        }
54        // Add more tools here.
55    }
56}