cleanup: fix and reuse portable ruby logic.

Align the logic in `cleanup_portable_ruby` with that in `ruby.sh`.

Co-authored-by: Maxim Belkin <maxim.belkin@gmail.com>
Co-authored-by: Mike McQuaid <mike@mikemcquaid.com>
This commit is contained in:
Maxim Belkin 2020-09-03 09:43:41 +01:00 committed by Mike McQuaid
parent 9b3bcfa70f
commit a6d29894d9
8 changed files with 70 additions and 27 deletions

View File

@ -12,9 +12,15 @@ raise "HOMEBREW_BREW_FILE was not exported! Please call bin/brew directly!" unle
std_trap = trap("INT") { exit! 130 } # no backtrace thanks
# check ruby version before requiring any modules.
unless ENV["HOMEBREW_REQUIRED_RUBY_VERSION"]
raise "HOMEBREW_REQUIRED_RUBY_VERSION was not exported! Please call bin/brew directly!"
end
REQUIRED_RUBY_X, REQUIRED_RUBY_Y, = ENV["HOMEBREW_REQUIRED_RUBY_VERSION"].split(".").map(&:to_i)
RUBY_X, RUBY_Y, = RUBY_VERSION.split(".").map(&:to_i)
if RUBY_X < 2 || (RUBY_X == 2 && RUBY_Y < 6)
raise "Homebrew must be run under Ruby 2.6! You're running #{RUBY_VERSION}."
if RUBY_X < REQUIRED_RUBY_X || (RUBY_X == REQUIRED_RUBY_X && RUBY_Y < REQUIRED_RUBY_Y)
raise "Homebrew must be run under Ruby #{REQUIRED_RUBY_X}.#{REQUIRED_RUBY_Y}! " \
"You're running #{RUBY_VERSION}."
end
# Also define here so we can rescue regardless of location.

View File

@ -348,24 +348,26 @@ module Homebrew
end
def cleanup_portable_ruby
system_ruby_version =
Utils.popen_read("/usr/bin/ruby", "-e", "puts RUBY_VERSION")
.chomp
use_system_ruby = (
Gem::Version.new(system_ruby_version) >= Gem::Version.new(RUBY_VERSION)
) && !Homebrew::EnvConfig.force_vendor_ruby?
vendor_path = HOMEBREW_LIBRARY/"Homebrew/vendor"
portable_ruby_version_file = vendor_path/"portable-ruby-version"
portable_ruby_version = if portable_ruby_version_file.exist?
portable_ruby_version_file.read
.chomp
rubies = [which("ruby"), which("ruby", ENV["HOMEBREW_PATH"])].compact
system_ruby = Pathname.new("/usr/bin/ruby")
rubies << system_ruby if system_ruby.exist?
use_system_ruby = if Homebrew::EnvConfig.force_vendor_ruby?
false
else
check_ruby_version = HOMEBREW_LIBRARY_PATH/"utils/ruby_check_version_script.rb"
rubies.uniq.any? do |ruby|
system ruby, "--enable-frozen-string-literal", "--disable=gems,did_you_mean,rubyopt",
check_ruby_version, HOMEBREW_REQUIRED_RUBY_VERSION
end
end
portable_ruby_path = vendor_path/"portable-ruby"
portable_ruby_glob = "#{portable_ruby_path}/*.*"
vendor_dir = HOMEBREW_LIBRARY/"Homebrew/vendor"
portable_ruby_latest_version = (vendor_dir/"portable-ruby-version").read.chomp
portable_rubies_to_remove = []
Pathname.glob(portable_ruby_glob).each do |path|
next if !use_system_ruby && portable_ruby_version == path.basename.to_s
Pathname.glob(vendor_dir/"portable-ruby/*.*").select(&:directory?).each do |path|
next if !use_system_ruby && portable_ruby_latest_version == path.basename.to_s
portable_rubies_to_remove << path
puts "Would remove: #{path} (#{path.abv})" if dry_run?
@ -373,7 +375,7 @@ module Homebrew
return if portable_rubies_to_remove.empty?
bundler_path = vendor_path/"bundle/ruby"
bundler_path = vendor_dir/"bundle/ruby"
if dry_run?
puts Utils.popen_read("git", "-C", HOMEBREW_REPOSITORY, "clean", "-nx", bundler_path).chomp
else

View File

@ -178,13 +178,12 @@ module Homebrew
end
def check_ruby_version
ruby_version = "2.6.3"
return if RUBY_VERSION == ruby_version
return if RUBY_VERSION == HOMEBREW_REQUIRED_RUBY_VERSION
return if Homebrew::EnvConfig.developer? && OS::Mac.prerelease?
<<~EOS
Ruby version #{RUBY_VERSION} is unsupported on #{MacOS.version}. Homebrew
is developed and tested on Ruby #{ruby_version}, and may not work correctly
is developed and tested on Ruby #{HOMEBREW_REQUIRED_RUBY_VERSION}, and may not work correctly
on other Rubies. Patches are accepted as long as they don't cause breakage
on supported Rubies.
EOS

View File

@ -37,6 +37,7 @@ HOMEBREW_CORE_DEFAULT_GIT_REMOTE = ENV["HOMEBREW_CORE_DEFAULT_GIT_REMOTE"]
HOMEBREW_DEFAULT_CACHE = ENV["HOMEBREW_DEFAULT_CACHE"]
HOMEBREW_DEFAULT_LOGS = ENV["HOMEBREW_DEFAULT_LOGS"]
HOMEBREW_DEFAULT_TEMP = ENV["HOMEBREW_DEFAULT_TEMP"]
HOMEBREW_REQUIRED_RUBY_VERSION = ENV["HOMEBREW_REQUIRED_RUBY_VERSION"]
require "env_config"
require "config"

View File

@ -36,11 +36,14 @@ describe Homebrew::Cleanup do
around do |example|
FileUtils.touch ds_store
FileUtils.touch lock_file
FileUtils.mkdir_p HOMEBREW_LIBRARY/"Homebrew/vendor"
FileUtils.touch HOMEBREW_LIBRARY/"Homebrew/vendor/portable-ruby-version"
example.run
ensure
FileUtils.rm_f ds_store
FileUtils.rm_f lock_file
FileUtils.rm_rf HOMEBREW_LIBRARY/"Homebrew"
end
describe "::cleanup" do

View File

@ -7,6 +7,15 @@ describe "Homebrew.cleanup_args" do
end
describe "brew cleanup", :integration_test do
before do
FileUtils.mkdir_p HOMEBREW_LIBRARY/"Homebrew/vendor/"
FileUtils.touch HOMEBREW_LIBRARY/"Homebrew/vendor/portable-ruby-version"
end
after do
FileUtils.rm_rf HOMEBREW_LIBRARY/"Homebrew"
end
describe "--prune=all" do
it "removes all files in Homebrew's cache" do
(HOMEBREW_CACHE/"test").write "test"

View File

@ -1,12 +1,14 @@
export HOMEBREW_REQUIRED_RUBY_VERSION=2.6.3
test_ruby () {
if [[ ! -x $1 ]]
then
return 1
fi
"$1" --enable-frozen-string-literal --disable=gems,did_you_mean,rubyopt -rrubygems -e \
"abort if Gem::Version.new(RUBY_VERSION.to_s.dup).to_s.split('.').first(2) != \
Gem::Version.new('$required_ruby_version').to_s.split('.').first(2)" 2>/dev/null
"$1" --enable-frozen-string-literal --disable=gems,did_you_mean,rubyopt \
"$HOMEBREW_LIBRARY/Homebrew/utils/ruby_check_version_script.rb" \
"$HOMEBREW_REQUIRED_RUBY_VERSION" 2>/dev/null
}
find_ruby() {
@ -47,7 +49,6 @@ setup-ruby-path() {
local vendor_ruby_current_version
# When bumping check if HOMEBREW_MACOS_SYSTEM_RUBY_NEW_ENOUGH (in brew.sh)
# also needs to be changed.
local required_ruby_version="2.6"
local ruby_exec
local upgrade_fail
local install_fail
@ -59,12 +60,12 @@ setup-ruby-path() {
else
local advice="
If there's no Homebrew Portable Ruby available for your processor:
- install Ruby $required_ruby_version with your system package manager (or rbenv/ruby-build)
- install Ruby $HOMEBREW_REQUIRED_RUBY_VERSION with your system package manager (or rbenv/ruby-build)
- make it first in your PATH
- try again
"
upgrade_fail="Failed to upgrade Homebrew Portable Ruby!$advice"
install_fail="Failed to install Homebrew Portable Ruby and cannot find another Ruby $required_ruby_version!$advice"
install_fail="Failed to install Homebrew Portable Ruby and cannot find another Ruby $HOMEBREW_REQUIRED_RUBY_VERSION!$advice"
fi
vendor_dir="$HOMEBREW_LIBRARY/Homebrew/vendor"

View File

@ -0,0 +1,22 @@
#!/usr/bin/env ruby --enable-frozen-string-literal --disable=gems,did_you_mean,rubyopt
# frozen_string_literal: true
HOMEBREW_REQUIRED_RUBY_VERSION = ARGV.first.freeze
raise "No Ruby version passed!" if HOMEBREW_REQUIRED_RUBY_VERSION.to_s.empty?
require "rubygems"
ruby_version = Gem::Version.new(RUBY_VERSION)
# This will only happen if the Ruby is too old anyway.
abort unless ruby_version.respond_to?(:canonical_segments)
homebrew_required_ruby_version = Gem::Version.new(HOMEBREW_REQUIRED_RUBY_VERSION)
ruby_version_major, ruby_version_minor, = ruby_version.canonical_segments
homebrew_required_ruby_version_major, homebrew_required_ruby_version_minor, =
homebrew_required_ruby_version.canonical_segments
if ruby_version_major != homebrew_required_ruby_version_major ||
ruby_version_minor != homebrew_required_ruby_version_minor
abort
end