]> arthur.barton.de Git - ax-zsh.git/blobdiff - plugins/homebrew/homebrew.zshrc
homebrew: Enhance code, no functional changes
[ax-zsh.git] / plugins / homebrew / homebrew.zshrc
index 34f0b69a4043b8409de04190a3773852f4ce3b46..12a0f2206b14838d3309f1e7349f6da764db82fc 100644 (file)
@@ -18,38 +18,39 @@ function brew() {
        #   installed by Homebrew.
        # - Call the "real" brew(1) command.
 
-       if [ -x /home/linuxbrew/.linuxbrew/bin/brew ]; then
+       if [[ -x /home/linuxbrew/.linuxbrew/bin/brew ]]; then
                real_brew_cmd="/home/linuxbrew/.linuxbrew/bin/brew"
-       elif [ -x /opt/homebrew/bin/brew ]; then
+       elif [[ -x /opt/homebrew/bin/brew ]]; then
                real_brew_cmd="/opt/homebrew/bin/brew"
-       elif [ -x /usr/local/bin/brew ]; then
+       elif [[ -x /usr/local/bin/brew ]]; then
                real_brew_cmd="/usr/local/bin/brew"
        else
-               if [ "$1" != "shellenv" ] && [ "$1" != "--prefix" ]; then
+               if [[ "$1" != "shellenv" && "$1" != "--prefix" ]]; then
                        echo "Oops, real \"brew\" command not found!? [for \"$1\"]" >&2
                fi
                return 101
        fi
 
-       brew_prefix=$("$real_brew_cmd" --prefix) || return 102
+       if [[ -z "$HOMEBREW_REPOSITORY" ]]; then
+               eval "$("$real_brew_cmd" shellenv)"
+       fi
 
-       if [[ $(/bin/ls -ldn "$brew_prefix" | awk '{print $3}') -eq $UID ]]; then
+       if [[ $(/bin/ls -ldn "$HOMEBREW_REPOSITORY" | awk '{print $3}') -eq $UID ]]; then
                # We are the owner of the Homebrew installation.
                (
-                       [[ $# -eq 0 && -t 1 ]] \
-                               && echo "Running \"$real_brew_cmd\" ..."
+                       [[ $# -eq 0 && -t 1 ]] && echo "Running \"$real_brew_cmd\" ..."
                        umask 0022 || return 103
                        "$real_brew_cmd" "$@"
                )
        else
-               # We are a different user than the owner of the Homebew
+               # We are a different user than the owner of the Homebrew
                # installation. So we need to change the user when running the
                # real "brew" command!
                priv_exec="umask 0022 || exit 103; \"$real_brew_cmd\" $*"
                (
                        cd /tmp
-                       user="$(/bin/ls -ld "$brew_prefix" | awk '{print $3}')"
-                       group="$(/bin/ls -ld "$brew_prefix" | awk '{print $4}')"
+                       user="$(/bin/ls -ld "$HOMEBREW_REPOSITORY" | awk '{print $3}')"
+                       group="$(/bin/ls -ld "$HOMEBREW_REPOSITORY" | awk '{print $4}')"
                        [[ $# -eq 0 && -t 1 ]] \
                                && echo "Running \"$real_brew_cmd\" as user \"$user:$group\" ..."
                        sudo -u "$user" -g "$group" -- sh -c "$priv_exec"