Blame SOURCES/operating_system.rb

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