]>
Commit | Line | Data |
---|---|---|
a6d548c5 ER |
1 | module Gem |
2 | class << self | |
3 | ||
4 | ## | |
5 | # Returns full path of previous but one directory of dir in path | |
6 | # E.g. for '/usr/share/ruby', 'ruby', it returns '/usr' | |
7 | ||
8 | def previous_but_one_dir_to(path, dir) | |
3112d6a6 ER |
9 | return unless path |
10 | ||
a6d548c5 ER |
11 | split_path = path.split(File::SEPARATOR) |
12 | File.join(split_path.take_while { |one_dir| one_dir !~ /^#{dir}$/ }[0..-2]) | |
13 | end | |
14 | private :previous_but_one_dir_to | |
15 | ||
6b9c4b92 ER |
16 | ## |
17 | # Detects --install-dir option specified on command line. | |
18 | ||
19 | def opt_install_dir? | |
20 | @opt_install_dir ||= ARGV.include?('--install-dir') || ARGV.include?('-i') | |
21 | end | |
22 | private :opt_install_dir? | |
23 | ||
24 | ## | |
25 | # Detects --build-root option specified on command line. | |
26 | ||
27 | def opt_build_root? | |
28 | @opt_build_root ||= ARGV.include?('--build-root') | |
29 | end | |
30 | private :opt_build_root? | |
31 | ||
1a83bc11 ER |
32 | ## |
33 | # Tries to detect, if arguments and environment variables suggest that | |
34 | # 'gem install' is executed from rpmbuild. | |
35 | ||
36 | def rpmbuild? | |
6b9c4b92 | 37 | @rpmbuild ||= ENV['RPM_PACKAGE_NAME'] && (opt_install_dir? || opt_build_root?) |
1a83bc11 ER |
38 | end |
39 | private :rpmbuild? | |
40 | ||
a6d548c5 ER |
41 | ## |
42 | # Default gems locations allowed on FHS system (/usr, /usr/share). | |
43 | # The locations are derived from directories specified during build | |
44 | # configuration. | |
45 | ||
46 | def default_locations | |
47 | @default_locations ||= { | |
3112d6a6 ER |
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']) | |
a6d548c5 ER |
50 | } |
51 | end | |
52 | ||
53 | ## | |
54 | # For each location provides set of directories for binaries (:bin_dir) | |
55 | # platform independent (:gem_dir) and dependent (:ext_dir) files. | |
56 | ||
57 | def default_dirs | |
58 | @libdir ||= case RUBY_PLATFORM | |
59 | when 'java' | |
3112d6a6 | 60 | RbConfig::CONFIG['datadir'] |
a6d548c5 | 61 | else |
3112d6a6 | 62 | RbConfig::CONFIG['libdir'] |
a6d548c5 ER |
63 | end |
64 | ||
3112d6a6 ER |
65 | @default_dirs ||= default_locations.inject(Hash.new) do |hash, location| |
66 | destination, path = location | |
67 | ||
68 | hash[destination] = if path | |
69 | { | |
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') | |
73 | } | |
74 | else | |
75 | { | |
76 | :bin_dir => '', | |
77 | :gem_dir => '', | |
78 | :ext_dir => '' | |
79 | } | |
80 | end | |
81 | ||
82 | hash | |
83 | end | |
a6d548c5 ER |
84 | end |
85 | ||
86 | ## | |
87 | # Remove methods we are going to override. This avoids "method redefined;" | |
88 | # warnings otherwise issued by Ruby. | |
89 | ||
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 | |
94 | ||
95 | ## | |
96 | # RubyGems default overrides. | |
97 | ||
98 | def default_dir | |
6b9c4b92 ER |
99 | if opt_build_root? |
100 | Gem.default_dirs[:system][:gem_dir] | |
101 | elsif Process.uid == 0 | |
a6d548c5 ER |
102 | Gem.default_dirs[:local][:gem_dir] |
103 | else | |
104 | Gem.user_dir | |
105 | end | |
106 | end | |
107 | ||
108 | def default_path | |
109 | path = default_dirs.collect {|location, paths| paths[:gem_dir]} | |
110 | path.unshift Gem.user_dir if File.exist? Gem.user_home | |
111 | end | |
112 | ||
113 | def default_bindir | |
6b9c4b92 ER |
114 | if opt_build_root? |
115 | Gem.default_dirs[:system][:bin_dir] | |
116 | elsif Process.uid == 0 | |
a6d548c5 ER |
117 | Gem.default_dirs[:local][:bin_dir] |
118 | else | |
119 | File.join [Dir.home, 'bin'] | |
120 | end | |
121 | end | |
122 | ||
123 | def default_ext_dir_for base_dir | |
1a83bc11 ER |
124 | dir = if rpmbuild? |
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] | |
128 | end | |
129 | else | |
130 | dirs = Gem.default_dirs.detect {|location, paths| paths[:gem_dir] == base_dir} | |
131 | dirs && dirs.last[:ext_dir] | |
132 | end | |
133 | dir && File.join(dir, RbConfig::CONFIG['RUBY_INSTALL_NAME']) | |
134 | end | |
135 | ||
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 | |
140 | ||
141 | def install_extension_in_lib | |
142 | false | |
143 | end | |
a6d548c5 ER |
144 | end |
145 | end | |
146 | end |