[前][次][番号順一覧][スレッド一覧]

ruby-changes:60882

From: Hiroshi <ko1@a...>
Date: Thu, 23 Apr 2020 19:16:24 +0900 (JST)
Subject: [ruby-changes:60882] 5c6269c459 (master): Support XDG_* (#2174)

https://git.ruby-lang.org/ruby.git/commit/?id=5c6269c459

From 5c6269c4593f8b1a83b72e157c460dd2b37338c7 Mon Sep 17 00:00:00 2001
From: Hiroshi SHIBATA <hsbt@r...>
Date: Thu, 23 Apr 2020 19:16:06 +0900
Subject: Support XDG_* (#2174)

* Support XDG_CONFIG_HOME for gemrc.

* Support XDG_DATA_HOME for .gem

* Added test for XDG_DATA_HOME

* Do not reuse environmental variable.

* Unify .rdoc path to RDoc.home.

* Support XDG_DATA_HOME for .rdoc

* Ignore exists?

* Extracted config_home path

* Use XDG_CONFIG_HOME for default credential path

* Fixed inconsistency location.

* Fixed the broken tests.

* Support XDG_CONFIG_HOME for irbrc

* Introduce Gem.cache_home as XDG_CACHE_HOME

* Use Gem.cache_home instead of Gem.config_home for the credential file of RubyGems.

* Initialized the old configurations

* Fixed test failure related the configuration initialization

* restore XDG_DATA_HOME

* Fixed the broken examples of bundler with XDG_*

* Do not modify environmental variable on test file

* Use XDG_DATA_HOME insted of XDG_CACHE_HOME for credential file

* stub out Gem.data_home

* Move dir accessor to defaults.rb file

* Use XDG_DATA_HOME for signed gem features

* Use XDG_DATA_HOME for spec cache

* Do not rely on Gem.user_home

* Gem.user_home is always exists. Don't need to use FileUitls.mkdir_p

* Bump support version to RubyGems 3.2.0+

* Removed the needless fallback configuration

* Fixed the inconsistency methods that are find_config_file and config_file

* Use Gem.configuration.credentials_path instead of hard-coded path

* gem_path is always provided

* Removed the duplicated code of find_home

* Also removed the duplicated code of user_home

* use Gem::UNTAINT instead of untaint for surpressing the warnings

* Use File.directory

* Restore XDG_DATA_HOME

* Use File.write

diff --git a/lib/irb/init.rb b/lib/irb/init.rb
index 37d1f8d..da40bee 100644
--- a/lib/irb/init.rb
+++ b/lib/irb/init.rb
@@ -271,10 +271,19 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/init.rb#L271
     if irbrc = ENV["IRBRC"]
       yield proc{|rc| rc == "rc" ? irbrc : irbrc+rc}
     end
+    if xdg_config_home = ENV["XDG_CONFIG_HOME"]
+      irb_home = File.join(xdg_config_home, "irb")
+      unless File.exist? irb_home
+        require 'fileutils'
+        FileUtils.mkdir_p irb_home
+      end
+      yield proc{|rc| irb_home + "/irb#{rc}"}
+    end
     if home = ENV["HOME"]
       yield proc{|rc| home+"/.irb#{rc}"}
     end
     current_dir = Dir.pwd
+    yield proc{|rc| current_dir+"/.config/irb/irb#{rc}"}
     yield proc{|rc| current_dir+"/.irb#{rc}"}
     yield proc{|rc| current_dir+"/irb#{rc.sub(/\A_?/, '.')}"}
     yield proc{|rc| current_dir+"/_irb#{rc}"}
diff --git a/lib/rdoc.rb b/lib/rdoc.rb
index fc8ad9e..a05391d 100644
--- a/lib/rdoc.rb
+++ b/lib/rdoc.rb
@@ -120,6 +120,24 @@ module RDoc https://github.com/ruby/ruby/blob/trunk/lib/rdoc.rb#L120
     end
   end
 
+  def self.home
+    rdoc_dir = begin
+                File.expand_path('~/.rdoc')
+              rescue ArgumentError
+              end
+
+    if File.directory?(rdoc_dir)
+      rdoc_dir
+    else
+      # XDG
+      xdg_data_home = ENV["XDG_DATA_HOME"] || File.join(File.expand_path("~"), '.local', 'share')
+      unless File.exist?(xdg_data_home)
+        FileUtils.mkdir_p xdg_data_home
+      end
+      File.join xdg_data_home, "rdoc"
+    end
+  end
+
   autoload :RDoc,           'rdoc/rdoc'
 
   autoload :CrossReference, 'rdoc/cross_reference'
diff --git a/lib/rdoc/ri/paths.rb b/lib/rdoc/ri/paths.rb
index b1d5848..7891d1e 100644
--- a/lib/rdoc/ri/paths.rb
+++ b/lib/rdoc/ri/paths.rb
@@ -14,10 +14,7 @@ module RDoc::RI::Paths https://github.com/ruby/ruby/blob/trunk/lib/rdoc/ri/paths.rb#L14
 
   BASE    = File.join RbConfig::CONFIG['ridir'], version
 
-  HOMEDIR = begin
-              File.expand_path('~/.rdoc')
-            rescue ArgumentError
-            end
+  HOMEDIR = RDoc.home
   #:startdoc:
 
   ##
diff --git a/lib/rdoc/store.rb b/lib/rdoc/store.rb
index 0f6cd06..05d8383 100644
--- a/lib/rdoc/store.rb
+++ b/lib/rdoc/store.rb
@@ -482,7 +482,7 @@ class RDoc::Store https://github.com/ruby/ruby/blob/trunk/lib/rdoc/store.rb#L482
     when :gem    then
       parent = File.expand_path '..', @path
       "gem #{File.basename parent}"
-    when :home   then '~/.rdoc'
+    when :home   then RDoc.home
     when :site   then 'ruby site'
     when :system then 'ruby core'
     else @path
diff --git a/lib/rubygems.rb b/lib/rubygems.rb
index 73321ec..5026fc4 100644
--- a/lib/rubygems.rb
+++ b/lib/rubygems.rb
@@ -338,13 +338,6 @@ module Gem https://github.com/ruby/ruby/blob/trunk/lib/rubygems.rb#L338
   end
 
   ##
-  # The path to standard location of the user's .gemrc file.
-
-  def self.config_file
-    @config_file ||= File.join Gem.user_home, '.gemrc'
-  end
-
-  ##
   # The standard configuration object for gems.
 
   def self.configuration
@@ -558,33 +551,6 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} https://github.com/ruby/ruby/blob/trunk/lib/rubygems.rb#L551
   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
-
-  ##
   # Top level install helper method. Allows you to install gems interactively:
   #
   #   % irb
@@ -1057,13 +1023,6 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} https://github.com/ruby/ruby/blob/trunk/lib/rubygems.rb#L1023
   end
 
   ##
-  # The home directory for the user.
-
-  def self.user_home
-    @user_home ||= find_home.tap(&Gem::UNTAINT)
-  end
-
-  ##
   # Is this a windows platform?
 
   def self.win_platform?
diff --git a/lib/rubygems/config_file.rb b/lib/rubygems/config_file.rb
index 54d8a9c..393f947 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. https://github.com/ruby/ruby/blob/trunk/lib/rubygems/config_file.rb#L261
   # 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. https://github.com/ruby/ruby/blob/trunk/lib/rubygems/config_file.rb#L449
 
   # 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 0e83f1b..2d0b077 100644
--- a/lib/rubygems/defaults.rb
+++ b/lib/rubygems/defaults.rb
@@ -20,7 +20,13 @@ module Gem https://github.com/ruby/ruby/blob/trunk/lib/rubygems/defaults.rb#L20
   # 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 https://github.com/ruby/ruby/blob/trunk/lib/rubygems/defaults.rb#L77
   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 ||= ( (... truncated)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]