summaryrefslogtreecommitdiff
path: root/lib/rubygems
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rubygems')
-rw-r--r--lib/rubygems/config_file.rb11
-rw-r--r--lib/rubygems/defaults.rb102
-rw-r--r--lib/rubygems/gemcutter_utilities.rb2
-rw-r--r--lib/rubygems/test_case.rb7
4 files changed, 116 insertions, 6 deletions
diff --git a/lib/rubygems/config_file.rb b/lib/rubygems/config_file.rb
index 54d8a9c152..393f947d77 100644
--- a/lib/rubygems/config_file.rb
+++ b/lib/rubygems/config_file.rb
@@ -261,7 +261,12 @@ if you believe they were disclosed to a third party.
# Location of RubyGems.org credentials
def credentials_path
- File.join Gem.user_home, '.gem', 'credentials'
+ credentials = File.join Gem.user_home, '.gem', 'credentials'
+ if File.exist? credentials
+ credentials
+ else
+ File.join Gem.data_home, "gem", "credentials"
+ end
end
def load_api_keys
@@ -444,6 +449,10 @@ if you believe they were disclosed to a third party.
# Writes out this config file, replacing its source.
def write
+ unless File.exist?(File.dirname(config_file_name))
+ FileUtils.mkdir_p File.dirname(config_file_name)
+ end
+
File.open config_file_name, 'w' do |io|
io.write to_yaml
end
diff --git a/lib/rubygems/defaults.rb b/lib/rubygems/defaults.rb
index 0e83f1b006..2d0b077fdc 100644
--- a/lib/rubygems/defaults.rb
+++ b/lib/rubygems/defaults.rb
@@ -20,7 +20,13 @@ module Gem
# specified in the environment
def self.default_spec_cache_dir
- File.join Gem.user_home, '.gem', 'specs'
+ default_spec_cache_dir = File.join Gem.user_home, '.gem', 'specs'
+
+ unless File.exist?(default_spec_cache_dir)
+ default_spec_cache_dir = File.join Gem.data_home, 'gem', 'specs'
+ end
+
+ default_spec_cache_dir
end
##
@@ -71,15 +77,91 @@ module Gem
end
##
+ # Finds the user's home directory.
+ #--
+ # Some comments from the ruby-talk list regarding finding the home
+ # directory:
+ #
+ # I have HOME, USERPROFILE and HOMEDRIVE + HOMEPATH. Ruby seems
+ # to be depending on HOME in those code samples. I propose that
+ # it should fallback to USERPROFILE and HOMEDRIVE + HOMEPATH (at
+ # least on Win32).
+ #++
+ #--
+ #
+ #++
+
+ def self.find_home
+ Dir.home.dup
+ rescue
+ if Gem.win_platform?
+ File.expand_path File.join(ENV['HOMEDRIVE'] || ENV['SystemDrive'], '/')
+ else
+ File.expand_path "/"
+ end
+ end
+
+ private_class_method :find_home
+
+ ##
+ # The home directory for the user.
+
+ def self.user_home
+ @user_home ||= find_home.tap(&Gem::UNTAINT)
+ end
+
+ ##
# Path for gems in the user's home directory
def self.user_dir
- parts = [Gem.user_home, '.gem', ruby_engine]
+ gem_dir = File.join(Gem.user_home, ".gem")
+ gem_dir = File.join(Gem.data_home, "gem") unless File.exist?(gem_dir)
+ parts = [gem_dir, ruby_engine]
parts << RbConfig::CONFIG['ruby_version'] unless RbConfig::CONFIG['ruby_version'].empty?
File.join parts
end
##
+ # The path to standard location of the user's configuration directory.
+
+ def self.config_home
+ @config_home ||= (ENV["XDG_CONFIG_HOME"] || File.join(Gem.user_home, '.config'))
+ end
+
+ ##
+ # Finds the user's config file
+
+ def self.find_config_file
+ gemrc = File.join Gem.user_home, '.gemrc'
+ if File.exist? gemrc
+ gemrc
+ else
+ File.join Gem.config_home, "gem", "gemrc"
+ end
+ end
+
+ ##
+ # The path to standard location of the user's .gemrc file.
+
+ def self.config_file
+ @config_file ||= find_config_file.tap(&Gem::UNTAINT)
+ end
+
+ ##
+ # The path to standard location of the user's cache directory.
+
+ def self.cache_home
+ @cache_home ||= (ENV["XDG_CACHE_HOME"] || File.join(Gem.user_home, '.cache'))
+ end
+
+ ##
+ # The path to standard location of the user's data directory.
+
+ def self.data_home
+ @data_home ||= (ENV["XDG_DATA_HOME"] || File.join(Gem.user_home, '.local', 'share'))
+ end
+
+ ##
# How String Gem paths should be split. Overridable for esoteric platforms.
def self.path_separator
@@ -130,14 +212,26 @@ module Gem
# The default signing key path
def self.default_key_path
- File.join Gem.user_home, ".gem", "gem-private_key.pem"
+ default_key_path = File.join Gem.user_home, ".gem", "gem-private_key.pem"
+
+ unless File.exist?(default_key_path)
+ default_key_path = File.join Gem.data_home, "gem", "gem-private_key.pem"
+ end
+
+ default_key_path
end
##
# The default signing certificate chain path
def self.default_cert_path
- File.join Gem.user_home, ".gem", "gem-public_cert.pem"
+ default_cert_path = File.join Gem.user_home, ".gem", "gem-public_cert.pem"
+
+ unless File.exist?(default_cert_path)
+ default_cert_path = File.join Gem.data_home, "gem", "gem-public_cert.pem"
+ end
+
+ default_cert_path
end
##
diff --git a/lib/rubygems/gemcutter_utilities.rb b/lib/rubygems/gemcutter_utilities.rb
index 2950d94dc1..bba9280691 100644
--- a/lib/rubygems/gemcutter_utilities.rb
+++ b/lib/rubygems/gemcutter_utilities.rb
@@ -19,7 +19,7 @@ module Gem::GemcutterUtilities
def add_key_option
add_option('-k', '--key KEYNAME', Symbol,
'Use the given API key',
- 'from ~/.gem/credentials') do |value,options|
+ "from #{Gem.configuration.credentials_path}") do |value,options|
options[:key] = value
end
end
diff --git a/lib/rubygems/test_case.rb b/lib/rubygems/test_case.rb
index e56150caf9..d028254465 100644
--- a/lib/rubygems/test_case.rb
+++ b/lib/rubygems/test_case.rb
@@ -363,6 +363,13 @@ class Gem::TestCase < (defined?(Minitest::Test) ? Minitest::Test : MiniTest::Uni
Dir.chdir @tempdir
ENV['HOME'] = @userhome
+ FileUtils.mkdir_p File.join(@userhome, ".gem")
+ File.write File.join(@userhome, ".gemrc"), "--- {}"
+ temp_cred = File.join(@userhome, '.gem', 'credentials')
+ FileUtils.mkdir_p File.dirname(temp_cred)
+ File.write temp_cred, ':rubygems_api_key: 701229f217cdf23b1344c7b4b54ca97'
+ File.chmod 0600, temp_cred
+
Gem.instance_variable_set :@user_home, nil
Gem.instance_variable_set :@gemdeps, nil
Gem.instance_variable_set :@env_requirements_by_name, nil