121925
module Gem
121925
  class << self
121925
121925
    ##
121925
    # Returns full path of previous but one directory of dir in path
121925
    # E.g. for '/usr/share/ruby', 'ruby', it returns '/usr'
121925
121925
    def previous_but_one_dir_to(path, dir)
121925
      split_path = path.split(File::SEPARATOR)
121925
      File.join(split_path.take_while { |one_dir| one_dir !~ /^#{dir}$/ }[0..-2])
121925
    end
121925
    private :previous_but_one_dir_to
121925
121925
    ##
121925
    # Default gems locations allowed on FHS system (/usr, /usr/share).
121925
    # The locations are derived from directories specified during build
121925
    # configuration.
121925
121925
    def default_locations
121925
      @default_locations ||= {
121925
        :system => previous_but_one_dir_to(ConfigMap[:vendordir], ConfigMap[:RUBY_INSTALL_NAME]),
121925
        :local => previous_but_one_dir_to(ConfigMap[:sitedir], ConfigMap[:RUBY_INSTALL_NAME])
121925
      }
121925
    end
121925
121925
    ##
121925
    # For each location provides set of directories for binaries (:bin_dir)
121925
    # platform independent (:gem_dir) and dependent (:ext_dir) files.
121925
121925
    def default_dirs
121925
      @libdir ||= case RUBY_PLATFORM
121925
      when 'java'
121925
        ConfigMap[:datadir]
121925
      else
121925
        ConfigMap[:libdir]
121925
      end
121925
121925
      @default_dirs ||= Hash[default_locations.collect do |destination, path|
121925
        [destination, {
121925
          :bin_dir => File.join(path, ConfigMap[:bindir].split(File::SEPARATOR).last),
121925
          :gem_dir => File.join(path, ConfigMap[:datadir].split(File::SEPARATOR).last, 'gems'),
121925
          :ext_dir => File.join(path, @libdir.split(File::SEPARATOR).last, 'gems')
121925
        }]
121925
      end]
121925
    end
121925
121925
    ##
121925
    # Remove methods we are going to override. This avoids "method redefined;"
121925
    # warnings otherwise issued by Ruby.
121925
121925
    remove_method :default_dir if method_defined? :default_dir
121925
    remove_method :default_path if method_defined? :default_path
121925
    remove_method :default_bindir if method_defined? :default_bindir
121925
    remove_method :default_ext_dir_for if method_defined? :default_ext_dir_for
121925
121925
    ##
121925
    # RubyGems default overrides.
121925
121925
    def default_dir
121925
      if Process.uid == 0
121925
        Gem.default_dirs[:local][:gem_dir]
121925
      else
121925
        Gem.user_dir
121925
      end
121925
    end
121925
121925
    def default_path
121925
      path = default_dirs.collect {|location, paths| paths[:gem_dir]}
121925
      path.unshift Gem.user_dir if File.exist? Gem.user_home
121925
      path
121925
    end
121925
121925
    def default_bindir
121925
      if Process.uid == 0
121925
        Gem.default_dirs[:local][:bin_dir]
121925
      else
121925
        File.join [Dir.home, 'bin']
121925
      end
121925
    end
121925
121925
    def default_ext_dir_for base_dir
121925
      dirs = Gem.default_dirs.detect {|location, paths| paths[:gem_dir] == base_dir}
121925
      dirs && File.join(dirs.last[:ext_dir], RbConfig::CONFIG['RUBY_INSTALL_NAME'])
121925
    end
121925
  end
121925
end