1
require 'rubygems/command'
7
# Installs RubyGems itself. This command is ordinarily only available from a
8
# RubyGems checkout or tarball.
10
class Gem::Commands::SetupCommand < Gem::Command
13
super 'setup', 'Install RubyGems',
14
:format_executable => true, :rdoc => true, :ri => true,
15
:site_or_vendor => :sitelibdir,
16
:destdir => '', :prefix => ''
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
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
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})"
38
options[:site_or_vendor] = vendor ? :vendorlibdir : :sitelibdir
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
47
add_option '--[no-]rdoc',
48
'Generate RDoc documentation for RubyGems' do |value, options|
49
options[:rdoc] = value
52
add_option '--[no-]ri',
53
'Generate RI documentation for RubyGems' do |value, options|
58
def check_ruby_version
59
required_version = Gem::Requirement.new '>= 1.8.6'
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
67
def defaults_str # :nodoc:
68
"--format-executable --rdoc --ri"
71
def description # :nodoc:
73
Installs RubyGems itself.
75
RubyGems installs RDoc for itself in GEM_HOME. By default this is:
78
If you prefer a different directory, set the GEM_HOME environment variable.
80
RubyGems will install the gem command with a name matching ruby's
81
prefix and suffix. If ruby was installed as `ruby18`, gem will be
84
By default, this RubyGems will install gem as:
85
#{Gem.default_exec_format % 'gem'}
90
@verbose = Gem.configuration.really_verbose
92
install_destdir = options[:destdir]
94
unless install_destdir.empty? then
95
ENV['GEM_HOME'] ||= File.join(install_destdir,
96
Gem.default_dir.gsub(/^[a-zA-Z]:/, ''))
101
if Gem.configuration.really_verbose then
102
extend FileUtils::Verbose
107
lib_dir, bin_dir = make_destination_dirs install_destdir
111
install_executables bin_dir
113
remove_old_bin_files bin_dir
115
remove_source_caches install_destdir
117
say "RubyGems #{Gem::VERSION} installed"
119
uninstall_old_gemcutter
129
release_notes = File.join Dir.pwd, 'History.txt'
131
release_notes = if File.exist? release_notes then
132
open release_notes do |io|
134
text << io.gets('===')
138
"Oh-no! Unable to find release notes!"
147
say "RubyGems installed the following executables:"
148
say @bin_file_names.map { |name| "\t#{name}\n" }
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."
158
def install_executables(bin_dir)
159
say "Installing gem executable" if @verbose
166
bin_files.delete 'update_rubygems'
168
bin_files.each do |bin_file|
169
bin_file_formatted = if options[:format_executable] then
170
Gem.default_exec_format % bin_file
175
dest_file = File.join bin_dir, bin_file_formatted
176
bin_tmp_file = File.join Dir.tmpdir, bin_file
179
bin = File.readlines bin_file
180
bin[0] = "#!#{Gem.ruby}\n"
182
File.open bin_tmp_file, 'w' do |fp|
186
install bin_tmp_file, dest_file, :mode => 0755
187
@bin_file_names << dest_file
192
next unless Gem.win_platform?
195
bin_cmd_file = File.join Dir.tmpdir, "#{bin_file}.bat"
197
File.open bin_cmd_file, 'w' do |file|
200
IF NOT "%~f0" == "~f0" GOTO :WinNT
201
@"#{File.basename(Gem.ruby).chomp('"')}" "#{dest_file}" %1 %2 %3 %4 %5 %6 %7 %8 %9
204
@"#{File.basename(Gem.ruby).chomp('"')}" "%~dpn0" %*
208
install bin_cmd_file, "#{dest_file}.bat", :mode => 0755
216
def install_lib(lib_dir)
217
say "Installing RubyGems" if @verbose
220
lib_files = Dir[File.join('**', '*rb')]
222
lib_files.each do |lib_file|
223
dest_file = File.join lib_dir, lib_file
224
dest_dir = File.dirname dest_file
225
mkdir_p dest_dir unless File.directory? dest_dir
227
install lib_file, dest_file, :mode => 0644
233
gem_doc_dir = File.join Gem.dir, 'doc'
234
rubygems_name = "rubygems-#{Gem::VERSION}"
235
rubygems_doc_dir = File.join gem_doc_dir, rubygems_name
237
if File.writable? gem_doc_dir and
238
(not File.exist? rubygems_doc_dir or
239
File.writable? rubygems_doc_dir) then
240
say "Removing old RubyGems RDoc and ri" if @verbose
241
Dir[File.join(Gem.dir, 'doc', 'rubygems-[0-9]*')].each do |dir|
246
ri_dir = File.join rubygems_doc_dir, 'ri'
247
say "Installing #{rubygems_name} ri into #{ri_dir}" if @verbose
248
run_rdoc '--ri', '--op', ri_dir
251
if options[:rdoc] then
252
rdoc_dir = File.join rubygems_doc_dir, 'rdoc'
253
say "Installing #{rubygems_name} rdoc into #{rdoc_dir}" if @verbose
254
run_rdoc '--op', rdoc_dir
257
say "Skipping RDoc generation, #{gem_doc_dir} not writable"
258
say "Set the GEM_HOME environment variable if you want RDoc generated"
262
def make_destination_dirs(install_destdir)
266
prefix = options[:prefix]
267
site_or_vendor = options[:site_or_vendor]
269
if prefix.empty? then
270
lib_dir = Gem::ConfigMap[site_or_vendor]
271
bin_dir = Gem::ConfigMap[:bindir]
273
# Apple installed RubyGems into libdir, and RubyGems <= 1.1.0 gets
274
# confused about installation location, so switch back to
275
# sitelibdir/vendorlibdir.
276
if defined?(APPLE_GEM_HOME) and
277
# just in case Apple and RubyGems don't get this patched up proper.
278
(prefix == Gem::ConfigMap[:libdir] or
279
# this one is important
280
prefix == File.join(Gem::ConfigMap[:libdir], 'ruby')) then
281
lib_dir = Gem::ConfigMap[site_or_vendor]
282
bin_dir = Gem::ConfigMap[:bindir]
284
lib_dir = File.join prefix, 'lib'
285
bin_dir = File.join prefix, 'bin'
289
unless install_destdir.empty? then
290
lib_dir = File.join install_destdir, lib_dir.gsub(/^[a-zA-Z]:/, '')
291
bin_dir = File.join install_destdir, bin_dir.gsub(/^[a-zA-Z]:/, '')
297
return lib_dir, bin_dir
300
def remove_old_bin_files(bin_dir)
302
'gem_mirror' => 'gem mirror',
303
'gem_server' => 'gem server',
304
'gemlock' => 'gem lock',
306
'gemwhich' => 'gem which',
307
'index_gem_repository.rb' => 'gem generate_index',
310
old_bin_files.each do |old_bin_file, new_name|
311
old_bin_path = File.join bin_dir, old_bin_file
312
next unless File.exist? old_bin_path
314
deprecation_message = "`#{old_bin_file}` has been deprecated. Use `#{new_name}` instead."
316
File.open old_bin_path, 'w' do |fp|
320
abort "#{deprecation_message}"
324
next unless Gem.win_platform?
326
File.open "#{old_bin_path}.bat", 'w' do |fp|
327
fp.puts %{@ECHO.#{deprecation_message}}
332
def remove_source_caches(install_destdir)
333
if install_destdir.empty?
334
require 'rubygems/source_info_cache'
336
user_cache_file = File.join(install_destdir,
337
Gem::SourceInfoCache.user_cache_file)
338
system_cache_file = File.join(install_destdir,
339
Gem::SourceInfoCache.system_cache_file)
341
say "Removing old source_cache files" if Gem.configuration.really_verbose
342
rm_f user_cache_file if File.writable? File.dirname(user_cache_file)
343
rm_f system_cache_file if File.writable? File.dirname(system_cache_file)
350
rescue Gem::LoadError
356
args << '--main' << 'README'
357
args << '.' << 'README' << 'LICENSE.txt' << 'GPL.txt'
363
def uninstall_old_gemcutter
364
require 'rubygems/uninstaller'
366
ui = Gem::Uninstaller.new('gemcutter', :all => true, :ignore => true,
369
rescue Gem::InstallError