install.fairie/scripts/partials/homebrew

78 lines
3.2 KiB
Text
Raw Normal View History

2023-12-08 16:39:21 -08:00
### Ensure Homebrew is loaded
loadHomebrew() {
2023-11-27 14:53:29 -08:00
if ! command -v brew > /dev/null; then
2023-12-08 16:39:21 -08:00
if [ -f /usr/local/bin/brew ]; then
logg info "Using /usr/local/bin/brew" && eval "$(/usr/local/bin/brew shellenv)"
2023-12-23 03:59:45 -08:00
elif [ -f "${HOMEBREW_PREFIX:-/opt/homebrew}/bin/brew" ]; then
2023-12-08 16:39:21 -08:00
logg info "Using ${HOMEBREW_PREFIX:-/opt/homebrew}/bin/brew" && eval "$("${HOMEBREW_PREFIX:-/opt/homebrew}/bin/brew" shellenv)"
2023-12-23 03:59:45 -08:00
elif [ -d "$HOME/.linuxbrew" ]; then
2023-12-08 16:39:21 -08:00
logg info "Using $HOME/.linuxbrew/bin/brew" && eval "$("$HOME/.linuxbrew/bin/brew" shellenv)"
elif [ -d "/home/linuxbrew/.linuxbrew" ]; then
logg info 'Using /home/linuxbrew/.linuxbrew/bin/brew' && eval "(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
else
2023-12-08 16:39:21 -08:00
logg info 'Could not find Homebrew installation'
fi
fi
}
2023-12-08 16:39:21 -08:00
### Ensures Homebrew folders have proper owners / permissions
fixHomebrewPermissions() {
if command -v brew > /dev/null; then
logg info 'Applying proper permissions on Homebrew folders'
sudo chmod -R go-w "$(brew --prefix)/share"
BREW_DIRS="share etc/bash_completion.d"
for BREW_DIR in $BREW_DIRS; do
if [ -d "$(brew --prefix)/$BREW_DIR" ]; then
sudo chown -Rf "$(whoami)" "$(brew --prefix)/$BREW_DIR"
2023-11-27 14:53:29 -08:00
fi
2023-12-08 16:39:21 -08:00
done
logg info 'Running brew update --force --quiet' && brew update --force --quiet
fi
}
### Installs Homebrew
ensurePackageManagerHomebrew() {
if ! command -v brew > /dev/null; then
### Select install type based off of whether or not sudo privileges are available
if command -v sudo > /dev/null && sudo -n true; then
logg info 'Installing Homebrew. Sudo privileges available.'
echo | bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" || BREW_EXIT_CODE="$?"
else
logg info 'Installing Homebrew. Sudo privileges not available. Password may be required.'
bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" || BREW_EXIT_CODE="$?"
fi
2023-12-08 16:39:21 -08:00
### Attempt to fix problematic installs
if [ -n "$BREW_EXIT_CODE" ]; then
logg warn 'Homebrew was installed but part of the installation failed to complete successfully.'
fixHomebrewPermissions
fi
2023-11-12 20:53:44 -08:00
fi
2023-12-08 16:39:21 -08:00
}
2023-11-12 20:37:45 -08:00
2023-12-08 16:39:21 -08:00
### Ensures gcc is installed
ensureGcc() {
2023-11-27 14:53:29 -08:00
if command -v brew > /dev/null; then
if ! brew list | grep gcc > /dev/null; then
2023-12-08 16:39:21 -08:00
logg info 'Installing Homebrew gcc' && brew install --quiet gcc
else
logg info 'Homebrew gcc is available'
2023-11-27 14:53:29 -08:00
fi
else
2023-12-08 16:39:21 -08:00
logg error 'Failed to initialize Homebrew' && exit 1
2023-11-12 20:53:44 -08:00
fi
2023-11-27 14:53:29 -08:00
}
2023-12-08 16:39:21 -08:00
# @description This function ensures Homebrew is installed and available in the `PATH`. It handles the installation of Homebrew on both **Linux and macOS**.
# It will attempt to bypass sudo password entry if it detects that it can do so. The function also has some error handling in regards to various
# directories falling out of the correct ownership and permission states. Finally, it loads Homebrew into the active profile (allowing other parts of the script
# to use the `brew` command).
#
# With Homebrew installed and available, the script finishes by installing the `gcc` Homebrew package which is a very common dependency.
ensureHomebrew() {
loadHomebrew
ensurePackageManagerHomebrew
loadHomebrew
ensureGcc
}