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