From 9e255c21392adf612f735381c98ec9e2683d6c21 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sun, 8 Aug 2021 00:54:27 -0400 Subject: [PATCH 1/3] lib/helpers: new function to set BASH_IT_HOMEBREW_PREFIX New function `_bash_it_homebrew_check()` sets global variable `$BASH_IT_HOMEBREW_PREFIX` using `brew --prefix` if `brew` exists as a valid command. If `brew` isn't installed, then return failure. Plugins can test for `brew` by calling this function and, if it succeeds, they can rely on `$BASH_IT_HOMEBREW_PREFIX` being defined properly. --- lib/helpers.bash | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/helpers.bash b/lib/helpers.bash index 4d058fd1..82c710a0 100755 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -48,6 +48,19 @@ function _completion_exists () complete -p "$1" &> /dev/null && _log_warning "$msg" ; } +function _bash_it_homebrew_check() +{ + if [[ "${BASH_IT_HOMEBREW_PREFIX:-unset}" == 'unset' ]] + then # variable isn't set + if _binary_exists 'brew' + then # Homebrew is installed + BASH_IT_HOMEBREW_PREFIX="$(brew --prefix)" + else # Homebrew is not installed. + false # return failure if brew not installed. + fi + fi +} + function _make_reload_alias() { echo "source \${BASH_IT}/scripts/reloader.bash ${1} ${2}" } From 65ef8e2e8be9fc6ded3611751725a32548c933c2 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Wed, 11 Aug 2021 18:05:20 -0700 Subject: [PATCH 2/3] Use _bash_it_homebrew_check() in plugins, completions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use new function `_bash_it_homebrew_check()` in existing plugins and completions which look for Homebrew. Alsö, use `$OSTYPE` instead of calling external `uname` binary. --- completion/available/brew.completion.bash | 16 +++++++--------- completion/available/system.completion.bash | 9 ++++----- plugins/available/autojump.plugin.bash | 4 ++-- plugins/available/nvm.plugin.bash | 4 ++-- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/completion/available/brew.completion.bash b/completion/available/brew.completion.bash index 2149d20d..61998f8a 100644 --- a/completion/available/brew.completion.bash +++ b/completion/available/brew.completion.bash @@ -11,22 +11,20 @@ if [[ "$OSTYPE" != 'darwin'* ]]; then fi # Make sure brew is installed -_command_exists brew || return 0 +_bash_it_homebrew_check || return 0 -BREW_PREFIX=${BREW_PREFIX:-$(brew --prefix)} - -if [[ -r "$BREW_PREFIX"/etc/bash_completion.d/brew ]]; then +if [[ -r "$BASH_IT_HOMEBREW_PREFIX/etc/bash_completion.d/brew" ]]; then # shellcheck disable=1090 - source "$BREW_PREFIX"/etc/bash_completion.d/brew + source "$BASH_IT_HOMEBREW_PREFIX/etc/bash_completion.d/brew" -elif [[ -r "$BREW_PREFIX"/Library/Contributions/brew_bash_completion.sh ]]; then +elif [[ -r "$BASH_IT_HOMEBREW_PREFIX/Library/Contributions/brew_bash_completion.sh" ]]; then # shellcheck disable=1090 - source "$BREW_PREFIX"/Library/Contributions/brew_bash_completion.sh + source "$BASH_IT_HOMEBREW_PREFIX/Library/Contributions/brew_bash_completion.sh" -elif [[ -f "$BREW_PREFIX"/completions/bash/brew ]]; then +elif [[ -f "$BASH_IT_HOMEBREW_PREFIX/completions/bash/brew" ]]; then # For the git-clone based installation, see here for more info: # https://github.com/Bash-it/bash-it/issues/1458 # https://docs.brew.sh/Shell-Completion # shellcheck disable=1090 - source "$BREW_PREFIX"/completions/bash/brew + source "$BASH_IT_HOMEBREW_PREFIX/completions/bash/brew" fi diff --git a/completion/available/system.completion.bash b/completion/available/system.completion.bash index b64fb82b..142e0880 100755 --- a/completion/available/system.completion.bash +++ b/completion/available/system.completion.bash @@ -14,12 +14,11 @@ elif [[ -r /etc/profile.d/bash_completion.sh ]] ; then # shellcheck disable=SC1091 source /etc/profile.d/bash_completion.sh -elif [[ $OSTYPE == 'darwin'* ]] && _command_exists brew ; then - BREW_PREFIX=${BREW_PREFIX:-$(brew --prefix)} - +elif _bash_it_homebrew_check +then # homebrew/versions/bash-completion2 (required for projects.completion.bash) is installed to this path - if [[ -r "$BREW_PREFIX"/etc/bash_completion ]] ; then + if [[ -r "$BASH_IT_HOMEBREW_PREFIX"/etc/profile.d/bash_completion.sh ]] ; then # shellcheck disable=SC1090 - source "$BREW_PREFIX"/etc/bash_completion + source "$BASH_IT_HOMEBREW_PREFIX"/etc/profile.d/bash_completion.sh fi fi diff --git a/plugins/available/autojump.plugin.bash b/plugins/available/autojump.plugin.bash index 345ecadb..7e6df7fc 100644 --- a/plugins/available/autojump.plugin.bash +++ b/plugins/available/autojump.plugin.bash @@ -3,8 +3,8 @@ about-plugin 'Autojump configuration, see https://github.com/wting/autojump for # Only supports the Homebrew variant, Debian and Arch at the moment. # Feel free to provide a PR to support other install locations -if command -v brew &>/dev/null && [[ -s $(brew --prefix)/etc/profile.d/autojump.sh ]]; then - . $(brew --prefix)/etc/profile.d/autojump.sh +if _bash_it_homebrew_check && [[ -s "${BASH_IT_HOMEBREW_PREFIX}/etc/profile.d/autojump.sh" ]]; then + . "${BASH_IT_HOMEBREW_PREFIX}/etc/profile.d/autojump.sh" elif command -v dpkg &>/dev/null && dpkg -s autojump &>/dev/null ; then . "$(dpkg-query -S autojump.sh | cut -d' ' -f2)" elif command -v pacman &>/dev/null && pacman -Q autojump &>/dev/null ; then diff --git a/plugins/available/nvm.plugin.bash b/plugins/available/nvm.plugin.bash index e845d922..87dce644 100644 --- a/plugins/available/nvm.plugin.bash +++ b/plugins/available/nvm.plugin.bash @@ -9,9 +9,9 @@ about-plugin 'node version manager configuration' export NVM_DIR=${NVM_DIR:-$HOME/.nvm} # This loads nvm -if command -v brew &>/dev/null && [ -s $(brew --prefix nvm)/nvm.sh ] +if _bash_it_homebrew_check && [ -s "${BASH_IT_HOMEBREW_PREFIX}/nvm.sh" ] then - . $(brew --prefix nvm)/nvm.sh + . "${BASH_IT_HOMEBREW_PREFIX}/nvm.sh" else [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" fi From 0f28824ad5161ed0ad0e6c8c63713f28096fec7c Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sat, 14 Aug 2021 18:23:06 -0700 Subject: [PATCH 3/3] lib/helpers: invert test in _bash_it_homebrew_check() Check if `brew` is installed every time, and *unset* `$BASH_IT_HOMEBREW_PREFIX` if not found. This accounts for the edge-case of a user _uninstalling_ Homebrew without restarting the shell. --- lib/helpers.bash | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/helpers.bash b/lib/helpers.bash index 82c710a0..a3468a63 100755 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -50,14 +50,17 @@ function _completion_exists () function _bash_it_homebrew_check() { - if [[ "${BASH_IT_HOMEBREW_PREFIX:-unset}" == 'unset' ]] - then # variable isn't set - if _binary_exists 'brew' - then # Homebrew is installed + if _binary_exists 'brew' + then # Homebrew is installed + if [[ "${BASH_IT_HOMEBREW_PREFIX:-unset}" == 'unset' ]] + then # variable isn't set BASH_IT_HOMEBREW_PREFIX="$(brew --prefix)" - else # Homebrew is not installed. - false # return failure if brew not installed. + else + true # Variable is set already, don't invoke `brew`. fi + else # Homebrew is not installed. + BASH_IT_HOMEBREW_PREFIX= # clear variable, if set to anything. + false # return failure if brew not installed. fi }