Skip to content

Commit 1d2f045

Browse files
authored
Merge pull request #20047 from Homebrew/brew_sh_profiles
`brew (bundle|) sh`: use user's configuration but override prompts.
2 parents 485f1ab + 5fe43ed commit 1d2f045

File tree

6 files changed

+80
-23
lines changed

6 files changed

+80
-23
lines changed

Library/Homebrew/bundle/commands/exec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,18 @@ def self.run(*args, global: false, file: nil, subcommand: "", services: false, c
170170
end
171171
end
172172
return
173+
elsif subcommand == "sh"
174+
preferred_path = Utils::Shell.preferred_path(default: "/bin/bash")
175+
notice = unless Homebrew::EnvConfig.no_env_hints?
176+
<<~EOS
177+
Your shell has been configured to use a build environment from your `Brewfile`.
178+
This should help you build stuff.
179+
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
180+
When done, type `exit`.
181+
EOS
182+
end
183+
ENV["HOMEBREW_FORCE_API_AUTO_UPDATE"] = nil
184+
args = [Utils::Shell.shell_with_prompt("brew bundle", preferred_path:, notice:)]
173185
end
174186

175187
if services

Library/Homebrew/cmd/bundle.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -276,17 +276,7 @@ def run
276276
_subcommand, *named_args = args.named
277277
named_args
278278
when "sh"
279-
preferred_path = Utils::Shell.preferred_path(default: "/bin/bash")
280-
notice = unless Homebrew::EnvConfig.no_env_hints?
281-
<<~EOS
282-
Your shell has been configured to use a build environment from your `Brewfile`.
283-
This should help you build stuff.
284-
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
285-
When done, type `exit`.
286-
EOS
287-
end
288-
ENV["HOMEBREW_FORCE_API_AUTO_UPDATE"] = nil
289-
[Utils::Shell.shell_with_prompt("brew bundle", preferred_path:, notice:)]
279+
["sh"]
290280
when "env"
291281
["env"]
292282
end

Library/Homebrew/test/utils/shell_spec.rb

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,34 @@
107107
end
108108

109109
describe "::shell_with_prompt" do
110+
let(:home) { HOMEBREW_TEMP }
111+
let(:notice) { "" }
112+
let(:prompt) { "test" }
113+
let(:path) { "/some/path" }
114+
110115
it "returns zsh-specific prompt configuration" do
111-
ENV["SHELL"] = "/bin/zsh"
112-
expect(described_class.shell_with_prompt("test", preferred_path: "/bin/zsh", notice: "")).to eq(
113-
"PROMPT='%B%F{green}test%f %F{blue}$%f%b ' RPROMPT='[%B%F{red}%~%f%b]' /bin/zsh -f",
114-
)
116+
preferred_path = "/bin/zsh"
117+
ENV["SHELL"] = preferred_path
118+
ENV["PATH"] = path
119+
zdotdir = "#{HOMEBREW_TEMP}/brew-zsh-prompt-#{Process.euid}"
120+
expect(described_class.shell_with_prompt(prompt, preferred_path:, notice:, home:)).to eq \
121+
"BREW_PROMPT_PATH=\"#{path}\" BREW_PROMPT_TYPE=\"#{prompt}\" ZDOTDIR=\"#{zdotdir}\" #{preferred_path}"
115122
end
116123

117-
it "returns generic shell prompt configuration" do
124+
it "returns bash-specific prompt configuration" do
125+
preferred_path = "/bin/bash"
118126
ENV["SHELL"] = "/bin/bash"
119-
expect(described_class.shell_with_prompt("test", preferred_path: "/bin/bash", notice: "")).to eq(
120-
"PS1=\"\\[\\033[1;32m\\]brew \\[\\033[1;31m\\]\\w \\[\\033[1;34m\\]$\\[\\033[0m\\] \" /bin/bash",
121-
)
127+
ENV["PATH"] = path
128+
rcfile = "#{HOMEBREW_LIBRARY_PATH}/utils/bash/brew-sh-prompt-bashrc.bash"
129+
expect(described_class.shell_with_prompt(prompt, preferred_path:, notice:, home:)).to eq \
130+
"BREW_PROMPT_PATH=\"#{path}\" BREW_PROMPT_TYPE=\"#{prompt}\" #{preferred_path} --rcfile \"#{rcfile}\""
131+
end
132+
133+
it "returns generic shell prompt configuration" do
134+
preferred_path = "/bin/dash"
135+
ENV["SHELL"] = preferred_path
136+
expect(described_class.shell_with_prompt(prompt, preferred_path:, notice:, home:)).to eq \
137+
"PS1=\"\\[\\033[1;32m\\]#{prompt} \\[\\033[1;31m\\]\\w \\[\\033[1;34m\\]$\\[\\033[0m\\] \" #{preferred_path}"
122138
end
123139

124140
it "outputs notice when provided" do
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Read the user's ~/.bashrc first
2+
if [[ -f "${HOME}/.bashrc" ]]
3+
then
4+
source "${HOME}/.bashrc"
5+
fi
6+
7+
# Override the user's Bash prompt with our custom prompt
8+
export PS1="\\[\\033[1;32m\\]${BREW_PROMPT_TYPE} \\[\\033[1;31m\\]\\w \\[\\033[1;34m\\]$\\[\\033[0m\\] "
9+
10+
# Add the Homebrew PATH in front of the user's PATH
11+
export PATH="${BREW_PROMPT_PATH}:${PATH}"
12+
unset BREW_PROMPT_TYPE BREW_PROMPT_PATH

Library/Homebrew/utils/shell.rb

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,28 @@ def sh_quote(str)
153153
str
154154
end
155155

156-
sig { params(type: String, preferred_path: String, notice: T.nilable(String)).returns(String) }
157-
def shell_with_prompt(type, preferred_path:, notice:)
156+
sig { params(type: String, preferred_path: String, notice: T.nilable(String), home: String).returns(String) }
157+
def shell_with_prompt(type, preferred_path:, notice:, home: Dir.home)
158158
preferred = from_path(preferred_path)
159+
path = ENV.fetch("PATH")
159160
subshell = case preferred
160161
when :zsh
161-
"PROMPT='%B%F{green}#{type}%f %F{blue}$%f%b ' RPROMPT='[%B%F{red}%~%f%b]' #{preferred_path} -f"
162+
zdotdir = Pathname.new(HOMEBREW_TEMP/"brew-zsh-prompt-#{Process.euid}")
163+
zdotdir.mkpath
164+
FileUtils.chmod_R(0700, zdotdir)
165+
FileUtils.cp(HOMEBREW_LIBRARY_PATH/"utils/zsh/brew-sh-prompt-zshrc.zsh", zdotdir/".zshrc")
166+
%w[.zcompdump .zsh_history .zsh_sessions].each do |file|
167+
FileUtils.ln_sf("#{home}/#{file}", zdotdir/file)
168+
end
169+
<<~ZSH.strip
170+
BREW_PROMPT_PATH="#{path}" BREW_PROMPT_TYPE="#{type}" ZDOTDIR="#{zdotdir}" #{preferred_path}
171+
ZSH
172+
when :bash
173+
<<~BASH.strip
174+
BREW_PROMPT_PATH="#{path}" BREW_PROMPT_TYPE="#{type}" #{preferred_path} --rcfile "#{HOMEBREW_LIBRARY_PATH}/utils/bash/brew-sh-prompt-bashrc.bash"
175+
BASH
162176
else
163-
"PS1=\"\\[\\033[1;32m\\]brew \\[\\033[1;31m\\]\\w \\[\\033[1;34m\\]$\\[\\033[0m\\] \" #{preferred_path}"
177+
"PS1=\"\\[\\033[1;32m\\]#{type} \\[\\033[1;31m\\]\\w \\[\\033[1;34m\\]$\\[\\033[0m\\] \" #{preferred_path}"
164178
end
165179

166180
puts notice if notice.present?
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Read the user's ~/.zshrc first
2+
if [[ -f "${HOME}/.zshrc" ]]
3+
then
4+
source "${HOME}/.zshrc"
5+
fi
6+
7+
# Override the user's ZSH prompt with our custom prompt
8+
export PROMPT="%B%F{green}${BREW_PROMPT_TYPE}%f %F{blue}$%f%b "
9+
export RPROMPT="[%B%F{red}%~%f%b]"
10+
11+
# Add the Homebrew PATH in front of the user's PATH
12+
export PATH="${BREW_PROMPT_PATH}:${PATH}"
13+
unset BREW_PROMPT_TYPE BREW_PROMPT_PATH

0 commit comments

Comments
 (0)