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