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:
		
							parent
							
								
									97958410f4
								
							
						
					
					
						commit
						17f3ee1957
					
				@ -1,5 +1,6 @@
 | 
			
		||||
---
 | 
			
		||||
BUNDLE_BIN: "false"
 | 
			
		||||
BUNDLE_CLEAN: "true"
 | 
			
		||||
BUNDLE_DISABLE_SHARED_GEMS: "true"
 | 
			
		||||
BUNDLE_JOBS: "4"
 | 
			
		||||
BUNDLE_PATH: "vendor/bundle"
 | 
			
		||||
 | 
			
		||||
@ -199,8 +199,6 @@ module Homebrew
 | 
			
		||||
          FileUtils.touch PERIODIC_CLEAN_FILE
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        cleanup_bundler
 | 
			
		||||
 | 
			
		||||
        # Cleaning up Ruby needs to be done last to avoid requiring additional
 | 
			
		||||
        # 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
 | 
			
		||||
@ -351,21 +349,6 @@ module Homebrew
 | 
			
		||||
      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
 | 
			
		||||
      system_ruby_version =
 | 
			
		||||
        Utils.popen_read("/usr/bin/ruby", "-e", "puts RUBY_VERSION")
 | 
			
		||||
 | 
			
		||||
@ -33,6 +33,9 @@ module Homebrew
 | 
			
		||||
  def tests
 | 
			
		||||
    tests_args.parse
 | 
			
		||||
 | 
			
		||||
    Homebrew.install_bundler_gems!
 | 
			
		||||
    gem_user_dir = Gem.user_dir
 | 
			
		||||
 | 
			
		||||
    HOMEBREW_LIBRARY_PATH.cd do
 | 
			
		||||
      ENV.delete("HOMEBREW_COLOR")
 | 
			
		||||
      ENV.delete("HOMEBREW_NO_COLOR")
 | 
			
		||||
@ -69,8 +72,6 @@ module Homebrew
 | 
			
		||||
        ENV["GIT_#{role}_DATE"]  = "Sun Jan 22 19:59:13 2017 +0000"
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      Homebrew.install_bundler_gems!
 | 
			
		||||
 | 
			
		||||
      parallel = true
 | 
			
		||||
 | 
			
		||||
      files = if args.only
 | 
			
		||||
@ -123,6 +124,9 @@ module Homebrew
 | 
			
		||||
 | 
			
		||||
      puts "Randomized with seed #{seed}"
 | 
			
		||||
 | 
			
		||||
      # Let `bundle` in PATH find its gem.
 | 
			
		||||
      ENV["GEM_PATH"] = "#{ENV["GEM_PATH"]}:#{gem_user_dir}"
 | 
			
		||||
 | 
			
		||||
      if parallel
 | 
			
		||||
        system "bundle", "exec", "parallel_rspec", *parallel_args, "--", *bundle_args, "--", *files
 | 
			
		||||
      else
 | 
			
		||||
 | 
			
		||||
@ -42,7 +42,7 @@ describe Cask::Cmd::Style, :cask do
 | 
			
		||||
 | 
			
		||||
    context "when installation succeeds" do
 | 
			
		||||
      before do
 | 
			
		||||
        allow(Homebrew).to receive(:install_gem_setup_path!)
 | 
			
		||||
        allow(Homebrew).to receive(:install_bundler_gems!)
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it "exits successfully" do
 | 
			
		||||
@ -52,7 +52,7 @@ describe Cask::Cmd::Style, :cask do
 | 
			
		||||
 | 
			
		||||
    context "when installation fails" 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
 | 
			
		||||
 | 
			
		||||
      it "raises an error" do
 | 
			
		||||
 | 
			
		||||
@ -15,6 +15,10 @@ describe "brew style" do
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  before do
 | 
			
		||||
    allow(Homebrew).to receive(:install_bundler_gems!)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe "Homebrew::check_style_json" do
 | 
			
		||||
    let(:dir) { mktmpdir }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -72,11 +72,9 @@ RSpec.configure do |config|
 | 
			
		||||
 | 
			
		||||
  config.silence_filter_announcements = true if ENV["TEST_ENV_NUMBER"]
 | 
			
		||||
 | 
			
		||||
  # TODO: when https://github.com/rspec/rspec-expectations/pull/1056
 | 
			
		||||
  #       makes it into a stable release:
 | 
			
		||||
  # config.expect_with :rspec do |c|
 | 
			
		||||
  #   c.max_formatted_output_length = 200
 | 
			
		||||
  # end
 | 
			
		||||
  config.expect_with :rspec do |c|
 | 
			
		||||
    c.max_formatted_output_length = 200
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  # Never truncate output objects.
 | 
			
		||||
  RSpec::Support::ObjectFormatter.default_instance.max_formatted_output_length = nil
 | 
			
		||||
 | 
			
		||||
@ -8,12 +8,34 @@ module Homebrew
 | 
			
		||||
  module_function
 | 
			
		||||
 | 
			
		||||
  def ruby_bindir
 | 
			
		||||
    "#{RbConfig::CONFIG["prefix"]}/bin"
 | 
			
		||||
    "#{RbConfig::CONFIG["prefix"]}/bin".freeze
 | 
			
		||||
  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.
 | 
			
		||||
    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"]
 | 
			
		||||
 | 
			
		||||
    # Make RubyGems notice environment changes.
 | 
			
		||||
@ -21,41 +43,41 @@ module Homebrew
 | 
			
		||||
    Gem::Specification.reset
 | 
			
		||||
 | 
			
		||||
    # Add necessary Ruby and Gem binary directories to PATH.
 | 
			
		||||
    gem_bindir ||= Gem.bindir
 | 
			
		||||
    paths = ENV["PATH"].split(":")
 | 
			
		||||
    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(":")
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def install_gem!(name, version = nil)
 | 
			
		||||
    setup_gem_environment!
 | 
			
		||||
  def install_gem!(name, version = nil, setup_gem_environment: true)
 | 
			
		||||
    setup_gem_environment! if setup_gem_environment
 | 
			
		||||
    return unless Gem::Specification.find_all_by_name(name, version).empty?
 | 
			
		||||
 | 
			
		||||
    # 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 << "--version" << version if version
 | 
			
		||||
    return if system "#{ruby_bindir}/gem", "install", *install_args
 | 
			
		||||
 | 
			
		||||
    $stderr.puts "Error: failed to install the '#{name}' gem."
 | 
			
		||||
    exit 1
 | 
			
		||||
    odie_if_defined "failed to install the '#{name}' gem."
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def install_gem_setup_path!(name, executable: name)
 | 
			
		||||
    install_gem!(name)
 | 
			
		||||
  def install_gem_setup_path!(name, executable: name, setup_gem_environment: true)
 | 
			
		||||
    install_gem!(name, setup_gem_environment: setup_gem_environment)
 | 
			
		||||
    return if ENV["PATH"].split(":").any? do |path|
 | 
			
		||||
      File.executable?("#{path}/#{executable}")
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    $stderr.puts <<~EOS
 | 
			
		||||
      Error: the '#{name}' gem is installed but couldn't find '#{executable}' in the PATH:
 | 
			
		||||
    odie_if_defined <<~EOS
 | 
			
		||||
      the '#{name}' gem is installed but couldn't find '#{executable}' in the PATH:
 | 
			
		||||
      #{ENV["PATH"]}
 | 
			
		||||
    EOS
 | 
			
		||||
    exit 1
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  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
 | 
			
		||||
 | 
			
		||||
  def install_bundler_gems!
 | 
			
		||||
@ -63,12 +85,17 @@ module Homebrew
 | 
			
		||||
 | 
			
		||||
    ENV["BUNDLE_GEMFILE"] = "#{ENV["HOMEBREW_LIBRARY"]}/Homebrew/Gemfile"
 | 
			
		||||
    @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?
 | 
			
		||||
 | 
			
		||||
      # for some reason sometimes the exit code lies so check the output too.
 | 
			
		||||
      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
 | 
			
		||||
        true
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user