]> git.pld-linux.org Git - packages/ruby.git/blob - operating_system.rb
drop no longer used ruby(ver)
[packages/ruby.git] / operating_system.rb
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       return unless path
10
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
16     ##
17     # Tries to detect, if arguments and environment variables suggest that
18     # 'gem install' is executed from rpmbuild.
19
20     def rpmbuild?
21       (ARGV.include?('--install-dir') || ARGV.include?('-i')) && ENV['RPM_PACKAGE_NAME']
22     end
23     private :rpmbuild?
24
25     ##
26     # Default gems locations allowed on FHS system (/usr, /usr/share).
27     # The locations are derived from directories specified during build
28     # configuration.
29
30     def default_locations
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'])
34       }
35     end
36
37     ##
38     # For each location provides set of directories for binaries (:bin_dir)
39     # platform independent (:gem_dir) and dependent (:ext_dir) files.
40
41     def default_dirs
42       @libdir ||= case RUBY_PLATFORM
43       when 'java'
44         RbConfig::CONFIG['datadir']
45       else
46         RbConfig::CONFIG['libdir']
47       end
48
49       @default_dirs ||= default_locations.inject(Hash.new) do |hash, location|
50         destination, path = location
51
52         hash[destination] = if path
53           {
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')
57           }
58         else
59           {
60             :bin_dir => '',
61             :gem_dir => '',
62             :ext_dir => ''
63           }
64         end
65
66         hash
67       end
68     end
69
70     ##
71     # Remove methods we are going to override. This avoids "method redefined;"
72     # warnings otherwise issued by Ruby.
73
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
78
79     ##
80     # RubyGems default overrides.
81
82     def default_dir
83       if Process.uid == 0
84         Gem.default_dirs[:local][:gem_dir]
85       else
86         Gem.user_dir
87       end
88     end
89
90     def default_path
91       path = default_dirs.collect {|location, paths| paths[:gem_dir]}
92       path.unshift Gem.user_dir if File.exist? Gem.user_home
93     end
94
95     def default_bindir
96       if Process.uid == 0
97         Gem.default_dirs[:local][:bin_dir]
98       else
99         File.join [Dir.home, 'bin']
100       end
101     end
102
103     def default_ext_dir_for base_dir
104       dir = if rpmbuild?
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]
108         end
109       else
110         dirs = Gem.default_dirs.detect {|location, paths| paths[:gem_dir] == base_dir}
111         dirs && dirs.last[:ext_dir]
112       end
113       dir && File.join(dir, RbConfig::CONFIG['RUBY_INSTALL_NAME'])
114     end
115
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
120
121       def install_extension_in_lib
122         false
123       end
124     end
125   end
126 end
This page took 0.067401 seconds and 3 git commands to generate.