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