pvalena / rpms / ruby

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