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 std_trap = trap("INT") { exit! 130 } # no backtrace thanks
# check ruby version before requiring any modules. # 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) RUBY_X, RUBY_Y, = RUBY_VERSION.split(".").map(&:to_i)
if RUBY_X < 2 || (RUBY_X == 2 && RUBY_Y < 6) if RUBY_X < REQUIRED_RUBY_X || (RUBY_X == REQUIRED_RUBY_X && RUBY_Y < REQUIRED_RUBY_Y)
raise "Homebrew must be run under Ruby 2.6! You're running #{RUBY_VERSION}." raise "Homebrew must be run under Ruby #{REQUIRED_RUBY_X}.#{REQUIRED_RUBY_Y}! " \
"You're running #{RUBY_VERSION}."
end end
# Also define here so we can rescue regardless of location. # Also define here so we can rescue regardless of location.

View File

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

View File

@ -178,13 +178,12 @@ module Homebrew
end end
def check_ruby_version def check_ruby_version
ruby_version = "2.6.3" return if RUBY_VERSION == HOMEBREW_REQUIRED_RUBY_VERSION
return if RUBY_VERSION == ruby_version
return if Homebrew::EnvConfig.developer? && OS::Mac.prerelease? return if Homebrew::EnvConfig.developer? && OS::Mac.prerelease?
<<~EOS <<~EOS
Ruby version #{RUBY_VERSION} is unsupported on #{MacOS.version}. Homebrew 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 other Rubies. Patches are accepted as long as they don't cause breakage
on supported Rubies. on supported Rubies.
EOS 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_CACHE = ENV["HOMEBREW_DEFAULT_CACHE"]
HOMEBREW_DEFAULT_LOGS = ENV["HOMEBREW_DEFAULT_LOGS"] HOMEBREW_DEFAULT_LOGS = ENV["HOMEBREW_DEFAULT_LOGS"]
HOMEBREW_DEFAULT_TEMP = ENV["HOMEBREW_DEFAULT_TEMP"] HOMEBREW_DEFAULT_TEMP = ENV["HOMEBREW_DEFAULT_TEMP"]
HOMEBREW_REQUIRED_RUBY_VERSION = ENV["HOMEBREW_REQUIRED_RUBY_VERSION"]
require "env_config" require "env_config"
require "config" require "config"

View File

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

View File

@ -7,6 +7,15 @@ describe "Homebrew.cleanup_args" do
end end
describe "brew cleanup", :integration_test do 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 describe "--prune=all" do
it "removes all files in Homebrew's cache" do it "removes all files in Homebrew's cache" do
(HOMEBREW_CACHE/"test").write "test" (HOMEBREW_CACHE/"test").write "test"

View File

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