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.

Methods

Public Class methods

[Source]

    # 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

Public Instance methods

[Source]

    # 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

[Source]

     # File lib/rubygems/commands/setup_command.rb, line 91
 91:   def execute
 92:     @verbose = Gem.configuration.really_verbose
 93: 
 94:     install_destdir = options[:destdir]
 95: 
 96:     unless install_destdir.empty? then
 97:       ENV['GEM_HOME'] ||= File.join(install_destdir,
 98:                                     Gem.default_dir.gsub(/^[a-zA-Z]:/, ''))
 99:     end
100: 
101:     check_ruby_version
102: 
103:     if Gem.configuration.really_verbose then
104:       extend FileUtils::Verbose
105:     else
106:       extend FileUtils
107:     end
108: 
109:     lib_dir, bin_dir = make_destination_dirs install_destdir
110: 
111:     install_lib lib_dir
112: 
113:     install_executables bin_dir
114: 
115:     remove_old_bin_files bin_dir
116: 
117:     remove_source_caches install_destdir
118: 
119:     say "RubyGems #{Gem::VERSION} installed"
120: 
121:     install_rdoc
122: 
123:     say
124:     if @verbose then
125:       say "-" * 78
126:       say
127:     end
128: 
129:     release_notes = File.join Dir.pwd, 'History.txt'
130: 
131:     release_notes = if File.exist? release_notes then
132:                       open release_notes do |io|
133:                         text = io.gets '==='
134:                         text << io.gets('===')
135:                         text[0...-3]
136:                       end
137:                     else
138:                       "Oh-no! Unable to find release notes!"
139:                     end
140: 
141:     say release_notes
142: 
143:     say
144:     say "-" * 78
145:     say
146: 
147:     say "RubyGems installed the following executables:"
148:     say @bin_file_names.map { |name| "\t#{name}\n" }
149:     say
150: 
151:     unless @bin_file_names.grep(/#{File::SEPARATOR}gem$/) then
152:       say "If `gem` was installed by a previous RubyGems installation, you may need"
153:       say "to remove it by hand."
154:       say
155:     end
156:   end

[Source]

     # File lib/rubygems/commands/setup_command.rb, line 158
158:   def install_executables(bin_dir)
159:     say "Installing gem executable" if @verbose
160: 
161:     @bin_file_names = []
162: 
163:     Dir.chdir 'bin' do
164:       bin_files = Dir['*']
165: 
166:       bin_files.delete 'update_rubygems'
167: 
168:       bin_files.each do |bin_file|
169:         bin_file_formatted = if options[:format_executable] then
170:                                Gem.default_exec_format % bin_file
171:                              else
172:                                bin_file
173:                              end
174: 
175:         dest_file = File.join bin_dir, bin_file_formatted
176:         bin_tmp_file = File.join Dir.tmpdir, bin_file
177: 
178:         begin
179:           bin = File.readlines bin_file
180:           bin[0] = "#!#{Gem.ruby}\n"
181: 
182:           File.open bin_tmp_file, 'w' do |fp|
183:             fp.puts bin.join
184:           end
185: 
186:           install bin_tmp_file, dest_file, :mode => 0755
187:           @bin_file_names << dest_file
188:         ensure
189:           rm bin_tmp_file
190:         end
191: 
192:         next unless Gem.win_platform?
193: 
194:         begin
195:           bin_cmd_file = File.join Dir.tmpdir, "#{bin_file}.bat"
196: 
197:           File.open bin_cmd_file, 'w' do |file|
198:             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"
199:           end
200: 
201:           install bin_cmd_file, "#{dest_file}.bat", :mode => 0755
202:         ensure
203:           rm bin_cmd_file
204:         end
205:       end
206:     end
207:   end

[Source]

     # File lib/rubygems/commands/setup_command.rb, line 217
217:   def install_lib(lib_dir)
218:     say "Installing RubyGems" if @verbose
219: 
220:     Dir.chdir 'lib' do
221:       lib_files = Dir[File.join('**', '*rb')]
222: 
223:       lib_files.each do |lib_file|
224:         dest_file = File.join lib_dir, lib_file
225:         dest_dir = File.dirname dest_file
226:         mkdir_p dest_dir unless File.directory? dest_dir
227: 
228:         install lib_file, dest_file, :mode => 0644
229:       end
230:     end
231:   end

[Source]

     # File lib/rubygems/commands/setup_command.rb, line 233
233:   def install_rdoc
234:     gem_doc_dir = File.join Gem.dir, 'doc'
235:     rubygems_name = "rubygems-#{Gem::RubyGemsVersion}"
236:     rubygems_doc_dir = File.join gem_doc_dir, rubygems_name
237: 
238:     if File.writable? gem_doc_dir and
239:        (not File.exist? rubygems_doc_dir or
240:         File.writable? rubygems_doc_dir) then
241:       say "Removing old RubyGems RDoc and ri" if @verbose
242:       Dir[File.join(Gem.dir, 'doc', 'rubygems-[0-9]*')].each do |dir|
243:         rm_rf dir
244:       end
245: 
246:       if options[:ri] then
247:         ri_dir = File.join rubygems_doc_dir, 'ri'
248:         say "Installing #{rubygems_name} ri into #{ri_dir}" if @verbose
249:         run_rdoc '--ri', '--op', ri_dir
250:       end
251: 
252:       if options[:rdoc] then
253:         rdoc_dir = File.join rubygems_doc_dir, 'rdoc'
254:         say "Installing #{rubygems_name} rdoc into #{rdoc_dir}" if @verbose
255:         run_rdoc '--op', rdoc_dir
256:       end
257:     elsif @verbose then
258:       say "Skipping RDoc generation, #{gem_doc_dir} not writable"
259:       say "Set the GEM_HOME environment variable if you want RDoc generated"
260:     end
261:   end

[Source]

     # File lib/rubygems/commands/setup_command.rb, line 263
263:   def make_destination_dirs(install_destdir)
264:     lib_dir = nil
265:     bin_dir = nil
266: 
267:     prefix = options[:prefix]
268:     site_or_vendor = options[:site_or_vendor]
269: 
270:     if prefix.empty? then
271:       lib_dir = Gem::ConfigMap[site_or_vendor]
272:       bin_dir = Gem::ConfigMap[:bindir]
273:     else
274:       # Apple installed RubyGems into libdir, and RubyGems <= 1.1.0 gets
275:       # confused about installation location, so switch back to
276:       # sitelibdir/vendorlibdir.
277:       if defined?(APPLE_GEM_HOME) and
278:         # just in case Apple and RubyGems don't get this patched up proper.
279:         (prefix == Gem::ConfigMap[:libdir] or
280:          # this one is important
281:          prefix == File.join(Gem::ConfigMap[:libdir], 'ruby')) then
282:          lib_dir = Gem::ConfigMap[site_or_vendor]
283:          bin_dir = Gem::ConfigMap[:bindir]
284:       else
285:         lib_dir = File.join prefix, 'lib'
286:         bin_dir = File.join prefix, 'bin'
287:       end
288:     end
289: 
290:     unless install_destdir.empty? then
291:       lib_dir = File.join install_destdir, lib_dir.gsub(/^[a-zA-Z]:/, '')
292:       bin_dir = File.join install_destdir, bin_dir.gsub(/^[a-zA-Z]:/, '')
293:     end
294: 
295:     mkdir_p lib_dir
296:     mkdir_p bin_dir
297: 
298:     return lib_dir, bin_dir
299:   end

[Source]

     # File lib/rubygems/commands/setup_command.rb, line 301
301:   def remove_old_bin_files(bin_dir)
302:     old_bin_files = {
303:       'gem_mirror' => 'gem mirror',
304:       'gem_server' => 'gem server',
305:       'gemlock' => 'gem lock',
306:       'gemri' => 'ri',
307:       'gemwhich' => 'gem which',
308:       'index_gem_repository.rb' => 'gem generate_index',
309:     }
310: 
311:     old_bin_files.each do |old_bin_file, new_name|
312:       old_bin_path = File.join bin_dir, old_bin_file
313:       next unless File.exist? old_bin_path
314: 
315:       deprecation_message = "`#{old_bin_file}` has been deprecated.  Use `#{new_name}` instead."
316: 
317:       File.open old_bin_path, 'w' do |fp|
318:         fp.write "#!\#{Gem.ruby}\n\nabort \"\#{deprecation_message}\"\n"
319:       end
320: 
321:       next unless Gem.win_platform?
322: 
323:       File.open "#{old_bin_path}.bat", 'w' do |fp|
324:         fp.puts %{@ECHO.#{deprecation_message}}
325:       end
326:     end
327:   end

[Source]

     # File lib/rubygems/commands/setup_command.rb, line 334
334:   def remove_source_caches(install_destdir)
335:     if install_destdir.empty?
336:       require 'rubygems/source_info_cache'
337: 
338:       user_cache_file = File.join(install_destdir,
339:                                   Gem::SourceInfoCache.user_cache_file)
340:       system_cache_file = File.join(install_destdir,
341:                                     Gem::SourceInfoCache.system_cache_file)
342: 
343:       say "Removing old source_cache files" if Gem.configuration.really_verbose
344:       rm_f user_cache_file if File.writable? File.dirname(user_cache_file)
345:       rm_f system_cache_file if File.writable? File.dirname(system_cache_file)
346:     end
347:   end

[Source]

     # File lib/rubygems/commands/setup_command.rb, line 349
349:   def run_rdoc(*args)
350:     begin
351:       gem 'rdoc'
352:     rescue Gem::LoadError
353:     end
354: 
355:     require 'rdoc/rdoc'
356: 
357:     args << '--quiet'
358:     args << '--main' << 'README'
359:     args << '.' << 'README' << 'LICENSE.txt' << 'GPL.txt'
360: 
361:     r = RDoc::RDoc.new
362:     r.document args
363:   end

[Validate]