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 # Tries to detect, if arguments and environment variables suggest that
18 # 'gem install' is executed from rpmbuild.
21 (ARGV.include?('--install-dir') || ARGV.include?('-i')) && ENV['RPM_PACKAGE_NAME']
26 # Default gems locations allowed on FHS system (/usr, /usr/share).
27 # The locations are derived from directories specified during build
31 @default_locations ||= {
32 :system => previous_but_one_dir_to(RbConfig::CONFIG['vendordir'], RbConfig::CONFIG['RUBY_INSTALL_NAME']),
33 :local => previous_but_one_dir_to(RbConfig::CONFIG['sitedir'], RbConfig::CONFIG['RUBY_INSTALL_NAME'])
38 # For each location provides set of directories for binaries (:bin_dir)
39 # platform independent (:gem_dir) and dependent (:ext_dir) files.
42 @libdir ||= case RUBY_PLATFORM
44 RbConfig::CONFIG['datadir']
46 RbConfig::CONFIG['libdir']
49 @default_dirs ||= default_locations.inject(Hash.new) do |hash, location|
50 destination, path = location
52 hash[destination] = if path
54 :bin_dir => File.join(path, RbConfig::CONFIG['bindir'].split(File::SEPARATOR).last),
55 :gem_dir => File.join(path, RbConfig::CONFIG['datadir'].split(File::SEPARATOR).last, 'gems'),
56 :ext_dir => File.join(path, @libdir.split(File::SEPARATOR).last, 'gems')
71 # Remove methods we are going to override. This avoids "method redefined;"
72 # warnings otherwise issued by Ruby.
74 remove_method :default_dir if method_defined? :default_dir
75 remove_method :default_path if method_defined? :default_path
76 remove_method :default_bindir if method_defined? :default_bindir
77 remove_method :default_ext_dir_for if method_defined? :default_ext_dir_for
80 # RubyGems default overrides.
84 Gem.default_dirs[:local][:gem_dir]
91 path = default_dirs.collect {|location, paths| paths[:gem_dir]}
92 path.unshift Gem.user_dir if File.exist? Gem.user_home
97 Gem.default_dirs[:local][:bin_dir]
99 File.join [Dir.home, 'bin']
103 def default_ext_dir_for base_dir
105 build_dir = base_dir.chomp Gem.default_dirs[:system][:gem_dir]
106 if build_dir != base_dir
107 File.join build_dir, Gem.default_dirs[:system][:ext_dir]
110 dirs = Gem.default_dirs.detect {|location, paths| paths[:gem_dir] == base_dir}
111 dirs && dirs.last[:ext_dir]
113 dir && File.join(dir, RbConfig::CONFIG['RUBY_INSTALL_NAME'])
116 # This method should be available since RubyGems 2.2 until RubyGems 3.0.
117 # https://github.com/rubygems/rubygems/issues/749
118 if method_defined? :install_extension_in_lib
119 remove_method :install_extension_in_lib
121 def install_extension_in_lib