Improve Bundler cleanup

Rather than trying to be smart and doing this ourselves in `brew cleanup` let’s just installed Bundler somewhere it doesn’t try to clean itself up and use `bundle install --cleanup` when we need cleanup done.

Also, use `ohai` and `odie` when possible as they look nicer.
This commit is contained in:
Mike McQuaid 2019-02-26 22:13:00 +00:00
parent 97958410f4
commit 17f3ee1957
No known key found for this signature in database
GPG Key ID: 48A898132FD8EE70
8 changed files with 60 additions and 43 deletions

View File

@ -1,5 +1,6 @@
--- ---
BUNDLE_BIN: "false" BUNDLE_BIN: "false"
BUNDLE_CLEAN: "true"
BUNDLE_DISABLE_SHARED_GEMS: "true" BUNDLE_DISABLE_SHARED_GEMS: "true"
BUNDLE_JOBS: "4" BUNDLE_JOBS: "4"
BUNDLE_PATH: "vendor/bundle" BUNDLE_PATH: "vendor/bundle"

View File

@ -199,8 +199,6 @@ module Homebrew
FileUtils.touch PERIODIC_CLEAN_FILE FileUtils.touch PERIODIC_CLEAN_FILE
end end
cleanup_bundler
# Cleaning up Ruby needs to be done last to avoid requiring additional # Cleaning up Ruby needs to be done last to avoid requiring additional
# files afterwards. Additionally, don't allow it on periodic cleans to # files afterwards. Additionally, don't allow it on periodic cleans to
# avoid having to try to do a `brew install` when we've just deleted # avoid having to try to do a `brew install` when we've just deleted
@ -351,21 +349,6 @@ module Homebrew
end end
end end
def cleanup_bundler
HOMEBREW_LIBRARY_PATH.cd do
Homebrew.setup_gem_environment!
bundle = "#{Gem.bindir}/bundle"
return unless File.executable?(bundle)
return if Gem::Specification.find_all_by_name("bundler").empty?
if dry_run?
system bundle, "clean", "--dry-run"
else
system bundle, "clean"
end
end
end
def cleanup_portable_ruby def cleanup_portable_ruby
system_ruby_version = system_ruby_version =
Utils.popen_read("/usr/bin/ruby", "-e", "puts RUBY_VERSION") Utils.popen_read("/usr/bin/ruby", "-e", "puts RUBY_VERSION")

View File

@ -33,6 +33,9 @@ module Homebrew
def tests def tests
tests_args.parse tests_args.parse
Homebrew.install_bundler_gems!
gem_user_dir = Gem.user_dir
HOMEBREW_LIBRARY_PATH.cd do HOMEBREW_LIBRARY_PATH.cd do
ENV.delete("HOMEBREW_COLOR") ENV.delete("HOMEBREW_COLOR")
ENV.delete("HOMEBREW_NO_COLOR") ENV.delete("HOMEBREW_NO_COLOR")
@ -69,8 +72,6 @@ module Homebrew
ENV["GIT_#{role}_DATE"] = "Sun Jan 22 19:59:13 2017 +0000" ENV["GIT_#{role}_DATE"] = "Sun Jan 22 19:59:13 2017 +0000"
end end
Homebrew.install_bundler_gems!
parallel = true parallel = true
files = if args.only files = if args.only
@ -123,6 +124,9 @@ module Homebrew
puts "Randomized with seed #{seed}" puts "Randomized with seed #{seed}"
# Let `bundle` in PATH find its gem.
ENV["GEM_PATH"] = "#{ENV["GEM_PATH"]}:#{gem_user_dir}"
if parallel if parallel
system "bundle", "exec", "parallel_rspec", *parallel_args, "--", *bundle_args, "--", *files system "bundle", "exec", "parallel_rspec", *parallel_args, "--", *bundle_args, "--", *files
else else

View File

@ -42,7 +42,7 @@ describe Cask::Cmd::Style, :cask do
context "when installation succeeds" do context "when installation succeeds" do
before do before do
allow(Homebrew).to receive(:install_gem_setup_path!) allow(Homebrew).to receive(:install_bundler_gems!)
end end
it "exits successfully" do it "exits successfully" do
@ -52,7 +52,7 @@ describe Cask::Cmd::Style, :cask do
context "when installation fails" do context "when installation fails" do
before do before do
allow(Homebrew).to receive(:install_gem_setup_path!).and_raise(SystemExit) allow(Homebrew).to receive(:install_bundler_gems!).and_raise(SystemExit)
end end
it "raises an error" do it "raises an error" do

View File

@ -15,6 +15,10 @@ describe "brew style" do
end end
end end
before do
allow(Homebrew).to receive(:install_bundler_gems!)
end
describe "Homebrew::check_style_json" do describe "Homebrew::check_style_json" do
let(:dir) { mktmpdir } let(:dir) { mktmpdir }

View File

@ -72,11 +72,9 @@ RSpec.configure do |config|
config.silence_filter_announcements = true if ENV["TEST_ENV_NUMBER"] config.silence_filter_announcements = true if ENV["TEST_ENV_NUMBER"]
# TODO: when https://github.com/rspec/rspec-expectations/pull/1056 config.expect_with :rspec do |c|
# makes it into a stable release: c.max_formatted_output_length = 200
# config.expect_with :rspec do |c| end
# c.max_formatted_output_length = 200
# end
# Never truncate output objects. # Never truncate output objects.
RSpec::Support::ObjectFormatter.default_instance.max_formatted_output_length = nil RSpec::Support::ObjectFormatter.default_instance.max_formatted_output_length = nil

View File

@ -8,12 +8,34 @@ module Homebrew
module_function module_function
def ruby_bindir def ruby_bindir
"#{RbConfig::CONFIG["prefix"]}/bin" "#{RbConfig::CONFIG["prefix"]}/bin".freeze
end end
def setup_gem_environment! def gem_user_bindir
"#{Gem.user_dir}/bin".freeze
end
def ohai_if_defined(message)
if defined?(ohai)
ohai message
else
puts "==> #{message}"
end
end
def odie_if_defined(message)
if defined?(odie)
odie message
else
$stderr.puts "Error: #{message}"
exit 1
end
end
def setup_gem_environment!(gem_home: nil, gem_bindir: nil)
# Match where our bundler gems are. # Match where our bundler gems are.
ENV["GEM_HOME"] = "#{ENV["HOMEBREW_LIBRARY"]}/Homebrew/vendor/bundle/ruby/#{RbConfig::CONFIG["ruby_version"]}" gem_home ||= "#{ENV["HOMEBREW_LIBRARY"]}/Homebrew/vendor/bundle/ruby/#{RbConfig::CONFIG["ruby_version"]}"
ENV["GEM_HOME"] = gem_home
ENV["GEM_PATH"] = ENV["GEM_HOME"] ENV["GEM_PATH"] = ENV["GEM_HOME"]
# Make RubyGems notice environment changes. # Make RubyGems notice environment changes.
@ -21,41 +43,41 @@ module Homebrew
Gem::Specification.reset Gem::Specification.reset
# Add necessary Ruby and Gem binary directories to PATH. # Add necessary Ruby and Gem binary directories to PATH.
gem_bindir ||= Gem.bindir
paths = ENV["PATH"].split(":") paths = ENV["PATH"].split(":")
paths.unshift(ruby_bindir) unless paths.include?(ruby_bindir) paths.unshift(ruby_bindir) unless paths.include?(ruby_bindir)
paths.unshift(Gem.bindir) unless paths.include?(Gem.bindir) paths.unshift(gem_bindir) unless paths.include?(gem_bindir)
ENV["PATH"] = paths.compact.join(":") ENV["PATH"] = paths.compact.join(":")
end end
def install_gem!(name, version = nil) def install_gem!(name, version = nil, setup_gem_environment: true)
setup_gem_environment! setup_gem_environment! if setup_gem_environment
return unless Gem::Specification.find_all_by_name(name, version).empty? return unless Gem::Specification.find_all_by_name(name, version).empty?
# Shell out to `gem` to avoid RubyGems requires e.g. loading JSON. # Shell out to `gem` to avoid RubyGems requires e.g. loading JSON.
puts "==> Installing '#{name}' gem" ohai_if_defined "Installing '#{name}' gem"
install_args = %W[--no-document #{name}] install_args = %W[--no-document #{name}]
install_args << "--version" << version if version install_args << "--version" << version if version
return if system "#{ruby_bindir}/gem", "install", *install_args return if system "#{ruby_bindir}/gem", "install", *install_args
$stderr.puts "Error: failed to install the '#{name}' gem." odie_if_defined "failed to install the '#{name}' gem."
exit 1
end end
def install_gem_setup_path!(name, executable: name) def install_gem_setup_path!(name, executable: name, setup_gem_environment: true)
install_gem!(name) install_gem!(name, setup_gem_environment: setup_gem_environment)
return if ENV["PATH"].split(":").any? do |path| return if ENV["PATH"].split(":").any? do |path|
File.executable?("#{path}/#{executable}") File.executable?("#{path}/#{executable}")
end end
$stderr.puts <<~EOS odie_if_defined <<~EOS
Error: the '#{name}' gem is installed but couldn't find '#{executable}' in the PATH: the '#{name}' gem is installed but couldn't find '#{executable}' in the PATH:
#{ENV["PATH"]} #{ENV["PATH"]}
EOS EOS
exit 1
end end
def install_bundler! def install_bundler!
install_gem_setup_path! "bundler", executable: "bundle" setup_gem_environment!(gem_home: Gem.user_dir, gem_bindir: gem_user_bindir)
install_gem_setup_path!("bundler", executable: "bundle", setup_gem_environment: false)
end end
def install_bundler_gems! def install_bundler_gems!
@ -63,12 +85,17 @@ module Homebrew
ENV["BUNDLE_GEMFILE"] = "#{ENV["HOMEBREW_LIBRARY"]}/Homebrew/Gemfile" ENV["BUNDLE_GEMFILE"] = "#{ENV["HOMEBREW_LIBRARY"]}/Homebrew/Gemfile"
@bundle_installed ||= begin @bundle_installed ||= begin
bundle_check_output = `#{Gem.bindir}/bundle check 2>&1` bundle = "#{gem_user_bindir}/bundle".freeze
bundle_check_output = `#{bundle} check 2>&1`
bundle_check_failed = !$CHILD_STATUS.exitstatus.zero? bundle_check_failed = !$CHILD_STATUS.exitstatus.zero?
# for some reason sometimes the exit code lies so check the output too. # for some reason sometimes the exit code lies so check the output too.
if bundle_check_failed || bundle_check_output.include?("Install missing gems") if bundle_check_failed || bundle_check_output.include?("Install missing gems")
system "#{Gem.bindir}/bundle", "install" unless system bundle, "install"
odie_if_defined <<~EOS
failed to run `#{bundle} install`!
EOS
end
else else
true true
end end