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 12
12:   def initialize
13:     super 'setup', 'Install RubyGems',
14:           :format_executable => true, :rdoc => true, :ri => true,
15:           :site_or_vendor => :sitelibdir,
16:           :destdir => '', :prefix => ''
17: 
18:     add_option '--prefix=PREFIX',
19:                'Prefix path for installing RubyGems',
20:                'Will not affect gem repository location' do |prefix, options|
21:       options[:prefix] = File.expand_path prefix
22:     end
23: 
24:     add_option '--destdir=DESTDIR',
25:                'Root directory to install RubyGems into',
26:                'Mainly used for packaging RubyGems' do |destdir, options|
27:       options[:destdir] = File.expand_path destdir
28:     end
29: 
30:     add_option '--[no-]vendor',
31:                'Install into vendorlibdir not sitelibdir',
32:                '(Requires Ruby 1.8.7)' do |vendor, options|
33:       if vendor and Gem.ruby_version < Gem::Version.new('1.8.7') then
34:         raise OptionParser::InvalidOption,
35:               "requires ruby 1.8.7+ (you have #{Gem.ruby_version})"
36:       end
37: 
38:       options[:site_or_vendor] = vendor ? :vendorlibdir : :sitelibdir
39:     end
40: 
41:     add_option '--[no-]format-executable',
42:                'Makes `gem` match ruby',
43:                'If ruby is ruby18, gem will be gem18' do |value, options|
44:       options[:format_executable] = value
45:     end
46: 
47:     add_option '--[no-]rdoc',
48:                'Generate RDoc documentation for RubyGems' do |value, options|
49:       options[:rdoc] = value
50:     end
51: 
52:     add_option '--[no-]ri',
53:                'Generate RI documentation for RubyGems' do |value, options|
54:       options[:ri] = value
55:     end
56:   end

Public Instance methods

[Source]

    # File lib/rubygems/commands/setup_command.rb, line 58
58:   def check_ruby_version
59:     required_version = Gem::Requirement.new '>= 1.8.6'
60: 
61:     unless required_version.satisfied_by? Gem.ruby_version then
62:       alert_error "Expected Ruby version #{required_version}, is #{Gem.ruby_version}"
63:       terminate_interaction 1
64:     end
65:   end

[Source]

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

[Source]

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

[Source]

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

[Source]

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

[Source]

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

[Source]

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

[Source]

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

[Source]

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

[Source]

     # File lib/rubygems/commands/setup_command.rb, line 366
366:   def uninstall_old_gemcutter
367:     require 'rubygems/uninstaller'
368: 
369:     ui = Gem::Uninstaller.new('gemcutter', :all => true, :ignore => true,
370:                               :version => '< 0.4')
371:     ui.uninstall
372:   rescue Gem::InstallError
373:   end

[Validate]