Blame SOURCES/operating_system.rb

d54c42
module Gem
d54c42
  class << self
d54c42
d54c42
    ##
d54c42
    # Returns full path of previous but one directory of dir in path
d54c42
    # E.g. for '/usr/share/ruby', 'ruby', it returns '/usr'
d54c42
d54c42
    def previous_but_one_dir_to(path, dir)
d54c42
      return unless path
d54c42
d54c42
      split_path = path.split(File::SEPARATOR)
d54c42
      File.join(split_path.take_while { |one_dir| one_dir !~ /^#{dir}$/ }[0..-2])
d54c42
    end
d54c42
    private :previous_but_one_dir_to
d54c42
d54c42
    ##
d54c42
    # Tries to detect, if arguments and environment variables suggest that
d54c42
    # 'gem install' is executed from rpmbuild.
d54c42
d54c42
    def rpmbuild?
d54c42
      (ARGV.include?('--install-dir') || ARGV.include?('-i')) && ENV['RPM_PACKAGE_NAME']
d54c42
    end
d54c42
    private :rpmbuild?
d54c42
d54c42
    ##
d54c42
    # Default gems locations allowed on FHS system (/usr, /usr/share).
d54c42
    # The locations are derived from directories specified during build
d54c42
    # configuration.
d54c42
d54c42
    def default_locations
d54c42
      @default_locations ||= {
d54c42
        :system => previous_but_one_dir_to(RbConfig::CONFIG['vendordir'], RbConfig::CONFIG['RUBY_INSTALL_NAME']),
d54c42
        :local => previous_but_one_dir_to(RbConfig::CONFIG['sitedir'], RbConfig::CONFIG['RUBY_INSTALL_NAME'])
d54c42
      }
d54c42
d54c42
      # Add additional default locations for enabled software collections
d54c42
      # Dependent scls needs to add themselves on $GEM_PATH
d54c42
      if ENV['GEM_PATH']
d54c42
        gem_paths = ENV['GEM_PATH'].split(':')
d54c42
d54c42
        ENV['X_SCLS'].split(' ').each do |scl|
d54c42
          next if scl == '@SCL@'
d54c42
d54c42
          regexp = /#{scl}\/root\/usr\/share\/gems/
d54c42
          scl_gem_path = gem_paths.grep(regexp)[0]
d54c42
          if scl_gem_path
d54c42
            prefix = scl_gem_path.gsub(/\A(.*)#{regexp}\z/, "\\1")
d54c42
            @default_locations["#{scl}_system".to_sym] = "#{prefix}#{scl}/root/usr"
d54c42
            @default_locations["#{scl}_local".to_sym] = "#{prefix}#{scl}/root/usr/local"
d54c42
          end
d54c42
        end if ENV['X_SCLS']
d54c42
      end
d54c42
d54c42
      @default_locations
d54c42
    end
d54c42
d54c42
    ##
d54c42
    # For each location provides set of directories for binaries (:bin_dir)
d54c42
    # platform independent (:gem_dir) and dependent (:ext_dir) files.
d54c42
d54c42
    def default_dirs
d54c42
      @libdir ||= case RUBY_PLATFORM
d54c42
      when 'java'
d54c42
        RbConfig::CONFIG['datadir']
d54c42
      else
d54c42
        RbConfig::CONFIG['libdir']
d54c42
      end
d54c42
d54c42
      @default_dirs ||= default_locations.inject(Hash.new) do |hash, location|
d54c42
        destination, path = location
d54c42
d54c42
        hash[destination] = if path
d54c42
          {
d54c42
            :bin_dir => File.join(path, RbConfig::CONFIG['bindir'].split(File::SEPARATOR).last),
d54c42
            :gem_dir => File.join(path, RbConfig::CONFIG['datadir'].split(File::SEPARATOR).last, 'gems'),
d54c42
            :ext_dir => File.join(path, @libdir.split(File::SEPARATOR).last, 'gems')
d54c42
          }
d54c42
        else
d54c42
          {
d54c42
            :bin_dir => '',
d54c42
            :gem_dir => '',
d54c42
            :ext_dir => ''
d54c42
          }
d54c42
        end
d54c42
d54c42
        hash
d54c42
      end
d54c42
    end
d54c42
d54c42
    ##
d54c42
    # Remove methods we are going to override. This avoids "method redefined;"
d54c42
    # warnings otherwise issued by Ruby.
d54c42
d54c42
    remove_method :default_dir if method_defined? :default_dir
d54c42
    remove_method :default_path if method_defined? :default_path
d54c42
    remove_method :default_bindir if method_defined? :default_bindir
d54c42
    remove_method :default_ext_dir_for if method_defined? :default_ext_dir_for
d54c42
d54c42
    ##
d54c42
    # RubyGems default overrides.
d54c42
d54c42
    def default_dir
d54c42
      if Process.uid == 0
d54c42
        Gem.default_dirs[:local][:gem_dir]
d54c42
      else
d54c42
        Gem.user_dir
d54c42
      end
d54c42
    end
d54c42
d54c42
    def default_path
d54c42
      path = default_dirs.collect {|location, paths| paths[:gem_dir]}
d54c42
      path.unshift Gem.user_dir if File.exist? Gem.user_home
d54c42
    end
d54c42
d54c42
    def default_bindir
d54c42
      if Process.uid == 0
d54c42
        Gem.default_dirs[:local][:bin_dir]
d54c42
      else
d54c42
        File.join [Dir.home, 'bin']
d54c42
      end
d54c42
    end
d54c42
d54c42
    def default_ext_dir_for base_dir
d54c42
      dir = if rpmbuild?
d54c42
        scl_prefix = ENV['X_SCLS'].split(' ').detect {|c| c != '@SCL@' && base_dir =~ /\/#{c}\//}
d54c42
        scl_prefix = scl_prefix ? scl_prefix + '_': nil
d54c42
d54c42
        build_dir = base_dir.chomp Gem.default_dirs[:"#{scl_prefix}system"][:gem_dir]
d54c42
        if build_dir != base_dir
d54c42
          File.join build_dir, Gem.default_dirs[:"#{scl_prefix}system"][:ext_dir]
d54c42
        end
d54c42
      else
d54c42
        dirs = Gem.default_dirs.detect {|location, paths| paths[:gem_dir] == base_dir}
d54c42
        dirs && dirs.last[:ext_dir]
d54c42
      end
d54c42
      dir && File.join(dir, RbConfig::CONFIG['RUBY_INSTALL_NAME'])
d54c42
    end
d54c42
d54c42
d54c42
    # This method should be available since RubyGems 2.2 until RubyGems 3.0.
d54c42
    # https://github.com/rubygems/rubygems/issues/749
d54c42
    if method_defined? :install_extension_in_lib
d54c42
      remove_method :install_extension_in_lib
d54c42
d54c42
      def install_extension_in_lib
d54c42
        false
d54c42
      end
d54c42
    end
d54c42
  end
d54c42
end