Skip to content

Commit

Permalink
Use original brew from PATH
Browse files Browse the repository at this point in the history
This may not be the same as `HOMEBREW_BREW_FILE` so let's handle that
case instead.
  • Loading branch information
MikeMcQuaid committed Jan 6, 2025
1 parent 9331765 commit 37e8846
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 20 deletions.
8 changes: 4 additions & 4 deletions lib/bundle/brew_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def link_change_state!(verbose: false)
verb = "#{cmd}ing".capitalize
with_args = " with #{link_args.join(" ")}" if link_args.present?
puts "#{verb} #{@name} formula#{with_args}." if verbose
return Bundle.system(HOMEBREW_BREW_FILE, cmd, *link_args, @name, verbose:)
return Bundle.brew(cmd, *link_args, @name, verbose:)
end

true
Expand Down Expand Up @@ -226,7 +226,7 @@ def resolve_conflicts!(verbose:)
It is currently installed and conflicts with #{@name}.
EOS
end
return false unless Bundle.system(HOMEBREW_BREW_FILE, "unlink", conflict, verbose:)
return false unless Bundle.brew("unlink", conflict, verbose:)

if restart_service?
puts "Stopping #{conflict} service (if it is running)." if verbose
Expand All @@ -242,7 +242,7 @@ def install!(verbose:, force:)
install_args << "--force" << "--overwrite" if force
with_args = " with #{install_args.join(" ")}" if install_args.present?
puts "Installing #{@name} formula#{with_args}. It is not currently installed." if verbose
unless Bundle.system(HOMEBREW_BREW_FILE, "install", "--formula", @full_name, *install_args, verbose:)
unless Bundle.brew("install", "--formula", @full_name, *install_args, verbose:)
@changed = nil
return false
end
Expand All @@ -257,7 +257,7 @@ def upgrade!(verbose:, force:)
upgrade_args << "--force" if force
with_args = " with #{upgrade_args.join(" ")}" if upgrade_args.present?
puts "Upgrading #{@name} formula#{with_args}. It is installed but not up-to-date." if verbose
unless Bundle.system(HOMEBREW_BREW_FILE, "upgrade", "--formula", @name, *upgrade_args, verbose:)
unless Bundle.brew("upgrade", "--formula", @name, *upgrade_args, verbose:)
@changed = nil
return false
end
Expand Down
6 changes: 3 additions & 3 deletions lib/bundle/brew_services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ def reset!
def stop(name, verbose: false)
return true unless started?(name)

return unless Bundle.system HOMEBREW_BREW_FILE, "services", "stop", name, verbose: verbose
return unless Bundle.brew("services", "stop", name, verbose:)

started_services.delete(name)
true
end

def start(name, verbose: false)
return unless Bundle.system HOMEBREW_BREW_FILE, "services", "start", name, verbose: verbose
return unless Bundle.brew("services", "start", name, verbose:)

started_services << name
true
end

def restart(name, verbose: false)
return unless Bundle.system HOMEBREW_BREW_FILE, "services", "restart", name, verbose: verbose
return unless Bundle.brew("services", "restart", name, verbose:)

started_services << name
true
Expand Down
8 changes: 8 additions & 0 deletions lib/bundle/bundle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ def system(cmd, *args, verbose: false)
success
end

def which_brew
@which_brew ||= which("brew", ORIGINAL_PATHS)
end

def brew(*args, verbose: false)
system(which_brew, *args, verbose:)
end

def mas_installed?
@mas_installed ||= which_formula("mas")
end
Expand Down
4 changes: 2 additions & 2 deletions lib/bundle/cask_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def install(name, preinstall: true, no_upgrade: false, verbose: false, force: fa
if installed_casks.include?(name) && upgrading?(no_upgrade, name, options)
status = "#{options[:greedy] ? "may not be" : "not"} up-to-date"
puts "Upgrading #{name} cask. It is installed but #{status}." if verbose
return Bundle.system HOMEBREW_BREW_FILE, "upgrade", "--cask", full_name, verbose:
return Bundle.brew("upgrade", "--cask", full_name, verbose:)
end

args = options.fetch(:args, []).filter_map do |k, v|
Expand All @@ -54,7 +54,7 @@ def install(name, preinstall: true, no_upgrade: false, verbose: false, force: fa
with_args = " with #{args.join(" ")}" if args.present?
puts "Installing #{name} cask#{with_args}. It is not currently installed." if verbose

return false unless Bundle.system HOMEBREW_BREW_FILE, "install", "--cask", full_name, *args, verbose: verbose
return false unless Bundle.brew("install", "--cask", full_name, *args, verbose:)

installed_casks << name
true
Expand Down
10 changes: 5 additions & 5 deletions lib/bundle/commands/cleanup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@ def run(global: false, file: nil, force: false, zap: false, dsl: nil)
if force
if casks.any?
args = zap ? ["--zap"] : []
Kernel.system HOMEBREW_BREW_FILE, "uninstall", "--cask", *args, "--force", *casks
Kernel.system Bundle.which_brew, "uninstall", "--cask", *args, "--force", *casks
puts "Uninstalled #{casks.size} cask#{(casks.size == 1) ? "" : "s"}"
end

if formulae.any?
Kernel.system HOMEBREW_BREW_FILE, "uninstall", "--formula", "--force", *formulae
Kernel.system Bundle.which_brew, "uninstall", "--formula", "--force", *formulae
puts "Uninstalled #{formulae.size} formula#{(formulae.size == 1) ? "" : "e"}"
end

Kernel.system HOMEBREW_BREW_FILE, "untap", *taps if taps.any?
Kernel.system Bundle.which_brew, "untap", *taps if taps.any?

Bundle.exchange_uid_if_needed! do
vscode_extensions.each do |extension|
Kernel.system "code", "--uninstall-extension", extension
end
end

cleanup = system_output_no_stderr(HOMEBREW_BREW_FILE, "cleanup")
cleanup = system_output_no_stderr(Bundle.which_brew, "cleanup")
puts cleanup unless cleanup.empty?
else
would_uninstall = false
Expand Down Expand Up @@ -75,7 +75,7 @@ def run(global: false, file: nil, force: false, zap: false, dsl: nil)
would_uninstall = true
end

cleanup = system_output_no_stderr(HOMEBREW_BREW_FILE, "cleanup", "--dry-run")
cleanup = system_output_no_stderr(Bundle.which_brew, "cleanup", "--dry-run")
unless cleanup.empty?
puts "Would `brew cleanup`:"
puts cleanup
Expand Down
2 changes: 1 addition & 1 deletion lib/bundle/mac_app_store_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def reset!
def preinstall(name, id, no_upgrade: false, verbose: false)
unless Bundle.mas_installed?
puts "Installing mas. It is not currently installed." if verbose
Bundle.system(HOMEBREW_BREW_FILE, "install", "mas", verbose:)
Bundle.brew("install", "mas", verbose:)
raise "Unable to install #{name} app. mas installation failed." unless Bundle.mas_installed?
end

Expand Down
4 changes: 2 additions & 2 deletions lib/bundle/tap_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def install(name, preinstall: true, verbose: false, force: false, **options)
args.append("--force-auto-update") if options[:force_auto_update]

success = if options[:clone_target]
Bundle.system(HOMEBREW_BREW_FILE, "tap", name, options[:clone_target], *args, verbose:)
Bundle.brew("tap", name, options[:clone_target], *args, verbose:)
else
Bundle.system(HOMEBREW_BREW_FILE, "tap", name, *args, verbose:)
Bundle.brew("tap", name, *args, verbose:)
end

unless success
Expand Down
2 changes: 1 addition & 1 deletion lib/bundle/vscode_extension_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def reset!
def preinstall(name, no_upgrade: false, verbose: false)
if !Bundle.vscode_installed? && Bundle.cask_installed?
puts "Installing visual-studio-code. It is not currently installed." if verbose
Bundle.system HOMEBREW_BREW_FILE, "install", "--cask", "visual-studio-code", verbose:
Bundle.brew("install", "--cask", "visual-studio-code", verbose:)
end

if extension_installed?(name)
Expand Down
2 changes: 1 addition & 1 deletion lib/bundle/whalebrew_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def reset!
def preinstall(name, verbose: false, **_options)
unless Bundle.whalebrew_installed?
puts "Installing whalebrew. It is not currently installed." if verbose
Bundle.system(HOMEBREW_BREW_FILE, "install", "--formula", "whalebrew", verbose:)
Bundle.brew("install", "--formula", "whalebrew", verbose:)
raise "Unable to install #{name} app. Whalebrew installation failed." unless Bundle.whalebrew_installed?
end

Expand Down
1 change: 1 addition & 0 deletions spec/stub/global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
HOMEBREW_LIBRARY = Pathname(HOMEBREW_REPOSITORY/"Library").freeze
HOMEBREW_BREW_FILE = Pathname(HOMEBREW_PREFIX/"bin/brew").freeze
HOMEBREW_VERSION = "2.6.0"
ORIGINAL_PATHS = [Pathname("/usr/local/bin"), Pathname("/usr/bin"), Pathname("/bin")]

Check failure on line 8 in spec/stub/global.rb

View workflow job for this annotation

GitHub Actions / tests (ubuntu-latest)

Style/MutableConstant: Freeze mutable objects assigned to constants.

Check failure on line 8 in spec/stub/global.rb

View workflow job for this annotation

GitHub Actions / tests (macOS-latest)

Style/MutableConstant: Freeze mutable objects assigned to constants.

module Homebrew
module_function
Expand Down
2 changes: 1 addition & 1 deletion spec/stub/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "pathname"

def which(command)
def which(command, paths = ORIGINAL_PATHS)

Check failure on line 5 in spec/stub/utils.rb

View workflow job for this annotation

GitHub Actions / tests (ubuntu-latest)

Lint/UnusedMethodArgument: Unused method argument - `paths`. If it's necessary, use `_` or `_paths` as an argument name to indicate that it won't be used. If it's unnecessary, remove it.

Check failure on line 5 in spec/stub/utils.rb

View workflow job for this annotation

GitHub Actions / tests (macOS-latest)

Lint/UnusedMethodArgument: Unused method argument - `paths`. If it's necessary, use `_` or `_paths` as an argument name to indicate that it won't be used. If it's unnecessary, remove it.
Pathname("/usr/local/bin/#{command}")
end

Expand Down

0 comments on commit 37e8846

Please sign in to comment.