[rubygems/rubygems] Introduce self.load_with_rubygems_config_hash

https://github.com/rubygems/rubygems/commit/9175b8cf2a
This commit is contained in:
Hiroshi SHIBATA 2023-04-18 09:57:56 +09:00 коммит произвёл git
Родитель 644d7df021
Коммит ef54a9aeb6
3 изменённых файлов: 41 добавлений и 45 удалений

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

@ -342,21 +342,12 @@ if you believe they were disclosed to a third party.
end
def load_file(filename)
require "bundler/yaml_serializer"
yaml_errors = [ArgumentError]
return {} unless filename && !filename.empty? && File.exist?(filename)
begin
content = Bundler::YAMLSerializer.load(File.read(filename))
if content.is_a? Hash
content = self.class.convert_rubygems_config_hash(content)
else
warn "Failed to load #{filename} because it doesn't contain valid YAML hash"
return {}
end
return content
return self.class.load_with_rubygems_config_hash(File.read(filename))
rescue *yaml_errors => e
warn "Failed to load #{filename}, #{e}"
rescue Errno::EACCES
@ -530,42 +521,51 @@ if you believe they were disclosed to a third party.
Bundler::YAMLSerializer.dump(content)
end
def self.convert_rubygems_config_hash(content)
content.transform_keys! do |k|
if k.match?(/\A:(.*)\Z/)
k[1..-1].to_sym
elsif k.include?("__")
if k.is_a?(Symbol)
k.to_s.gsub(/__/,".").gsub(%r{/\Z}, "").to_sym
else
k.dup.gsub(/__/,".").gsub(%r{/\Z}, "")
end
else
k
end
end
def self.load_with_rubygems_config_hash(hash)
require "bundler/yaml_serializer"
content.transform_values! do |v|
if v.is_a?(String)
if v.match?(/\A:(.*)\Z/)
v[1..-1].to_sym
elsif v.match?(/\A[+-]?\d+\Z/)
v.to_i
elsif v.match?(/\Atrue|false\Z/)
v == "true"
elsif v.empty?
content = Bundler::YAMLSerializer.load(hash)
if content.is_a? Hash
content.transform_keys! do |k|
if k.match?(/\A:(.*)\Z/)
k[1..-1].to_sym
elsif k.include?("__")
if k.is_a?(Symbol)
k.to_s.gsub(/__/,".").gsub(%r{/\Z}, "").to_sym
else
k.dup.gsub(/__/,".").gsub(%r{/\Z}, "")
end
else
k
end
end
content.transform_values! do |v|
if v.is_a?(String)
if v.match?(/\A:(.*)\Z/)
v[1..-1].to_sym
elsif v.match?(/\A[+-]?\d+\Z/)
v.to_i
elsif v.match?(/\Atrue|false\Z/)
v == "true"
elsif v.empty?
nil
else
v
end
elsif v.is_a?(Hash) && v.empty?
nil
else
v
end
elsif v.is_a?(Hash) && v.empty?
nil
else
v
end
end
content
content
else
warn "Failed to load #{filename} because it doesn't contain valid YAML hash"
{}
end
end
private

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

@ -332,10 +332,8 @@ module Gem::GemcutterUtilities
request.basic_auth email, password
end
require "bundler/yaml_serializer"
with_response response do |resp|
profile = Bundler::YAMLSerializer.load clean_text(resp.body)
Gem::ConfigFile.convert_rubygems_config_hash profile
Gem::ConfigFile.load_with_rubygems_config_hash(clean_text(resp.body))
end
end

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

@ -679,10 +679,8 @@ class Gem::TestCase < Test::Unit::TestCase
# Load a YAML file, the psych 3 way
def load_yaml_file(file)
require "bundler/yaml_serializer"
require "rubygems/config_file"
yaml = Bundler::YAMLSerializer.load(File.read(file))
Gem::ConfigFile.convert_rubygems_config_hash(yaml)
Gem::ConfigFile.load_with_rubygems_config_hash(File.read(file))
end
def all_spec_names