c03704
module Gem
c03704
  class << self
c03704
c03704
    ##
c03704
    # Returns full path of previous but one directory of dir in path
c03704
    # E.g. for '/usr/share/ruby', 'ruby', it returns '/usr'
c03704
c03704
    def previous_but_one_dir_to(path, dir)
c03704
      return unless path
c03704
c03704
      split_path = path.split(File::SEPARATOR)
c03704
      File.join(split_path.take_while { |one_dir| one_dir !~ /^#{dir}$/ }[0..-2])
c03704
    end
c03704
    private :previous_but_one_dir_to
c03704
c03704
    ##
c03704
    # Detects --install-dir option specified on command line.
c03704
c03704
    def opt_install_dir?
c03704
      @opt_install_dir ||= ARGV.include?('--install-dir') || ARGV.include?('-i')
c03704
    end
c03704
    private :opt_install_dir?
c03704
c03704
    ##
c03704
    # Detects --build-root option specified on command line.
c03704
c03704
    def opt_build_root?
c03704
      @opt_build_root ||= ARGV.include?('--build-root')
c03704
    end
c03704
    private :opt_build_root?
c03704
c03704
    ##
c03704
    # Tries to detect, if arguments and environment variables suggest that
c03704
    # 'gem install' is executed from rpmbuild.
c03704
c03704
    def rpmbuild?
c03704
      @rpmbuild ||= ENV['RPM_PACKAGE_NAME'] && (opt_install_dir? || opt_build_root?)
c03704
    end
c03704
    private :rpmbuild?
c03704
c03704
    ##
c03704
    # Default gems locations allowed on FHS system (/usr, /usr/share).
c03704
    # The locations are derived from directories specified during build
c03704
    # configuration.
c03704
c03704
    def default_locations
c03704
      @default_locations ||= {
c03704
        :system => previous_but_one_dir_to(RbConfig::CONFIG['vendordir'], RbConfig::CONFIG['RUBY_INSTALL_NAME']),
c03704
        :local => previous_but_one_dir_to(RbConfig::CONFIG['sitedir'], RbConfig::CONFIG['RUBY_INSTALL_NAME'])
c03704
      }
c03704
    end
c03704
c03704
    ##
c03704
    # For each location provides set of directories for binaries (:bin_dir)
c03704
    # platform independent (:gem_dir) and dependent (:ext_dir) files.
c03704
c03704
    def default_dirs
c03704
      @libdir ||= case RUBY_PLATFORM
c03704
      when 'java'
c03704
        RbConfig::CONFIG['datadir']
c03704
      else
c03704
        RbConfig::CONFIG['libdir']
c03704
      end
c03704
c03704
      @default_dirs ||= default_locations.inject(Hash.new) do |hash, location|
c03704
        destination, path = location
c03704
c03704
        hash[destination] = if path
c03704
          {
c03704
            :bin_dir => File.join(path, RbConfig::CONFIG['bindir'].split(File::SEPARATOR).last),
c03704
            :gem_dir => File.join(path, RbConfig::CONFIG['datadir'].split(File::SEPARATOR).last, 'gems'),
c03704
            :ext_dir => File.join(path, @libdir.split(File::SEPARATOR).last, 'gems')
c03704
          }
c03704
        else
c03704
          {
c03704
            :bin_dir => '',
c03704
            :gem_dir => '',
c03704
            :ext_dir => ''
c03704
          }
c03704
        end
c03704
c03704
        hash
c03704
      end
c03704
    end
c03704
c03704
    ##
c03704
    # Remove methods we are going to override. This avoids "method redefined;"
c03704
    # warnings otherwise issued by Ruby.
c03704
c03704
    remove_method :operating_system_defaults if method_defined? :operating_system_defaults
c03704
    remove_method :default_dir if method_defined? :default_dir
c03704
    remove_method :default_path if method_defined? :default_path
c03704
    remove_method :default_ext_dir_for if method_defined? :default_ext_dir_for
c03704
c03704
    ##
c03704
    # Regular user installs into user directory, root manages /usr/local.
c03704
c03704
    def operating_system_defaults
c03704
      unless opt_build_root?
c03704
        options = if Process.uid == 0
c03704
          "--install-dir=#{Gem.default_dirs[:local][:gem_dir]} --bindir #{Gem.default_dirs[:local][:bin_dir]}"
c03704
        else
c03704
          "--user-install --bindir #{File.join [Dir.home, 'bin']}"
c03704
        end
c03704
c03704
        {"gem" => options}
c03704
      else
c03704
        {}
c03704
      end
c03704
    end
c03704
c03704
    ##
c03704
    # RubyGems default overrides.
c03704
c03704
    def default_dir
c03704
      Gem.default_dirs[:system][:gem_dir]
c03704
    end
c03704
c03704
    def default_path
c03704
      path = default_dirs.collect {|location, paths| paths[:gem_dir]}
c03704
      path.unshift Gem.user_dir if File.exist? Gem.user_home
c03704
      path
c03704
    end
c03704
c03704
    def default_ext_dir_for base_dir
c03704
      dir = if rpmbuild?
c03704
        build_dir = base_dir.chomp Gem.default_dirs[:system][:gem_dir]
c03704
        if build_dir != base_dir
c03704
          File.join build_dir, Gem.default_dirs[:system][:ext_dir]
c03704
        end
c03704
      else
c03704
        dirs = Gem.default_dirs.detect {|location, paths| paths[:gem_dir] == base_dir}
c03704
        dirs && dirs.last[:ext_dir]
c03704
      end
c03704
      dir && File.join(dir, RbConfig::CONFIG['RUBY_INSTALL_NAME'])
c03704
    end
c03704
c03704
    # This method should be available since RubyGems 2.2 until RubyGems 3.0.
c03704
    # https://github.com/rubygems/rubygems/issues/749
c03704
    if method_defined? :install_extension_in_lib
c03704
      remove_method :install_extension_in_lib
c03704
c03704
      def install_extension_in_lib
c03704
        false
c03704
      end
c03704
    end
c03704
  end
c03704
end