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