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