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