Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version_file: DSL to Brewfile #19579

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Library/Homebrew/bundle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,34 @@ def exchange_uid_if_needed!(&block)

return_value
end

def formula_versions_from_env
@formula_versions_from_env ||= begin
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
end
end

sig { void }
def reset!
@mas_installed = nil
@vscode_installed = nil
@whalebrew_installed = nil
@cask_installed = nil
@formula_versions_from_env = nil
end
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions Library/Homebrew/bundle/brew_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
@start_service = options.fetch(:start_service, @restart_service)
@link = options.fetch(:link, nil)
@postinstall = options.fetch(:postinstall, nil)
@version_file = options.fetch(:version_file, nil)
@changed = nil
end

Expand Down Expand Up @@ -57,6 +58,19 @@

postinstall_result = postinstall_change_state!(verbose:)
result &&= postinstall_result

if result && @version_file.present?
# Use the version from the environment if it hasn't changed.
version = if !changed? && (env_version = Bundle.formula_versions_from_env[@name])
env_version
else
Formula[@full_name].version.to_s
end
version_path = Pathname.new(@version_file)
version_path.write("#{version}\n")

Check warning on line 70 in Library/Homebrew/bundle/brew_installer.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/bundle/brew_installer.rb#L69-L70

Added lines #L69 - L70 were not covered by tests

puts "Wrote #{@name} version #{version} to #{@version_file}" if verbose
end
end

result
Expand Down
13 changes: 1 addition & 12 deletions Library/Homebrew/bundle/commands/exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,7 @@ def self.run(*args, global: false, file: nil, subcommand: "")
end

# 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|
Bundle.formula_versions_from_env.each do |formula_name, formula_version|
ENV.each do |key, value|
opt = %r{/opt/#{formula_name}([/:$])}
next unless value.match(opt)
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/test/bundle/commands/exec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
context "with valid command setup" do
before do
allow(described_class).to receive(:exec).and_return(nil)
Homebrew::Bundle.reset!
end

it "does not raise an error" do
Expand Down
2 changes: 2 additions & 0 deletions docs/Brew-Bundle-and-Brewfile.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ brew "[email protected]", restart_service: :changed, link: true, conflicts_with: ["mysql
# 'brew install' and run a command if installer or upgraded.
brew "postgresql@16",
postinstall: "${HOMEBREW_PREFIX}/opt/postgresql@16/bin/postgres -D ${HOMEBREW_PREFIX}/var/postgresql@16"
# 'brew install' and write the installed version to the '.ruby-version' file.
brew "ruby", version_file: ".ruby-version"
# install only on specified OS
brew "gnupg" if OS.mac?
brew "glibc" if OS.linux?
Expand Down
Loading