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