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