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