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) |
9 |
split_path = path.split(File::SEPARATOR) |
10 |
File.join(split_path.take_while { |one_dir| one_dir !~ /^#{dir}$/ }[0..-2]) |
11 |
end |
12 |
private :previous_but_one_dir_to |
13 |
|
14 |
## |
15 |
# Default gems locations allowed on FHS system (/usr, /usr/share). |
16 |
# The locations are derived from directories specified during build |
17 |
# configuration. |
18 |
|
19 |
def default_locations |
20 |
@default_locations ||= { |
21 |
:system => previous_but_one_dir_to(ConfigMap[:vendordir], ConfigMap[:RUBY_INSTALL_NAME]), |
22 |
:local => previous_but_one_dir_to(ConfigMap[:sitedir], ConfigMap[:RUBY_INSTALL_NAME]) |
23 |
} |
24 |
end |
25 |
|
26 |
## |
27 |
# For each location provides set of directories for binaries (:bin_dir) |
28 |
# platform independent (:gem_dir) and dependent (:ext_dir) files. |
29 |
|
30 |
def default_dirs |
31 |
@libdir ||= ConfigMap[:sitelibdir] == ConfigMap[:sitearchdir] ? ConfigMap[:datadir] : ConfigMap[:libdir] |
32 |
@default_dirs ||= Hash[default_locations.collect do |destination, path| |
33 |
[destination, { |
34 |
:bin_dir => File.join(path, ConfigMap[:bindir].split(File::SEPARATOR).last), |
35 |
:gem_dir => File.join(path, ConfigMap[:datadir].split(File::SEPARATOR).last, 'gems'), |
36 |
:ext_dir => File.join(path, @libdir.split(File::SEPARATOR).last, 'gems') |
37 |
}] |
38 |
end] |
39 |
end |
40 |
|
41 |
## |
42 |
# Remove methods we are going to override. This avoids "method redefined;" |
43 |
# warnings otherwise issued by Ruby. |
44 |
|
45 |
remove_method :default_dir if method_defined? :default_dir |
46 |
remove_method :default_path if method_defined? :default_path |
47 |
remove_method :default_bindir if method_defined? :default_bindir |
48 |
remove_method :default_ext_dir_for if method_defined? :default_ext_dir_for |
49 |
|
50 |
## |
51 |
# RubyGems default overrides. |
52 |
|
53 |
def default_dir |
54 |
if Process.uid == 0 |
55 |
Gem.default_dirs[:local][:gem_dir] |
56 |
else |
57 |
Gem.user_dir |
58 |
end |
59 |
end |
60 |
|
61 |
def default_path |
62 |
path = default_dirs.collect {|location, paths| paths[:gem_dir]} |
63 |
path.unshift Gem.user_dir if File.exist? Gem.user_home |
64 |
end |
65 |
|
66 |
def default_bindir |
67 |
if Process.uid == 0 |
68 |
Gem.default_dirs[:local][:bin_dir] |
69 |
else |
70 |
File.join [Dir.home, 'bin'] |
71 |
end |
72 |
end |
73 |
|
74 |
def default_ext_dir_for base_dir |
75 |
dirs = Gem.default_dirs.detect {|location, paths| paths[:gem_dir] == base_dir} |
76 |
dirs && File.join(dirs.last[:ext_dir], RbConfig::CONFIG['RUBY_INSTALL_NAME']) |
77 |
end |
78 |
end |
79 |
end |