Skip to content

Commit

Permalink
[rubygems/rubygems] Wrap self.convert_rubygems_config_hash from Bundl…
Browse files Browse the repository at this point in the history
…er::YAMLSerializer.load

rubygems/rubygems@080880ac23
  • Loading branch information
hsbt authored and matzbot committed Apr 19, 2023
1 parent 7b959f6 commit cdc2e3d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 35 deletions.
72 changes: 39 additions & 33 deletions lib/rubygems/config_file.rb
Expand Up @@ -357,39 +357,7 @@ def load_file(filename)
begin
content = Bundler::YAMLSerializer.load(File.read(filename))
if content.is_a? Hash
content.transform_keys! do |k|
if k.match?(/\A:(.*)\Z/)
k[1..-1].to_sym
elsif k.match?(/__/)
if k.is_a?(Symbol)
k.to_s.gsub(/__/,".").to_sym
else
k.dup.gsub(/__/,".")
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
end
content = self.class.convert_rubygems_config_hash(content)
else
warn "Failed to load #{filename} because it doesn't contain valid YAML hash"
return {}
Expand Down Expand Up @@ -564,6 +532,44 @@ def ==(other) # :nodoc:
attr_reader :hash
protected :hash

def self.convert_rubygems_config_hash(content)
content.transform_keys! do |k|
if k.match?(/\A:(.*)\Z/)
k[1..-1].to_sym
elsif k.match?(/__/)
if k.is_a?(Symbol)
k.to_s.gsub(/__/,".").to_sym
else
k.dup.gsub(/__/,".")
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
end

content
end

private

def set_config_file_name(args)
Expand Down
3 changes: 2 additions & 1 deletion lib/rubygems/gemcutter_utilities.rb
Expand Up @@ -334,7 +334,8 @@ def get_user_profile(email, password)

require "bundler/yaml_serializer"
with_response response do |resp|
Bundler::YAMLSerializer.load clean_text(resp.body)
profile = Bundler::YAMLSerializer.load clean_text(resp.body)
Gem::ConfigFile.convert_rubygems_config_hash profile
end
end

Expand Down
4 changes: 3 additions & 1 deletion test/rubygems/helper.rb
Expand Up @@ -680,7 +680,9 @@ def load_yaml(yaml)

def load_yaml_file(file)
require "bundler/yaml_serializer"
Bundler::YAMLSerializer.load(File.read(file))
require "rubygems/config_file"
yaml = Bundler::YAMLSerializer.load(File.read(file))
Gem::ConfigFile.convert_rubygems_config_hash(yaml)
end

def all_spec_names
Expand Down

0 comments on commit cdc2e3d

Please sign in to comment.