dotfiles_actions/homebrew_install/
action.rs

1// Copyright (c) 2021-2022 Miguel Barreto and others
2//
3// Permission is hereby granted, free of charge, to any person obtaining
4// a copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to
8// permit persons to whom the Software is furnished to do so, subject to
9// the following conditions:
10//
11// The above copyright notice and this permission notice shall be
12// included in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22//! This module contains the [HomebrewInstallAction] that installs
23//! homebrew on a macOS computer
24
25#![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
33/// [HomebrewInstallAction] installs homebrew.
34pub struct HomebrewInstallAction {}
35
36impl Default for HomebrewInstallAction {
37  fn default() -> Self {
38    Self::new()
39  }
40}
41
42impl HomebrewInstallAction {
43  /// Constructs a new [HomebrewInstallAction]
44  pub fn new() -> Self {
45    HomebrewInstallAction {}
46  }
47  /// Returns true if it can find a brew command
48  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}