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