Skip to content

Commit bce5253

Browse files
committed
bundle: require more lazily
This will hopefully make using `brew bundle` a little bit snappier.
1 parent f3bd91d commit bce5253

30 files changed

+202
-131
lines changed

Library/Homebrew/bundle.rb

+87-38
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,89 @@
1-
# typed: strict
1+
# typed: true # rubocop:todo Sorbet/StrictSigil
22
# frozen_string_literal: true
33

4-
require "bundle/brewfile"
5-
require "bundle/bundle"
6-
require "bundle/dsl"
7-
require "bundle/adder"
8-
require "bundle/checker"
9-
require "bundle/remover"
10-
require "bundle/skipper"
11-
require "bundle/brew_services"
12-
require "bundle/brew_service_checker"
13-
require "bundle/brew_installer"
14-
require "bundle/brew_checker"
15-
require "bundle/cask_installer"
16-
require "bundle/mac_app_store_installer"
17-
require "bundle/mac_app_store_checker"
18-
require "bundle/tap_installer"
19-
require "bundle/brew_dumper"
20-
require "bundle/cask_dumper"
21-
require "bundle/cask_checker"
22-
require "bundle/mac_app_store_dumper"
23-
require "bundle/tap_dumper"
24-
require "bundle/tap_checker"
25-
require "bundle/dumper"
26-
require "bundle/installer"
27-
require "bundle/lister"
28-
require "bundle/commands/install"
29-
require "bundle/commands/dump"
30-
require "bundle/commands/cleanup"
31-
require "bundle/commands/check"
32-
require "bundle/commands/exec"
33-
require "bundle/commands/list"
34-
require "bundle/commands/add"
35-
require "bundle/commands/remove"
36-
require "bundle/whalebrew_installer"
37-
require "bundle/whalebrew_dumper"
38-
require "bundle/vscode_extension_checker"
39-
require "bundle/vscode_extension_dumper"
40-
require "bundle/vscode_extension_installer"
4+
require "English"
5+
6+
module Homebrew
7+
module Bundle
8+
class << self
9+
def system(cmd, *args, verbose: false)
10+
return super cmd, *args if verbose
11+
12+
logs = []
13+
success = T.let(nil, T.nilable(T::Boolean))
14+
IO.popen([cmd, *args], err: [:child, :out]) do |pipe|
15+
while (buf = pipe.gets)
16+
logs << buf
17+
end
18+
Process.wait(pipe.pid)
19+
success = $CHILD_STATUS.success?
20+
pipe.close
21+
end
22+
puts logs.join unless success
23+
success
24+
end
25+
26+
def brew(*args, verbose: false)
27+
system(HOMEBREW_BREW_FILE, *args, verbose:)
28+
end
29+
30+
def mas_installed?
31+
@mas_installed ||= which_formula("mas")
32+
end
33+
34+
def vscode_installed?
35+
@vscode_installed ||= which_vscode.present?
36+
end
37+
38+
def which_vscode
39+
@which_vscode ||= which("code", ORIGINAL_PATHS)
40+
@which_vscode ||= which("codium", ORIGINAL_PATHS)
41+
@which_vscode ||= which("cursor", ORIGINAL_PATHS)
42+
@which_vscode ||= which("code-insiders", ORIGINAL_PATHS)
43+
end
44+
45+
def whalebrew_installed?
46+
@whalebrew_installed ||= which_formula("whalebrew")
47+
end
48+
49+
def cask_installed?
50+
@cask_installed ||= File.directory?("#{HOMEBREW_PREFIX}/Caskroom") &&
51+
(File.directory?("#{HOMEBREW_LIBRARY}/Taps/homebrew/homebrew-cask") ||
52+
!Homebrew::EnvConfig.no_install_from_api?)
53+
end
54+
55+
def which_formula(name)
56+
formula = Formulary.factory(name)
57+
ENV["PATH"] = "#{formula.opt_bin}:#{ENV.fetch("PATH", nil)}" if formula.any_version_installed?
58+
which(name).present?
59+
end
60+
61+
def exchange_uid_if_needed!(&block)
62+
euid = Process.euid
63+
uid = Process.uid
64+
return yield if euid == uid
65+
66+
old_euid = euid
67+
process_reexchangeable = Process::UID.re_exchangeable?
68+
if process_reexchangeable
69+
Process::UID.re_exchange
70+
else
71+
Process::Sys.seteuid(uid)
72+
end
73+
74+
home = T.must(Etc.getpwuid(Process.uid)).dir
75+
return_value = with_env("HOME" => home, &block)
76+
77+
if process_reexchangeable
78+
Process::UID.re_exchange
79+
else
80+
Process::Sys.seteuid(old_euid)
81+
end
82+
83+
return_value
84+
end
85+
end
86+
end
87+
end
88+
89+
require "extend/os/bundle/bundle"

Library/Homebrew/bundle/adder.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# typed: true # rubocop:todo Sorbet/StrictSigil
22
# frozen_string_literal: true
33

4+
require "bundle/brewfile"
5+
require "bundle/dumper"
6+
47
module Homebrew
58
module Bundle
69
module Adder

Library/Homebrew/bundle/brew_checker.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# typed: true # rubocop:todo Sorbet/StrictSigil
22
# frozen_string_literal: true
33

4+
require "bundle/brew_installer"
5+
46
module Homebrew
57
module Bundle
68
module Checker

Library/Homebrew/bundle/brew_dumper.rb

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module BrewDumper
1111
module_function
1212

1313
def reset!
14+
require "bundle/brew_services"
15+
1416
Homebrew::Bundle::BrewServices.reset!
1517
@formulae = nil
1618
@formulae_by_full_name = nil
@@ -54,6 +56,8 @@ def formulae_by_name(name)
5456
end
5557

5658
def dump(describe: false, no_restart: false)
59+
require "bundle/brew_services"
60+
5761
requested_formula = formulae.select do |f|
5862
f[:installed_on_request?] || !f[:installed_as_dependency?]
5963
end

Library/Homebrew/bundle/brew_installer.rb

+10-4
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def start_service?
7777
end
7878

7979
def start_service_needed?
80+
require "bundle/brew_services"
8081
start_service? && !BrewServices.started?(@full_name)
8182
end
8283

@@ -96,6 +97,7 @@ def changed?
9697
end
9798

9899
def service_change_state!(verbose:)
100+
require "bundle/brew_services"
99101
if restart_service_needed?
100102
puts "Restarting #{@name} service." if verbose
101103
BrewServices.restart(@full_name, verbose:)
@@ -156,6 +158,7 @@ def self.formula_in_array?(formula, array)
156158
return true if array.include?(formula)
157159
return true if array.include?(formula.split("/").last)
158160

161+
require "bundle/brew_dumper"
159162
old_names = Homebrew::Bundle::BrewDumper.formula_oldnames
160163
old_name = old_names[formula]
161164
old_name ||= old_names[formula.split("/").last]
@@ -195,6 +198,7 @@ def self.pinned_formulae
195198
end
196199

197200
def self.formulae
201+
require "bundle/brew_dumper"
198202
Homebrew::Bundle::BrewDumper.formulae
199203
end
200204

@@ -225,6 +229,7 @@ def conflicts_with
225229
conflicts_with = Set.new
226230
conflicts_with += @conflicts_with_arg
227231

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

249-
if restart_service?
250-
puts "Stopping #{conflict} service (if it is running)." if verbose
251-
BrewServices.stop(conflict, verbose:)
252-
end
254+
next unless restart_service?
255+
256+
require "bundle/brew_services"
257+
puts "Stopping #{conflict} service (if it is running)." if verbose
258+
BrewServices.stop(conflict, verbose:)
253259
end
254260

255261
true

Library/Homebrew/bundle/brew_service_checker.rb

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def installed_and_up_to_date?(formula, no_upgrade: false)
2424
end
2525

2626
def entry_to_formula(entry)
27+
require "bundle/brew_installer"
2728
Homebrew::Bundle::BrewInstaller.new(entry.name, entry.options)
2829
end
2930

@@ -32,10 +33,12 @@ def formula_needs_to_start?(formula)
3233
end
3334

3435
def service_is_started?(service_name)
36+
require "bundle/brew_services"
3537
Homebrew::Bundle::BrewServices.started?(service_name)
3638
end
3739

3840
def lookup_old_name(service_name)
41+
require "bundle/brew_dumper"
3942
@old_names ||= Homebrew::Bundle::BrewDumper.formula_oldnames
4043
old_name = @old_names[service_name]
4144
old_name ||= @old_names[service_name.split("/").last]

Library/Homebrew/bundle/brewfile.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# typed: true # rubocop:todo Sorbet/StrictSigil
22
# frozen_string_literal: true
33

4+
require "bundle/dsl"
5+
46
module Homebrew
57
module Bundle
68
module Brewfile

Library/Homebrew/bundle/bundle.rb

-89
This file was deleted.

Library/Homebrew/bundle/cask_checker.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# typed: true # rubocop:todo Sorbet/StrictSigil
22
# frozen_string_literal: true
33

4+
require "bundle/cask_installer"
5+
46
module Homebrew
57
module Bundle
68
module Checker

Library/Homebrew/bundle/cask_installer.rb

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def upgrading?(no_upgrade, name, options)
1616
return true if outdated_casks.include?(name)
1717
return false unless options[:greedy]
1818

19+
require "bundle/cask_dumper"
1920
Homebrew::Bundle::CaskDumper.cask_is_outdated_using_greedy?(name)
2021
end
2122

@@ -97,10 +98,12 @@ def cask_upgradable?(cask)
9798
end
9899

99100
def installed_casks
101+
require "bundle/cask_dumper"
100102
@installed_casks ||= Homebrew::Bundle::CaskDumper.cask_names
101103
end
102104

103105
def outdated_casks
106+
require "bundle/cask_dumper"
104107
@outdated_casks ||= Homebrew::Bundle::CaskDumper.outdated_cask_names
105108
end
106109
end

0 commit comments

Comments
 (0)