~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to lib/rubygems/commands/setup_command.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2010-07-31 17:08:39 UTC
  • mfrom: (1.1.4 upstream) (8.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100731170839-j034dmpdqt1cc4p6
Tags: 1.9.2~svn28788-1
* New release based on upstream snapshot from the 1.9.2 branch,
  after 1.9.2 RC2. That branch is (supposed to be) binary-compatible
  with the 1.9.1 branch.
  + Builds fine on i386. Closes: #580852.
* Upgrade to Standards-Version: 3.9.1. No changes needed.
* Updated generated incs.
* Patches that still need work:
  + Unclear status, need more investigation:
   090729_fix_Makefile_deps.dpatch
   090803_exclude_rdoc.dpatch
   203_adjust_base_of_search_path.dpatch
   902_define_YAML_in_yaml_stringio.rb.dpatch
   919_common.mk_tweaks.dpatch
   931_libruby_suffix.dpatch
   940_test_thread_mutex_sync_shorter.dpatch
  + Maybe not needed anymore, keeping but not applying.
   102_skip_test_copy_stream.dpatch (test doesn't block anymore?)
   104_skip_btest_io.dpatch (test doesn't block anymore?)
   201_gem_prelude.dpatch (we don't use that rubygems anyway?)
   202_gem_default_dir.dpatch (we don't use that rubygems anyway?)
   940_test_file_exhaustive_fails_as_root.dpatch
   940_test_priority_fails.dpatch
   100518_load_libc_libm.dpatch
* Add disable-tests.diff: disable some tests that cause failures on FreeBSD.
  Closes: #590002, #543805, #542927.
* However, many new failures on FreeBSD. Since that version is still an
  improvement, add the check that makes test suite failures non-fatal on
  FreeBSD again. That still needs to be investigated.
* Re-add 903_skip_base_ruby_check.dpatch
* Add build-dependency on ruby1.8 and drop all pre-generated files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'rubygems/command'
 
2
require 'fileutils'
 
3
require 'rbconfig'
 
4
require 'tmpdir'
 
5
 
 
6
##
 
7
# Installs RubyGems itself.  This command is ordinarily only available from a
 
8
# RubyGems checkout or tarball.
 
9
 
 
10
class Gem::Commands::SetupCommand < Gem::Command
 
11
 
 
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
 
57
 
 
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
 
66
 
 
67
  def defaults_str # :nodoc:
 
68
    "--format-executable --rdoc --ri"
 
69
  end
 
70
 
 
71
  def description # :nodoc:
 
72
    <<-EOF
 
73
Installs RubyGems itself.
 
74
 
 
75
RubyGems installs RDoc for itself in GEM_HOME.  By default this is:
 
76
  #{Gem.dir}
 
77
 
 
78
If you prefer a different directory, set the GEM_HOME environment variable.
 
79
 
 
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
 
82
installed as `gem18`.
 
83
 
 
84
By default, this RubyGems will install gem as:
 
85
  #{Gem.default_exec_format % 'gem'}
 
86
    EOF
 
87
  end
 
88
 
 
89
  def execute
 
90
    @verbose = Gem.configuration.really_verbose
 
91
 
 
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
    say "RubyGems #{Gem::VERSION} installed"
 
118
 
 
119
    uninstall_old_gemcutter
 
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
 
157
 
 
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 <<-TEXT
 
199
@ECHO OFF
 
200
IF NOT "%~f0" == "~f0" GOTO :WinNT
 
201
@"#{File.basename(Gem.ruby).chomp('"')}" "#{dest_file}" %1 %2 %3 %4 %5 %6 %7 %8 %9
 
202
GOTO :EOF
 
203
:WinNT
 
204
@"#{File.basename(Gem.ruby).chomp('"')}" "%~dpn0" %*
 
205
TEXT
 
206
          end
 
207
 
 
208
          install bin_cmd_file, "#{dest_file}.bat", :mode => 0755
 
209
        ensure
 
210
          rm bin_cmd_file
 
211
        end
 
212
      end
 
213
    end
 
214
  end
 
215
 
 
216
  def install_lib(lib_dir)
 
217
    say "Installing RubyGems" if @verbose
 
218
 
 
219
    Dir.chdir 'lib' do
 
220
      lib_files = Dir[File.join('**', '*rb')]
 
221
 
 
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
 
226
 
 
227
        install lib_file, dest_file, :mode => 0644
 
228
      end
 
229
    end
 
230
  end
 
231
 
 
232
  def install_rdoc
 
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
 
236
 
 
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|
 
242
        rm_rf dir
 
243
      end
 
244
 
 
245
      if options[:ri] then
 
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
 
249
      end
 
250
 
 
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
 
255
      end
 
256
    elsif @verbose then
 
257
      say "Skipping RDoc generation, #{gem_doc_dir} not writable"
 
258
      say "Set the GEM_HOME environment variable if you want RDoc generated"
 
259
    end
 
260
  end
 
261
 
 
262
  def make_destination_dirs(install_destdir)
 
263
    lib_dir = nil
 
264
    bin_dir = nil
 
265
 
 
266
    prefix = options[:prefix]
 
267
    site_or_vendor = options[:site_or_vendor]
 
268
 
 
269
    if prefix.empty? then
 
270
      lib_dir = Gem::ConfigMap[site_or_vendor]
 
271
      bin_dir = Gem::ConfigMap[:bindir]
 
272
    else
 
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]
 
283
      else
 
284
        lib_dir = File.join prefix, 'lib'
 
285
        bin_dir = File.join prefix, 'bin'
 
286
      end
 
287
    end
 
288
 
 
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]:/, '')
 
292
    end
 
293
 
 
294
    mkdir_p lib_dir
 
295
    mkdir_p bin_dir
 
296
 
 
297
    return lib_dir, bin_dir
 
298
  end
 
299
 
 
300
  def remove_old_bin_files(bin_dir)
 
301
    old_bin_files = {
 
302
      'gem_mirror' => 'gem mirror',
 
303
      'gem_server' => 'gem server',
 
304
      'gemlock' => 'gem lock',
 
305
      'gemri' => 'ri',
 
306
      'gemwhich' => 'gem which',
 
307
      'index_gem_repository.rb' => 'gem generate_index',
 
308
    }
 
309
 
 
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
 
313
 
 
314
      deprecation_message = "`#{old_bin_file}` has been deprecated.  Use `#{new_name}` instead."
 
315
 
 
316
      File.open old_bin_path, 'w' do |fp|
 
317
        fp.write <<-EOF
 
318
#!#{Gem.ruby}
 
319
 
 
320
abort "#{deprecation_message}"
 
321
    EOF
 
322
      end
 
323
 
 
324
      next unless Gem.win_platform?
 
325
 
 
326
      File.open "#{old_bin_path}.bat", 'w' do |fp|
 
327
        fp.puts %{@ECHO.#{deprecation_message}}
 
328
      end
 
329
    end
 
330
  end
 
331
 
 
332
  def remove_source_caches(install_destdir)
 
333
    if install_destdir.empty?
 
334
      require 'rubygems/source_info_cache'
 
335
 
 
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)
 
340
 
 
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)
 
344
    end
 
345
  end
 
346
 
 
347
  def run_rdoc(*args)
 
348
    begin
 
349
      gem 'rdoc'
 
350
    rescue Gem::LoadError
 
351
    end
 
352
 
 
353
    require 'rdoc/rdoc'
 
354
 
 
355
    args << '--quiet'
 
356
    args << '--main' << 'README'
 
357
    args << '.' << 'README' << 'LICENSE.txt' << 'GPL.txt'
 
358
 
 
359
    r = RDoc::RDoc.new
 
360
    r.document args
 
361
  end
 
362
 
 
363
  def uninstall_old_gemcutter
 
364
    require 'rubygems/uninstaller'
 
365
 
 
366
    ui = Gem::Uninstaller.new('gemcutter', :all => true, :ignore => true,
 
367
                              :version => '< 0.4')
 
368
    ui.uninstall
 
369
  rescue Gem::InstallError
 
370
  end
 
371
 
 
372
end
 
373