pvalena / rpms / ruby

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