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