2019-11-11 03:57:45 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "pathname"
|
|
|
|
require_relative "helpers"
|
|
|
|
require_relative "path"
|
|
|
|
|
|
|
|
class RubygemsVersionManager
|
|
|
|
include Spec::Helpers
|
|
|
|
include Spec::Path
|
|
|
|
|
2019-12-14 05:49:16 -05:00
|
|
|
def initialize(source)
|
|
|
|
@source = source
|
2019-11-11 03:57:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def switch
|
|
|
|
return if use_system?
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
assert_system_features_not_loaded!
|
|
|
|
|
2019-11-11 03:57:45 -05:00
|
|
|
switch_local_copy_if_needed
|
|
|
|
|
2019-12-14 05:49:16 -05:00
|
|
|
reexec_if_needed
|
2019-11-11 03:57:45 -05:00
|
|
|
end
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
def assert_system_features_not_loaded!
|
|
|
|
at_exit do
|
2020-06-21 08:44:19 -04:00
|
|
|
errors = if $?.nil?
|
|
|
|
""
|
|
|
|
else
|
|
|
|
all_commands_output
|
|
|
|
end
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
rubylibdir = RbConfig::CONFIG["rubylibdir"]
|
|
|
|
|
|
|
|
rubygems_path = rubylibdir + "/rubygems"
|
|
|
|
rubygems_default_path = rubygems_path + "/defaults"
|
|
|
|
|
|
|
|
bundler_path = rubylibdir + "/bundler"
|
|
|
|
bundler_exemptions = Gem.rubygems_version < Gem::Version.new("3.2.0") ? [bundler_path + "/errors.rb"] : []
|
|
|
|
|
|
|
|
bad_loaded_features = $LOADED_FEATURES.select do |loaded_feature|
|
|
|
|
(loaded_feature.start_with?(rubygems_path) && !loaded_feature.start_with?(rubygems_default_path)) ||
|
|
|
|
(loaded_feature.start_with?(bundler_path) && !bundler_exemptions.any? {|bundler_exemption| loaded_feature.start_with?(bundler_exemption) })
|
|
|
|
end
|
|
|
|
|
|
|
|
if bad_loaded_features.any?
|
2020-06-21 08:44:19 -04:00
|
|
|
errors += "the following features were incorrectly loaded:\n#{bad_loaded_features.join("\n")}"
|
2020-05-08 01:19:04 -04:00
|
|
|
end
|
2020-06-21 08:44:19 -04:00
|
|
|
|
|
|
|
raise errors unless errors.empty?
|
2020-05-08 01:19:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-15 00:20:25 -04:00
|
|
|
private
|
2019-11-11 03:57:45 -05:00
|
|
|
|
|
|
|
def use_system?
|
2019-12-14 05:49:16 -05:00
|
|
|
@source.nil?
|
2019-11-11 03:57:45 -05:00
|
|
|
end
|
|
|
|
|
2019-12-14 05:49:16 -05:00
|
|
|
def reexec_if_needed
|
2019-11-11 03:57:45 -05:00
|
|
|
return unless rubygems_unrequire_needed?
|
|
|
|
|
|
|
|
require "rbconfig"
|
|
|
|
|
2020-06-24 13:59:50 -04:00
|
|
|
cmd = [RbConfig.ruby, $0, *ARGV].compact
|
2019-12-14 05:49:16 -05:00
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
ENV["RUBYOPT"] = opt_add("-I#{local_copy_path.join("lib")}", opt_remove("--disable-gems", ENV["RUBYOPT"]))
|
2019-11-11 03:57:45 -05:00
|
|
|
|
|
|
|
exec(ENV, *cmd)
|
|
|
|
end
|
|
|
|
|
|
|
|
def switch_local_copy_if_needed
|
|
|
|
return unless local_copy_switch_needed?
|
|
|
|
|
2020-06-21 08:56:33 -04:00
|
|
|
sys_exec("git checkout #{target_tag}", :dir => local_copy_path)
|
2019-11-11 03:57:45 -05:00
|
|
|
|
2019-12-14 05:49:16 -05:00
|
|
|
ENV["RGV"] = local_copy_path.to_s
|
2019-11-11 03:57:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def rubygems_unrequire_needed?
|
2021-02-01 10:17:16 -05:00
|
|
|
require "rubygems"
|
2019-12-14 05:49:16 -05:00
|
|
|
!$LOADED_FEATURES.include?(local_copy_path.join("lib/rubygems.rb").to_s)
|
2019-11-11 03:57:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def local_copy_switch_needed?
|
2019-12-14 05:49:16 -05:00
|
|
|
!source_is_path? && target_tag != local_copy_tag
|
2019-11-11 03:57:45 -05:00
|
|
|
end
|
|
|
|
|
2019-12-14 05:49:16 -05:00
|
|
|
def target_tag
|
|
|
|
@target_tag ||= resolve_target_tag
|
2019-11-11 03:57:45 -05:00
|
|
|
end
|
|
|
|
|
2019-12-14 05:49:16 -05:00
|
|
|
def local_copy_tag
|
2020-06-03 14:46:35 -04:00
|
|
|
sys_exec("git rev-parse --abbrev-ref HEAD", :dir => local_copy_path)
|
2019-11-11 03:57:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def local_copy_path
|
|
|
|
@local_copy_path ||= resolve_local_copy_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def resolve_local_copy_path
|
2019-12-14 05:49:16 -05:00
|
|
|
return expanded_source if source_is_path?
|
2019-11-11 03:57:45 -05:00
|
|
|
|
2020-05-15 08:31:12 -04:00
|
|
|
rubygems_path = source_root.join("tmp/rubygems")
|
2019-11-11 03:57:45 -05:00
|
|
|
|
|
|
|
unless rubygems_path.directory?
|
2020-06-21 18:06:16 -04:00
|
|
|
sys_exec("git clone .. #{rubygems_path}", :dir => source_root)
|
2019-11-11 03:57:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
rubygems_path
|
|
|
|
end
|
|
|
|
|
2019-12-14 05:49:16 -05:00
|
|
|
def source_is_path?
|
|
|
|
expanded_source.directory?
|
2019-11-11 03:57:45 -05:00
|
|
|
end
|
|
|
|
|
2019-12-14 05:49:16 -05:00
|
|
|
def expanded_source
|
2020-05-15 08:31:12 -04:00
|
|
|
@expanded_source ||= Pathname.new(@source).expand_path(source_root)
|
2019-11-11 03:57:45 -05:00
|
|
|
end
|
|
|
|
|
2019-12-14 05:49:16 -05:00
|
|
|
def resolve_target_tag
|
|
|
|
return "v#{@source}" if @source.match(/^\d/)
|
2019-11-11 03:57:45 -05:00
|
|
|
|
2019-12-14 05:49:16 -05:00
|
|
|
@source
|
2019-11-11 03:57:45 -05:00
|
|
|
end
|
|
|
|
end
|