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