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