dotfiles_actions/homebrew_install/
action.rs1#![cfg(unix)]
26use dotfiles_core::action::Action;
27use dotfiles_core::error::DotfilesError;
28use dotfiles_core::exec_wrapper::execute_commands;
29use log::info;
30use std::process::Command;
31use subprocess::Exec;
32
33pub struct HomebrewInstallAction {}
35
36impl Default for HomebrewInstallAction {
37 fn default() -> Self {
38 Self::new()
39 }
40}
41
42impl HomebrewInstallAction {
43 pub fn new() -> Self {
45 HomebrewInstallAction {}
46 }
47 pub fn check_brew_is_installed(&self) -> bool {
49 Command::new("which")
50 .arg("brew")
51 .status()
52 .unwrap()
53 .success()
54 }
55}
56
57impl Action<'_> for HomebrewInstallAction {
58 fn execute(&self) -> Result<(), DotfilesError> {
59 if !self.check_brew_is_installed() {
60 let result = execute_commands(
61 vec!(Exec::shell(
62 "/bin/bash -c \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"")),
63 "Error running homebrew installer",
64 "Couldn't run homebrew installer");
65 #[cfg(not(any(target_os = "linux", all(target_os = "macos", target_arch = "aarch64"))))]
66 return result;
67 #[cfg(all(target_os = "macos", target_arch = "aarch64"))]
68 {
69 result?;
70 execute_commands(
71 vec![
72 Exec::shell("echo 'eval \"$(/opt/homebrew/bin/brew shellenv)\"' >> $HOME/.zprofile"),
73 Exec::shell(
74 "echo 'eval \"$(/opt/homebrew/bin/brew shellenv)\"' >> $HOME/.bash_profile",
75 ),
76 ],
77 "couldn't set .zprofile and .bash_profile to use homebrew",
78 "couldn't set .zprofile and .bash_profile to use homebrew",
79 )
80 }
81 #[cfg(all(target_os = "linux"))]
82 {
83 result?;
84 execute_commands(
85 vec![
86 Exec::shell(
87 "echo 'eval \"$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)\"' >> $HOME/.zprofile",
88 ),
89 Exec::shell(
90 "echo 'eval \"$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)\"' >> $HOME/.bash_profile",
91 ),
92 ],
93 "couldn't set .zprofile and .bash_profile to use homebrew",
94 "couldn't set .zprofile and .bash_profile to use homebrew",
95 )
96 }
97 } else {
98 info!("Homebrew already installed, no need to re-install");
99 Ok(())
100 }
101 }
102}