Skip to content
This repository was archived by the owner on Mar 20, 2025. It is now read-only.

Commit ac034c0

Browse files
committed
commands/exec: allow customising environment.
Provide `HOMEBREW_BUNDLE_EXEC_ALL_KEG_ONLY_DEPS` and `HOMEBREW_BUNDLE_EXEC_FORMULA_VERSION_*` variables to allow adjusting the environment variables that are set in `brew bundle exec`.
1 parent f5acb38 commit ac034c0

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

lib/bundle/commands/exec.rb

+30-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@ def run(*args, global: false, file: nil)
3535

3636
Formulary.factory(entry.name)
3737
end
38-
ENV.keg_only_deps = ENV.deps.select(&:keg_only?)
38+
39+
# Allow setting all dependencies to be keg-only
40+
# (i.e. should be explicitly in HOMEBREW_*PATHs ahead of HOMEBREW_PREFIX)
41+
ENV.keg_only_deps = if ENV["HOMEBREW_BUNDLE_EXEC_ALL_KEG_ONLY_DEPS"].present?
42+
ENV.delete("HOMEBREW_BUNDLE_EXEC_ALL_KEG_ONLY_DEPS")
43+
ENV.deps
44+
else
45+
ENV.deps.select(&:keg_only?)
46+
end
3947
ENV.setup_build_environment
4048

4149
# Enable compiler flag filtering
@@ -58,6 +66,27 @@ def run(*args, global: false, file: nil)
5866
# Ensure the Ruby path we saved goes before anything else, if the command was in the PATH
5967
ENV.prepend_path "PATH", command_path if command_path.present?
6068

69+
# Replace the formula versions from the environment variables
70+
formula_versions = {}
71+
ENV.each do |key, value|
72+
match = key.match(/^HOMEBREW_BUNDLE_EXEC_FORMULA_VERSION_(.+)$/)
73+
next if match.blank?
74+
75+
formula_name = match[1]
76+
next if formula_name.blank?
77+
78+
ENV.delete(key)
79+
formula_versions[formula_name.downcase] = value
80+
end
81+
formula_versions.each do |formula_name, formula_version|
82+
ENV.each do |key, value|
83+
opt = %r{/opt/#{formula_name}([/:$])}
84+
next unless value.match(opt)
85+
86+
ENV[key] = value.gsub(opt, "/Cellar/#{formula_name}/#{formula_version}\\1")
87+
end
88+
end
89+
6190
exec(*args)
6291
end
6392
end

0 commit comments

Comments
 (0)