Blame SOURCES/operating_system.rb

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