5 # Returns full path of previous but one directory of dir in path
6 # E.g. for '/usr/share/ruby', 'ruby', it returns '/usr'
8 def previous_but_one_dir_to(path, dir)
11 split_path = path.split(File::SEPARATOR)
12 File.join(split_path.take_while { |one_dir| one_dir !~ /^#{dir}$/ }[0..-2])
14 private :previous_but_one_dir_to
17 # Detects --install-dir option specified on command line.
20 @opt_install_dir ||= ARGV.include?('--install-dir') || ARGV.include?('-i')
22 private :opt_install_dir?
25 # Detects --build-root option specified on command line.
28 @opt_build_root ||= ARGV.include?('--build-root')
30 private :opt_build_root?
33 # Tries to detect, if arguments and environment variables suggest that
34 # 'gem install' is executed from rpmbuild.
37 @rpmbuild ||= ENV['RPM_PACKAGE_NAME'] && (opt_install_dir? || opt_build_root?)
42 # Default gems locations allowed on FHS system (/usr, /usr/share).
43 # The locations are derived from directories specified during build
47 @default_locations ||= {
48 :system => previous_but_one_dir_to(RbConfig::CONFIG['vendordir'], RbConfig::CONFIG['RUBY_INSTALL_NAME']),
49 :local => previous_but_one_dir_to(RbConfig::CONFIG['sitedir'], RbConfig::CONFIG['RUBY_INSTALL_NAME'])
54 # For each location provides set of directories for binaries (:bin_dir)
55 # platform independent (:gem_dir) and dependent (:ext_dir) files.
58 @libdir ||= case RUBY_PLATFORM
60 RbConfig::CONFIG['datadir']
62 RbConfig::CONFIG['libdir']
65 @default_dirs ||= default_locations.inject(Hash.new) do |hash, location|
66 destination, path = location
68 hash[destination] = if path
70 :bin_dir => File.join(path, RbConfig::CONFIG['bindir'].split(File::SEPARATOR).last),
71 :gem_dir => File.join(path, RbConfig::CONFIG['datadir'].split(File::SEPARATOR).last, 'gems'),
72 :ext_dir => File.join(path, @libdir.split(File::SEPARATOR).last, 'gems')
87 # Remove methods we are going to override. This avoids "method redefined;"
88 # warnings otherwise issued by Ruby.
90 remove_method :default_dir if method_defined? :default_dir
91 remove_method :default_path if method_defined? :default_path
92 remove_method :default_bindir if method_defined? :default_bindir
93 remove_method :default_ext_dir_for if method_defined? :default_ext_dir_for
96 # RubyGems default overrides.
100 Gem.default_dirs[:system][:gem_dir]
101 elsif Process.uid == 0
102 Gem.default_dirs[:local][:gem_dir]
109 path = default_dirs.collect {|location, paths| paths[:gem_dir]}
110 path.unshift Gem.user_dir if File.exist? Gem.user_home
115 Gem.default_dirs[:system][:bin_dir]
116 elsif Process.uid == 0
117 Gem.default_dirs[:local][:bin_dir]
119 File.join [Dir.home, 'bin']
123 def default_ext_dir_for base_dir
125 build_dir = base_dir.chomp Gem.default_dirs[:system][:gem_dir]
126 if build_dir != base_dir
127 File.join build_dir, Gem.default_dirs[:system][:ext_dir]
130 dirs = Gem.default_dirs.detect {|location, paths| paths[:gem_dir] == base_dir}
131 dirs && dirs.last[:ext_dir]
133 dir && File.join(dir, RbConfig::CONFIG['RUBY_INSTALL_NAME'])
136 # This method should be available since RubyGems 2.2 until RubyGems 3.0.
137 # https://github.com/rubygems/rubygems/issues/749
138 if method_defined? :install_extension_in_lib
139 remove_method :install_extension_in_lib
141 def install_extension_in_lib