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