Skip to content

Commit

Permalink
commands/exec: allow customising environment.
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
MikeMcQuaid committed Jan 23, 2025
1 parent f5acb38 commit ac034c0
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion lib/bundle/commands/exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ def run(*args, global: false, file: nil)

Formulary.factory(entry.name)
end
ENV.keg_only_deps = ENV.deps.select(&:keg_only?)

# Allow setting all dependencies to be keg-only
# (i.e. should be explicitly in HOMEBREW_*PATHs ahead of HOMEBREW_PREFIX)
ENV.keg_only_deps = if ENV["HOMEBREW_BUNDLE_EXEC_ALL_KEG_ONLY_DEPS"].present?
ENV.delete("HOMEBREW_BUNDLE_EXEC_ALL_KEG_ONLY_DEPS")
ENV.deps
else
ENV.deps.select(&:keg_only?)
end
ENV.setup_build_environment

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

# Replace the formula versions from the environment variables
formula_versions = {}
ENV.each do |key, value|
match = key.match(/^HOMEBREW_BUNDLE_EXEC_FORMULA_VERSION_(.+)$/)
next if match.blank?

formula_name = match[1]
next if formula_name.blank?

ENV.delete(key)
formula_versions[formula_name.downcase] = value
end
formula_versions.each do |formula_name, formula_version|
ENV.each do |key, value|
opt = %r{/opt/#{formula_name}([/:$])}
next unless value.match(opt)

ENV[key] = value.gsub(opt, "/Cellar/#{formula_name}/#{formula_version}\\1")
end
end

exec(*args)
end
end
Expand Down

0 comments on commit ac034c0

Please sign in to comment.