| Class | Gem::Commands::SetupCommand |
| In: |
lib/rubygems/commands/setup_command.rb
|
| Parent: | Gem::Command |
Installs RubyGems itself. This command is ordinarily only available from a RubyGems checkout or tarball.
# File lib/rubygems/commands/setup_command.rb, line 13
13: def initialize
14: super 'setup', 'Install RubyGems',
15: :format_executable => true, :rdoc => true, :ri => true,
16: :site_or_vendor => :sitelibdir,
17: :destdir => '', :prefix => ''
18:
19: add_option '--prefix=PREFIX',
20: 'Prefix path for installing RubyGems',
21: 'Will not affect gem repository location' do |prefix, options|
22: options[:prefix] = File.expand_path prefix
23: end
24:
25: add_option '--destdir=DESTDIR',
26: 'Root directory to install RubyGems into',
27: 'Mainly used for packaging RubyGems' do |destdir, options|
28: options[:destdir] = File.expand_path destdir
29: end
30:
31: add_option '--[no-]vendor',
32: 'Install into vendorlibdir not sitelibdir',
33: '(Requires Ruby 1.8.7)' do |vendor, options|
34: if vendor and Gem.ruby_version < Gem::Version.new('1.8.7') then
35: raise OptionParser::InvalidOption,
36: "requires ruby 1.8.7+ (you have #{Gem.ruby_version})"
37: end
38:
39: options[:site_or_vendor] = vendor ? :vendorlibdir : :sitelibdir
40: end
41:
42: add_option '--[no-]format-executable',
43: 'Makes `gem` match ruby',
44: 'If ruby is ruby18, gem will be gem18' do |value, options|
45: options[:format_executable] = value
46: end
47:
48: add_option '--[no-]rdoc',
49: 'Generate RDoc documentation for RubyGems' do |value, options|
50: options[:rdoc] = value
51: end
52:
53: add_option '--[no-]ri',
54: 'Generate RI documentation for RubyGems' do |value, options|
55: options[:ri] = value
56: end
57: end
# File lib/rubygems/commands/setup_command.rb, line 59
59: def check_ruby_version
60: required_version = Gem::Version.new '1.8.3'
61:
62: unless Gem.ruby_version > required_version then
63: alert_error "Ruby version > #{required_version} required, is #{Gem.ruby_version}"
64: terminate_interaction 1
65: end
66: end
# File lib/rubygems/commands/setup_command.rb, line 91
91: def execute
92: install_destdir = options[:destdir]
93:
94: unless install_destdir.empty? then
95: ENV['GEM_HOME'] ||= File.join(install_destdir,
96: Gem.default_dir.gsub(/^[a-zA-Z]:/, ''))
97: end
98:
99: check_ruby_version
100:
101: if Gem.configuration.really_verbose then
102: extend FileUtils::Verbose
103: else
104: extend FileUtils
105: end
106:
107: lib_dir, bin_dir = make_destination_dirs install_destdir
108:
109: install_lib lib_dir
110:
111: install_executables bin_dir
112:
113: remove_old_bin_files bin_dir
114:
115: remove_source_caches install_destdir
116:
117: install_rdoc
118:
119: say
120: say "-" * 78
121: say
122:
123: release_notes = File.join Dir.pwd, 'doc', 'release_notes',
124: "rel_#{Gem::RubyGemsVersion.gsub '.', '_'}.rdoc"
125:
126: if File.exist? release_notes then
127: say File.read(release_notes)
128: else
129: say "Oh-no! Unable to find release notes!"
130: say "Looked in: #{release_notes}" if Gem.configuration.really_verbose
131: end
132:
133: say
134: say "-" * 78
135: say
136:
137: say "RubyGems installed the following executables:"
138: say @bin_file_names.map { |name| "\t#{name}\n" }
139: say
140:
141: unless @bin_file_names.grep(/#{File::SEPARATOR}gem$/) then
142: say "If `gem` was installed by a previous RubyGems installation, you may need"
143: say "to remove it by hand."
144: say
145: end
146: end
# File lib/rubygems/commands/setup_command.rb, line 148
148: def install_executables(bin_dir)
149: say "Installing gem executable"
150:
151: @bin_file_names = []
152:
153: Dir.chdir 'bin' do
154: bin_files = Dir['*']
155:
156: bin_files.delete 'update_rubygems'
157:
158: bin_files.each do |bin_file|
159: bin_file_formatted = if options[:format_executable] then
160: Gem.default_exec_format % bin_file
161: else
162: bin_file
163: end
164:
165: dest_file = File.join bin_dir, bin_file_formatted
166: bin_tmp_file = File.join Dir.tmpdir, bin_file
167:
168: begin
169: bin = File.readlines bin_file
170: bin[0] = "#!#{Gem.ruby}\n"
171:
172: File.open bin_tmp_file, 'w' do |fp|
173: fp.puts bin.join
174: end
175:
176: install bin_tmp_file, dest_file, :mode => 0755
177: @bin_file_names << dest_file
178: ensure
179: rm bin_tmp_file
180: end
181:
182: next unless Gem.win_platform?
183:
184: begin
185: bin_cmd_file = File.join Dir.tmpdir, "#{bin_file}.bat"
186:
187: File.open bin_cmd_file, 'w' do |file|
188: file.puts "@ECHO OFF\nIF NOT \"%~f0\" == \"~f0\" GOTO :WinNT\n@\"\#{File.basename(Gem.ruby).chomp('\"')}\" \"\#{dest_file}\" %1 %2 %3 %4 %5 %6 %7 %8 %9\nGOTO :EOF\n:WinNT\n@\"\#{File.basename(Gem.ruby).chomp('\"')}\" \"%~dpn0\" %*\n"
189: end
190:
191: install bin_cmd_file, "#{dest_file}.bat", :mode => 0755
192: ensure
193: rm bin_cmd_file
194: end
195: end
196: end
197: end
# File lib/rubygems/commands/setup_command.rb, line 207
207: def install_lib(lib_dir)
208: say "Installing RubyGems"
209:
210: Dir.chdir 'lib' do
211: lib_files = Dir[File.join('**', '*rb')]
212:
213: lib_files.each do |lib_file|
214: dest_file = File.join lib_dir, lib_file
215: dest_dir = File.dirname dest_file
216: mkdir_p dest_dir unless File.directory? dest_dir
217:
218: install lib_file, dest_file, :mode => 0644
219: end
220: end
221: end
# File lib/rubygems/commands/setup_command.rb, line 223
223: def install_rdoc
224: gem_doc_dir = File.join Gem.dir, 'doc'
225: rubygems_name = "rubygems-#{Gem::RubyGemsVersion}"
226: rubygems_doc_dir = File.join gem_doc_dir, rubygems_name
227:
228: if File.writable? gem_doc_dir and
229: (not File.exist? rubygems_doc_dir or
230: File.writable? rubygems_doc_dir) then
231: say "Removing old RubyGems RDoc and ri"
232: Dir[File.join(Gem.dir, 'doc', 'rubygems-[0-9]*')].each do |dir|
233: rm_rf dir
234: end
235:
236: if options[:ri] then
237: ri_dir = File.join rubygems_doc_dir, 'ri'
238: say "Installing #{rubygems_name} ri into #{ri_dir}"
239: run_rdoc '--ri', '--op', ri_dir
240: end
241:
242: if options[:rdoc] then
243: rdoc_dir = File.join rubygems_doc_dir, 'rdoc'
244: say "Installing #{rubygems_name} rdoc into #{rdoc_dir}"
245: run_rdoc '--op', rdoc_dir
246: end
247: else
248: say "Skipping RDoc generation, #{gem_doc_dir} not writable"
249: say "Set the GEM_HOME environment variable if you want RDoc generated"
250: end
251: end
# File lib/rubygems/commands/setup_command.rb, line 253
253: def make_destination_dirs(install_destdir)
254: lib_dir = nil
255: bin_dir = nil
256:
257: prefix = options[:prefix]
258: site_or_vendor = options[:site_or_vendor]
259:
260: if prefix.empty? then
261: lib_dir = Gem::ConfigMap[site_or_vendor]
262: bin_dir = Gem::ConfigMap[:bindir]
263: else
264: # Apple installed RubyGems into libdir, and RubyGems <= 1.1.0 gets
265: # confused about installation location, so switch back to
266: # sitelibdir/vendorlibdir.
267: if defined?(APPLE_GEM_HOME) and
268: # just in case Apple and RubyGems don't get this patched up proper.
269: (prefix == Gem::ConfigMap[:libdir] or
270: # this one is important
271: prefix == File.join(Gem::ConfigMap[:libdir], 'ruby')) then
272: lib_dir = Gem::ConfigMap[site_or_vendor]
273: bin_dir = Gem::ConfigMap[:bindir]
274: else
275: lib_dir = File.join prefix, 'lib'
276: bin_dir = File.join prefix, 'bin'
277: end
278: end
279:
280: unless install_destdir.empty? then
281: lib_dir = File.join install_destdir, lib_dir.gsub(/^[a-zA-Z]:/, '')
282: bin_dir = File.join install_destdir, bin_dir.gsub(/^[a-zA-Z]:/, '')
283: end
284:
285: mkdir_p lib_dir
286: mkdir_p bin_dir
287:
288: return lib_dir, bin_dir
289: end
# File lib/rubygems/commands/setup_command.rb, line 291
291: def remove_old_bin_files(bin_dir)
292: old_bin_files = {
293: 'gem_mirror' => 'gem mirror',
294: 'gem_server' => 'gem server',
295: 'gemlock' => 'gem lock',
296: 'gemri' => 'ri',
297: 'gemwhich' => 'gem which',
298: 'index_gem_repository.rb' => 'gem generate_index',
299: }
300:
301: old_bin_files.each do |old_bin_file, new_name|
302: old_bin_path = File.join bin_dir, old_bin_file
303: next unless File.exist? old_bin_path
304:
305: deprecation_message = "`#{old_bin_file}` has been deprecated. Use `#{new_name}` instead."
306:
307: File.open old_bin_path, 'w' do |fp|
308: fp.write "#!\#{Gem.ruby}\n\nabort \"\#{deprecation_message}\"\n"
309: end
310:
311: next unless Gem.win_platform?
312:
313: File.open "#{old_bin_path}.bat", 'w' do |fp|
314: fp.puts %{@ECHO.#{deprecation_message}}
315: end
316: end
317: end
# File lib/rubygems/commands/setup_command.rb, line 324
324: def remove_source_caches(install_destdir)
325: if install_destdir.empty?
326: require 'rubygems/source_info_cache'
327:
328: user_cache_file = File.join(install_destdir,
329: Gem::SourceInfoCache.user_cache_file)
330: system_cache_file = File.join(install_destdir,
331: Gem::SourceInfoCache.system_cache_file)
332:
333: say "Removing old source_cache files"
334: rm_f user_cache_file if File.writable? File.dirname(user_cache_file)
335: rm_f system_cache_file if File.writable? File.dirname(system_cache_file)
336: end
337: end
# File lib/rubygems/commands/setup_command.rb, line 339
339: def run_rdoc(*args)
340: begin
341: gem 'rdoc'
342: rescue Gem::LoadError
343: end
344:
345: require 'rdoc/rdoc'
346:
347: args << '--quiet'
348: args << '--main' << 'README'
349: args << '.' << 'README' << 'LICENSE.txt' << 'GPL.txt'
350:
351: r = RDoc::RDoc.new
352: r.document args
353: end