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

bundle: require more lazily #19568

Merged
merged 1 commit into from
Mar 24, 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
125 changes: 87 additions & 38 deletions Library/Homebrew/bundle.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,89 @@
# typed: strict
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true

require "bundle/brewfile"
require "bundle/bundle"
require "bundle/dsl"
require "bundle/adder"
require "bundle/checker"
require "bundle/remover"
require "bundle/skipper"
require "bundle/brew_services"
require "bundle/brew_service_checker"
require "bundle/brew_installer"
require "bundle/brew_checker"
require "bundle/cask_installer"
require "bundle/mac_app_store_installer"
require "bundle/mac_app_store_checker"
require "bundle/tap_installer"
require "bundle/brew_dumper"
require "bundle/cask_dumper"
require "bundle/cask_checker"
require "bundle/mac_app_store_dumper"
require "bundle/tap_dumper"
require "bundle/tap_checker"
require "bundle/dumper"
require "bundle/installer"
require "bundle/lister"
require "bundle/commands/install"
require "bundle/commands/dump"
require "bundle/commands/cleanup"
require "bundle/commands/check"
require "bundle/commands/exec"
require "bundle/commands/list"
require "bundle/commands/add"
require "bundle/commands/remove"
require "bundle/whalebrew_installer"
require "bundle/whalebrew_dumper"
require "bundle/vscode_extension_checker"
require "bundle/vscode_extension_dumper"
require "bundle/vscode_extension_installer"
require "English"

module Homebrew
module Bundle
class << self
def system(cmd, *args, verbose: false)
return super cmd, *args if verbose

logs = []
success = T.let(nil, T.nilable(T::Boolean))
IO.popen([cmd, *args], err: [:child, :out]) do |pipe|
while (buf = pipe.gets)
logs << buf
end
Process.wait(pipe.pid)
success = $CHILD_STATUS.success?
pipe.close
end
puts logs.join unless success
success
end

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

def mas_installed?
@mas_installed ||= which_formula("mas")
end

def vscode_installed?
@vscode_installed ||= which_vscode.present?
end

def which_vscode
@which_vscode ||= which("code", ORIGINAL_PATHS)
@which_vscode ||= which("codium", ORIGINAL_PATHS)
@which_vscode ||= which("cursor", ORIGINAL_PATHS)
@which_vscode ||= which("code-insiders", ORIGINAL_PATHS)
end

def whalebrew_installed?
@whalebrew_installed ||= which_formula("whalebrew")
end

def cask_installed?
@cask_installed ||= File.directory?("#{HOMEBREW_PREFIX}/Caskroom") &&
(File.directory?("#{HOMEBREW_LIBRARY}/Taps/homebrew/homebrew-cask") ||
!Homebrew::EnvConfig.no_install_from_api?)
end

def which_formula(name)
formula = Formulary.factory(name)
ENV["PATH"] = "#{formula.opt_bin}:#{ENV.fetch("PATH", nil)}" if formula.any_version_installed?
which(name).present?
end

def exchange_uid_if_needed!(&block)
euid = Process.euid
uid = Process.uid
return yield if euid == uid

old_euid = euid
process_reexchangeable = Process::UID.re_exchangeable?
if process_reexchangeable
Process::UID.re_exchange
else
Process::Sys.seteuid(uid)
end

home = T.must(Etc.getpwuid(Process.uid)).dir
return_value = with_env("HOME" => home, &block)

if process_reexchangeable
Process::UID.re_exchange
else
Process::Sys.seteuid(old_euid)
end

return_value
end
end
end
end

require "extend/os/bundle/bundle"
3 changes: 3 additions & 0 deletions Library/Homebrew/bundle/adder.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true

require "bundle/brewfile"
require "bundle/dumper"

module Homebrew
module Bundle
module Adder
Expand Down
2 changes: 2 additions & 0 deletions Library/Homebrew/bundle/brew_checker.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true

require "bundle/brew_installer"

module Homebrew
module Bundle
module Checker
Expand Down
4 changes: 4 additions & 0 deletions Library/Homebrew/bundle/brew_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module BrewDumper
module_function

def reset!
require "bundle/brew_services"

Homebrew::Bundle::BrewServices.reset!
@formulae = nil
@formulae_by_full_name = nil
Expand Down Expand Up @@ -54,6 +56,8 @@ def formulae_by_name(name)
end

def dump(describe: false, no_restart: false)
require "bundle/brew_services"

requested_formula = formulae.select do |f|
f[:installed_on_request?] || !f[:installed_as_dependency?]
end
Expand Down
14 changes: 10 additions & 4 deletions Library/Homebrew/bundle/brew_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def start_service?
end

def start_service_needed?
require "bundle/brew_services"
start_service? && !BrewServices.started?(@full_name)
end

Expand All @@ -96,6 +97,7 @@ def changed?
end

def service_change_state!(verbose:)
require "bundle/brew_services"
if restart_service_needed?
puts "Restarting #{@name} service." if verbose
BrewServices.restart(@full_name, verbose:)
Expand Down Expand Up @@ -156,6 +158,7 @@ def self.formula_in_array?(formula, array)
return true if array.include?(formula)
return true if array.include?(formula.split("/").last)

require "bundle/brew_dumper"
old_names = Homebrew::Bundle::BrewDumper.formula_oldnames
old_name = old_names[formula]
old_name ||= old_names[formula.split("/").last]
Expand Down Expand Up @@ -195,6 +198,7 @@ def self.pinned_formulae
end

def self.formulae
require "bundle/brew_dumper"
Homebrew::Bundle::BrewDumper.formulae
end

Expand Down Expand Up @@ -225,6 +229,7 @@ def conflicts_with
conflicts_with = Set.new
conflicts_with += @conflicts_with_arg

require "bundle/brew_dumper"
if (formula = Homebrew::Bundle::BrewDumper.formulae_by_full_name(@full_name)) &&
(formula_conflicts_with = formula[:conflicts_with])
conflicts_with += formula_conflicts_with
Expand All @@ -246,10 +251,11 @@ def resolve_conflicts!(verbose:)
end
return false unless Bundle.brew("unlink", conflict, verbose:)

if restart_service?
puts "Stopping #{conflict} service (if it is running)." if verbose
BrewServices.stop(conflict, verbose:)
end
next unless restart_service?

require "bundle/brew_services"
puts "Stopping #{conflict} service (if it is running)." if verbose
BrewServices.stop(conflict, verbose:)
end

true
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/bundle/brew_service_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def installed_and_up_to_date?(formula, no_upgrade: false)
end

def entry_to_formula(entry)
require "bundle/brew_installer"
Homebrew::Bundle::BrewInstaller.new(entry.name, entry.options)
end

Expand All @@ -32,10 +33,12 @@ def formula_needs_to_start?(formula)
end

def service_is_started?(service_name)
require "bundle/brew_services"
Homebrew::Bundle::BrewServices.started?(service_name)
end

def lookup_old_name(service_name)
require "bundle/brew_dumper"
@old_names ||= Homebrew::Bundle::BrewDumper.formula_oldnames
old_name = @old_names[service_name]
old_name ||= @old_names[service_name.split("/").last]
Expand Down
2 changes: 2 additions & 0 deletions Library/Homebrew/bundle/brewfile.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true

require "bundle/dsl"

module Homebrew
module Bundle
module Brewfile
Expand Down
89 changes: 0 additions & 89 deletions Library/Homebrew/bundle/bundle.rb

This file was deleted.

2 changes: 2 additions & 0 deletions Library/Homebrew/bundle/cask_checker.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true

require "bundle/cask_installer"

module Homebrew
module Bundle
module Checker
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/bundle/cask_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def self.reset!
return true if outdated_casks.include?(name)
return false unless options[:greedy]

require "bundle/cask_dumper"
Homebrew::Bundle::CaskDumper.cask_is_outdated_using_greedy?(name)
end

Expand Down Expand Up @@ -95,10 +96,12 @@ def self.cask_upgradable?(cask)
end

def self.installed_casks
require "bundle/cask_dumper"
@installed_casks ||= Homebrew::Bundle::CaskDumper.cask_names
end

def self.outdated_casks
require "bundle/cask_dumper"
@outdated_casks ||= Homebrew::Bundle::CaskDumper.outdated_cask_names
end
end
Expand Down
Loading
Loading