d2dc3bd2da531754778d1e9d7fb082715fc532f1

Author
Carlo Sala <carlosalag@protonmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(brew): run `brew shellenv` command at plugin load (#10874)

Diff

 1diff --git a/plugins/brew/README.md b/plugins/brew/README.md
 2index bf952bf010ca1028e3f0359b44d19adf0b1eba54..64e0e1153977eba1ad74464ae13ca1bc00448d4b 100644
 3--- a/plugins/brew/README.md
 4+++ b/plugins/brew/README.md
 5@@ -8,19 +8,23 @@ To use it, add `brew` to the plugins array of your zshrc file:
 6 plugins=(... brew)
 7 ```
 8 
 9+## Shellenv
10+
11+This plugin also executes `brew shellenv` at plugin load to set up many useful variables, such as `HOMEBREW_PREFIX` or `HOMEBREW_REPOSITORY`.
12+
13 ## Aliases
14 
15 | Alias    | Command                               | Description                                                         |
16-|----------|---------------------------------------|---------------------------------------------------------------------|
17+| -------- | ------------------------------------- | ------------------------------------------------------------------- |
18+| `bcubc`  | `brew upgrade --cask && brew cleanup` | Update outdated casks, then run cleanup.                            |
19+| `bcubo`  | `brew update && brew outdated --cask` | Update Homebrew data, then list outdated casks.                     |
20 | `brewp`  | `brew pin`                            | Pin a specified formula so that it's not upgraded.                  |
21 | `brews`  | `brew list -1`                        | List installed formulae or the installed files for a given formula. |
22 | `brewsp` | `brew list --pinned`                  | List pinned formulae, or show the version of a given formula.       |
23-| `bubo`   | `brew update && brew outdated`        | Update Homebrew data, then list outdated formulae and casks.        |
24 | `bubc`   | `brew upgrade && brew cleanup`        | Upgrade outdated formulae and casks, then run cleanup.              |
25+| `bubo`   | `brew update && brew outdated`        | Update Homebrew data, then list outdated formulae and casks.        |
26 | `bubu`   | `bubo && bubc`                        | Do the last two operations above.                                   |
27 | `buf`    | `brew upgrade --formula`              | Upgrade only formulas (not casks).                                  |
28-| `bcubo`  | `brew update && brew outdated --cask` | Update Homebrew data, then list outdated casks.                     |
29-| `bcubc`  | `brew upgrade --cask && brew cleanup` | Update outdated casks, then run cleanup.                            |
30 
31 ## Completion
32 
33diff --git a/plugins/brew/brew.plugin.zsh b/plugins/brew/brew.plugin.zsh
34index 070a083d0c69dc98465d0751af253a3cb2c3062c..4a2322216d8585f691306f8ffad2128dd6ff7480 100644
35--- a/plugins/brew/brew.plugin.zsh
36+++ b/plugins/brew/brew.plugin.zsh
37@@ -1,11 +1,35 @@
38+if (( ! $+commands[brew] )); then
39+  if [[ -x /opt/homebrew/bin/brew ]]; then
40+    BREW_LOCATION="/opt/homebrew/bin/brew"
41+  elif [[ -x /usr/local/bin/brew ]]; then
42+    BREW_LOCATION="/usr/local/bin/brew"
43+  elif [[ -x /home/linuxbrew/.linuxbrew/bin/brew ]]; then
44+    BREW_LOCATION="/home/linuxbrew/.linuxbrew/bin/brew"
45+  elif [[ -x "$HOME/.linuxbrew/bin/brew" ]]; then
46+    BREW_LOCATION="$HOME/.linuxbrew/bin/brew"
47+  else
48+    return
49+  fi
50+fi
51+
52+if [[ -z "$HOMEBREW_PREFIX" ]]; then
53+  if [[ -z $BREW_LOCATION ]]; then
54+    eval "$(brew shellenv)"
55+  else
56+    eval "$("$BREW_LOCATION" shellenv)"
57+  fi
58+fi
59+
60+unset BREW_LOCATION
61+
62+alias bcubc='brew upgrade --cask && brew cleanup'
63+alias bcubo='brew update && brew outdated --cask'
64 alias brewp='brew pin'
65 alias brewsp='brew list --pinned'
66-alias bubo='brew update && brew outdated'
67 alias bubc='brew upgrade && brew cleanup'
68+alias bubo='brew update && brew outdated'
69 alias bubu='bubo && bubc'
70 alias buf='brew upgrade --formula'
71-alias bcubo='brew update && brew outdated --cask'
72-alias bcubc='brew upgrade --cask && brew cleanup'
73 
74 function brews() {
75   local formulae="$(brew leaves | xargs brew deps --installed --for-each)"