Skip to content

Commit ece1c6f

Browse files
committed
pre-compute brew shellenv at install time for faster shell startup
Instead of running `eval "$(brew shellenv)"` on every shell start (~100-200ms subprocess), generate the output once during cps init and save to ~/shell/env/brew.zsh. rc-base.zsh sources the static file instead.
1 parent 14146ef commit ece1c6f

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

internal/configs/rc-base.zsh

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
# --- Homebrew ---
22
export HOMEBREW_NO_AUTO_UPDATE=1
3-
if [ -x /opt/homebrew/bin/brew ]; then
4-
eval "$(/opt/homebrew/bin/brew shellenv)"
5-
elif [ -x /home/linuxbrew/.linuxbrew/bin/brew ]; then
6-
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
7-
elif [ -x /usr/local/bin/brew ]; then
8-
eval "$(/usr/local/bin/brew shellenv)"
9-
fi
3+
[ -f "$HOME/shell/env/brew.zsh" ] && source "$HOME/shell/env/brew.zsh"
104

115
# --- Oh My Zsh ---
126
export ZSH="$HOME/.oh-my-zsh"

internal/runner/runner.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func Init(ghToken string) {
4949
p.ShellExecDir(),
5050
filepath.Join(p.ShellDir(), "rc"),
5151
filepath.Join(p.ShellDir(), "rc", "custom"),
52+
filepath.Join(p.ShellDir(), "env"),
5253
filepath.Join(p.ShellDir(), "custom"),
5354
filepath.Join(p.ConfigDir(), "extensions"),
5455
} {
@@ -239,6 +240,7 @@ func runPostInstall(p platform.Platform) {
239240
}
240241
}
241242

243+
generateShellEnv(p, &errors, &lineCount)
242244
generateCompletions(p, &errors, &lineCount)
243245

244246
utils.ClearLines(lineCount + 1) // sub-lines + running header
@@ -252,6 +254,31 @@ func runPostInstall(p platform.Platform) {
252254
}
253255
}
254256

257+
func generateShellEnv(p platform.Platform, errors *[]jobResult, lineCount *int) {
258+
envDir := filepath.Join(p.ShellDir(), "env")
259+
if err := os.MkdirAll(envDir, 0755); err != nil {
260+
*errors = append(*errors, jobResult{name: "shell-env", err: err})
261+
return
262+
}
263+
264+
brewBin, err := exec.LookPath("brew")
265+
if err != nil {
266+
return // brew not found, nothing to generate
267+
}
268+
269+
utils.PrintIndentedRunning("shell-env: brew")
270+
*lineCount++
271+
cmd := exec.Command(brewBin, "shellenv")
272+
out, err := cmd.Output()
273+
if err != nil {
274+
*errors = append(*errors, jobResult{name: "shell-env-brew", err: err})
275+
return
276+
}
277+
if err := os.WriteFile(filepath.Join(envDir, "brew.zsh"), out, 0644); err != nil {
278+
*errors = append(*errors, jobResult{name: "shell-env-brew", err: err})
279+
}
280+
}
281+
255282
func generateCompletions(p platform.Platform, errors *[]jobResult, lineCount *int) {
256283
compDir := filepath.Join(p.ShellDir(), "completions")
257284
if err := os.MkdirAll(compDir, 0755); err != nil {

0 commit comments

Comments
 (0)