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