Class: Gem::InstallCommand

Inherits:
Command
  • Object
show all
Includes:
CommandAids, InstallUpdateOptions, LocalRemoteOptions, VersionOption
Defined in:
lib/rubygems/gem_commands.rb

Overview

Gem install command.

Instance Attribute Summary

Attributes inherited from Command

#command, #defaults, #options, #program_name, #summary

Instance Method Summary collapse

Methods included from InstallUpdateOptions

#add_install_update_options, #install_update_defaults_str

Methods included from LocalRemoteOptions

#add_local_remote_options, #local?, #remote?

Methods included from VersionOption

#add_version_option

Methods included from CommandAids

#begins?, #get_all_gem_names, #get_one_gem_name, #get_one_optional_argument

Methods inherited from Command

add_common_option, #add_option, add_specific_extra_args, common_options, extra_args, extra_args=, #handles?, #invoke, #merge_options, #remove_option, #show_help, specific_extra_args, specific_extra_args_hash, #when_invoked

Methods included from DefaultUserInteraction

#ui, ui, #ui=, ui=, #use_ui, use_ui

Constructor Details

#initializeInstallCommand

Returns a new instance of InstallCommand.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/rubygems/gem_commands.rb', line 199

def initialize
  super(
    'install',
    'Install a gem into the local repository',
    {
      :domain => :both, 
      :generate_rdoc => true,
      :generate_ri   => true,
      :force => false, 
      :test => false, 
      :wrappers => true,
      :version => "> 0",
      :install_dir => Gem.dir,
      :security_policy => nil,
    })
  add_version_option('install')
  add_local_remote_options
  add_install_update_options
end

Instance Method Details

#argumentsObject



224
225
226
# File 'lib/rubygems/gem_commands.rb', line 224

def arguments
  "GEMNAME   name of gem to install"
end

#defaults_strObject



228
229
230
231
# File 'lib/rubygems/gem_commands.rb', line 228

def defaults_str
  "--both --version '> 0' --rdoc --ri --no-force --no-test\n" +
  "--install-dir #{Gem.dir}"
end

#executeObject



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/rubygems/gem_commands.rb', line 233

def execute
  ENV['GEM_PATH'] = options[:install_dir]
  if(options[:args].empty?)
    fail Gem::CommandLineError,
      "Please specify a gem name on the command line (e.g. gem build GEMNAME)"
  end
  options[:args].each do |gem_name|
    if local?
      begin
        entries = []
          if(File.exist?(gem_name) && !File.directory?(gem_name))
            entries << gem_name
          else
            filepattern = gem_name + "*.gem"
            entries = Dir[filepattern] 
          end
          unless entries.size > 0
            if options[:domain] == :local
              alert_error "Local gem file not found: #{filepattern}"
            end
          else
            result = Gem::Installer.new(entries.last, options).install(options[:force],     options[:install_dir])
            installed_gems = [result].flatten
            say "Successfully installed #{installed_gems[0].name}, " +
            "version #{installed_gems[0].version}" if installed_gems
        end
      rescue LocalInstallationError => e
        say " -> Local installation can't proceed: #{e.message}"
      rescue Gem::LoadError => e
        say " -> Local installation can't proceed due to LoadError: #{e.message}"
      rescue Gem::InstallError => e
        raise "Error instaling #{gem_name}:\n\t#{e.message}"
      rescue => e
        # TODO: Fix this handle to allow the error to propagate to
        # the top level handler.  Examine the other errors as
        # well.  This implementation here looks suspicious to me --
        # JimWeirich (4/Jan/05) 
        alert_error "Error installing gem #{gem_name}[.gem]: #{e.message}"
        return
      end
    end
    
    if remote? && installed_gems.nil?
      installer = Gem::RemoteInstaller.new(options)
      installed_gems = installer.install(
        gem_name,
        options[:version],
        options[:force],
        options[:install_dir])
      if installed_gems
        installed_gems.compact!
        installed_gems.each do |spec|
          say "Successfully installed #{spec.full_name}"
        end
      end
    end
    
    unless installed_gems
      alert_error "Could not install a local " +
        "or remote copy of the gem: #{gem_name}"
      terminate_interaction(1)
    end
    
    # NOTE: *All* of the RI documents must be generated first.
    # For some reason, RI docs cannot be generated after any RDoc
    # documents are generated.

    if options[:generate_ri]
      installed_gems.each do |gem|
        Gem::DocManager.new(gem, options[:rdoc_args]).generate_ri
      end
    end

    if options[:generate_rdoc]
      installed_gems.each do |gem|
        Gem::DocManager.new(gem, options[:rdoc_args]).generate_rdoc
      end
    end

    if options[:test]
      installed_gems.each do |spec|
        gem_spec = Gem::SourceIndex.from_installed_gems.search(spec.name, spec.version.version).first
        result = Gem::Validator.new.unit_test(gem_spec)
        unless result.passed?
          unless ask_yes_no("...keep Gem?", true) then
            Gem::Uninstaller.new(spec.name, spec.version.version).uninstall
          end
        end
      end
    end
  end
end

#usageObject



219
220
221
222
# File 'lib/rubygems/gem_commands.rb', line 219

def usage
  "#{program_name} GEMNAME [options]
   or: #{program_name} GEMNAME [options] -- --build-flags"
end