Warn default gems which will be gemified in future

This commit is contained in:
Nobuyoshi Nakada 2023-07-11 18:08:16 +09:00 коммит произвёл Hiroshi SHIBATA
Родитель b9c2f4a424
Коммит 989f3add62
3 изменённых файлов: 85 добавлений и 2 удалений

Просмотреть файл

@ -4,6 +4,8 @@ rescue LoadError => e
raise unless e.path == 'rubygems'
warn "`RubyGems' were not loaded."
else
require 'bundled_gems'
end if defined?(Gem)
begin

Просмотреть файл

@ -1,5 +1,5 @@
module Gem
BUNDLED_GEMS = {
module Gem::BUNDLED_GEMS
SINCE = {
"rexml" => "3.0.0",
"rss" => "3.0.0",
"webrick" => "3.0.0",
@ -20,4 +20,84 @@ module Gem
"csv" => "3.4.0",
"base64" => "3.4.0"
}.freeze
EXACT = {
"abbrev"=>true,
"base64"=>true,
"csv"=>true,
"drb"=>true,
"getoptlong"=>true,
"mutex_m"=>true,
"nkf"=>true, "kconv"=>"nkf",
"observer"=>true,
"resolv-replace"=>true,
"rinda"=>true,
"syslog"=>true,
}.freeze
PREFIXED = {
"csv" => true,
"drb" => true,
"rinda" => true,
"syslog" => true,
}.freeze
WARNED = {} # unfrozen
def self.[](name)
SINCE[name]
end
def self.keys
SINCE.keys
end
conf = ::RbConfig::CONFIG
LIBDIR = (conf["rubylibdir"] + "/").freeze
ARCHDIR = (conf["rubyarchdir"] + "/").freeze
DLEXT = /\.#{Regexp.union([conf["DLEXT"], "so"].uniq)}\z/
def self.find_gem(path)
if !path
return
elsif path.start_with?(ARCHDIR)
n = path.delete_prefix(ARCHDIR).sub(DLEXT, "")
elsif path.start_with?(LIBDIR)
n = path.delete_prefix(LIBDIR).chomp(".rb")
else
return
end
EXACT[n] or PREFIXED[n[%r[\A[^/]+(?=/)]]]
end
def self.warning?(name)
_t, path = $:.resolve_feature_path(name)
return unless gem = find_gem(path)
caller, = caller_locations(3, 1)
return if find_gem(caller&.absolute_path)
return if WARNED[name]
WARNED[name] = true
if gem == true
gem = name
elsif gem
return if WARNED[gem]
WARNED[gem] = true
"#{name} is found in #{gem}"
else
return
end + " which is not part of the default gems since Ruby #{SINCE[gem]}"
end
bundled_gems = self
define_method(:find_unresolved_default_spec) do |name|
if msg = bundled_gems.warning?(name)
warn msg, uplevel: 1
end
super(name)
end
freeze
end
Gem.singleton_class.prepend Gem::BUNDLED_GEMS

Просмотреть файл

@ -38,6 +38,7 @@ module Kernel
RUBYGEMS_ACTIVATION_MONITOR.synchronize do
path = path.to_path if path.respond_to? :to_path
path = String.try_convert(path) || path
if spec = Gem.find_unresolved_default_spec(path)
# Ensure -I beats a default gem