Skip to content

Commit 33f83be

Browse files
committed
Tweak Gem vendoring.
If people have `HOMEBREW_RUBY_PATH` set then things explode in a rather confusing fashion. Instead, run `bundle` for them with the arguments that they'd want. Also, move `macho` requires into the module itself; it's a pain having to do everything for Bundler before requiring `pathname` which is a core Ruby class.
1 parent 3e4547f commit 33f83be

File tree

6 files changed

+25
-64
lines changed

6 files changed

+25
-64
lines changed

Library/Homebrew/brew.rb

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
RUBY_TWO = RUBY_VERSION.split(".").first.to_i >= 2
99
raise "Homebrew must be run under Ruby 2!" unless RUBY_TWO
1010

11-
homebrew_library_path = File.dirname(File.realpath(__FILE__))
12-
$:.unshift(homebrew_library_path)
13-
14-
require_relative "#{homebrew_library_path}/vendor/bundler/setup"
15-
1611
require "pathname"
17-
HOMEBREW_LIBRARY_PATH = Pathname.new(homebrew_library_path)
12+
HOMEBREW_LIBRARY_PATH = Pathname.new(__FILE__).realpath.parent
13+
$:.unshift(HOMEBREW_LIBRARY_PATH)
14+
15+
load_path_before_bundler = $:.dup
16+
require_relative "#{HOMEBREW_LIBRARY_PATH}/vendor/bundler/setup"
17+
ENV["HOMEBREW_GEMS_LOAD_PATH"] = ($: - load_path_before_bundler).join(":")
1818

1919
require "global"
2020
require "tap"
@@ -25,8 +25,6 @@
2525
exit 0
2626
end
2727

28-
HOMEBREW_GEM_HOME = HOMEBREW_LIBRARY_PATH/"vendor/#{RUBY_ENGINE}/#{RUBY_VERSION}"
29-
3028
def require?(path)
3129
require path
3230
rescue LoadError => e
@@ -60,18 +58,21 @@ def require?(path)
6058
path.append(Pathname.glob(Tap::TAP_DIRECTORY/"*/*/cmd"))
6159

6260
# Add RubyGems.
63-
ENV["GEM_HOME"] = ENV["GEM_PATH"] = HOMEBREW_GEM_HOME
61+
HOMEBREW_GEM_HOME = HOMEBREW_LIBRARY_PATH/"vendor/#{RUBY_ENGINE}/#{RUBY_VERSION}"
6462
path.append(HOMEBREW_GEM_HOME/"bin")
6563

66-
# Make RubyGems notice environment changes.
67-
Gem.clear_paths
68-
Gem::Specification.reset
69-
7064
# Add SCM wrappers.
7165
path.append(HOMEBREW_SHIMS_PATH/"scm")
7266

7367
ENV["PATH"] = path
7468

69+
# Setup RubyGems environment.
70+
ENV["GEM_HOME"] = ENV["GEM_PATH"] = HOMEBREW_GEM_HOME
71+
# Make RubyGems notice environment changes.
72+
Gem.clear_paths
73+
Gem::Specification.reset
74+
Homebrew.run_bundler_if_needed! unless HOMEBREW_GEM_HOME.exist?
75+
7576
if cmd
7677
internal_cmd = require? HOMEBREW_LIBRARY_PATH.join("cmd", cmd)
7778

Library/Homebrew/brew.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ then
6969
odie "Cowardly refusing to continue at this prefix: $HOMEBREW_PREFIX"
7070
fi
7171

72-
# Save value to use for installing gems
73-
export GEM_OLD_HOME="$GEM_HOME"
74-
export GEM_OLD_PATH="$GEM_PATH"
75-
7672
# Users may have these set, pointing the system Ruby
7773
# at non-system gem paths
7874
unset GEM_HOME

Library/Homebrew/config.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,8 @@
4646
HOMEBREW_LIBRARY_PATH = Pathname.new(__FILE__).realpath.parent
4747
end
4848

49+
# Load path to vendored gems used by Homebrew
50+
HOMEBREW_GEMS_LOAD_PATH = ENV["HOMEBREW_GEMS_LOAD_PATH"]
51+
4952
# Load path used by standalone scripts to access the Homebrew code base
50-
HOMEBREW_LOAD_PATH = HOMEBREW_LIBRARY_PATH
53+
HOMEBREW_LOAD_PATH = [HOMEBREW_LIBRARY_PATH, *HOMEBREW_GEMS_LOAD_PATH].join(":")

Library/Homebrew/os/mac/mach.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
require "macho"
21
require "os/mac/architecture_list"
32

43
module MachOShim
54
# @private
65
def macho
76
@macho ||= begin
7+
require "macho"
8+
89
MachO.open(to_s)
910
end
1011
end
1112

1213
# @private
1314
def mach_data
1415
@mach_data ||= begin
16+
require "macho"
17+
1518
machos = []
1619
mach_data = []
1720

Library/Homebrew/test/support/lib/config.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
end
1515

1616
# Paths pointing into the Homebrew code base that persist across test runs
17-
HOMEBREW_LIBRARY_PATH = Pathname.new(File.expand_path("../../../..", __FILE__))
18-
HOMEBREW_SHIMS_PATH = HOMEBREW_LIBRARY_PATH.parent+"Homebrew/shims"
19-
HOMEBREW_LOAD_PATH = [File.expand_path("..", __FILE__), HOMEBREW_LIBRARY_PATH].join(":")
17+
HOMEBREW_LIBRARY_PATH = Pathname.new(File.expand_path("../../../..", __FILE__))
18+
HOMEBREW_SHIMS_PATH = HOMEBREW_LIBRARY_PATH.parent+"Homebrew/shims"
19+
HOMEBREW_LOAD_PATH = [File.expand_path("..", __FILE__), HOMEBREW_LIBRARY_PATH, ENV["HOMEBREW_GEMS_LOAD_PATH"]].join(":")
2020

2121
# Paths redirected to a temporary directory and wiped at the end of the test run
2222
HOMEBREW_PREFIX = Pathname.new(TEST_TMPDIR).join("prefix")

Library/Homebrew/utils.rb

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -179,48 +179,6 @@ def system(cmd, *args)
179179
_system(cmd, *args)
180180
end
181181

182-
def install_gem_setup_path!(name, version = nil, executable = name)
183-
# Respect user's preferences for where gems should be installed.
184-
ENV["GEM_HOME"] = ENV["GEM_OLD_HOME"].to_s
185-
ENV["GEM_HOME"] = Gem.user_dir if ENV["GEM_HOME"].empty?
186-
ENV["GEM_PATH"] = ENV["GEM_OLD_PATH"] unless ENV["GEM_OLD_PATH"].to_s.empty?
187-
188-
# Make rubygems notice env changes.
189-
Gem.clear_paths
190-
Gem::Specification.reset
191-
192-
# Add Gem binary directory and (if missing) Ruby binary directory to PATH.
193-
path = PATH.new(ENV["PATH"])
194-
path.prepend(RUBY_BIN) if which("ruby") != RUBY_PATH
195-
path.prepend(Gem.bindir)
196-
ENV["PATH"] = path
197-
198-
if Gem::Specification.find_all_by_name(name, version).empty?
199-
ohai "Installing or updating '#{name}' gem"
200-
install_args = %W[--no-ri --no-rdoc #{name}]
201-
install_args << "--version" << version if version
202-
203-
# Do `gem install [...]` without having to spawn a separate process or
204-
# having to find the right `gem` binary for the running Ruby interpreter.
205-
require "rubygems/commands/install_command"
206-
install_cmd = Gem::Commands::InstallCommand.new
207-
install_cmd.handle_options(install_args)
208-
exit_code = 1 # Should not matter as `install_cmd.execute` always throws.
209-
begin
210-
install_cmd.execute
211-
rescue Gem::SystemExitException => e
212-
exit_code = e.exit_code
213-
end
214-
odie "Failed to install/update the '#{name}' gem." if exit_code.nonzero?
215-
end
216-
217-
return if which(executable)
218-
odie <<-EOS.undent
219-
The '#{name}' gem is installed but couldn't find '#{executable}' in the PATH:
220-
#{ENV["PATH"]}
221-
EOS
222-
end
223-
224182
def run_bundler_if_needed!
225183
return unless Pathname.glob("#{HOMEBREW_GEM_HOME}/bin/*").empty?
226184

0 commit comments

Comments
 (0)