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