Class Gem::Commands::UpdateCommand
In: lib/rubygems/commands/update_command.rb
Parent: Gem::Command

Methods

Included Modules

Gem::InstallUpdateOptions Gem::LocalRemoteOptions Gem::VersionOption

Public Class methods

[Source]

    # File lib/rubygems/commands/update_command.rb, line 15
15:   def initialize
16:     super 'update',
17:           'Update the named gems (or all installed gems) in the local repository',
18:       :generate_rdoc => true,
19:       :generate_ri   => true,
20:       :force         => false
21: 
22:     add_install_update_options
23: 
24:     OptionParser.accept Gem::Version do |value|
25:       Gem::Version.new value
26: 
27:       value
28:     end
29: 
30:     add_option('--system [VERSION]', Gem::Version,
31:                'Update the RubyGems system software') do |value, options|
32:       value = true unless value
33: 
34:       options[:system] = value
35:     end
36: 
37:     add_local_remote_options
38:     add_platform_option
39:     add_prerelease_option "as update targets"
40:   end

Public Instance methods

[Source]

     # File lib/rubygems/commands/update_command.rb, line 54
 54:   def execute
 55:     @installer = Gem::DependencyInstaller.new options
 56:     @updated   = []
 57: 
 58:     hig = {}
 59: 
 60:     if options[:system] then
 61:       if ENV.include?('REALLY_GEM_UPDATE_SYSTEM')
 62:         update_rubygems
 63:       else
 64:         fail "gem update --system is disabled on Debian, because it will overwrite the content of the rubygems Debian
 65:         package, and might break your Debian system in subtle ways. The Debian-supported way to update rubygems is through apt-
 66:         get, using Debian official repositories.\nIf you really know what you are doing, you can still update rubygems by setti
 67:         ng the REALLY_GEM_UPDATE_SYSTEM environment variable, but please remember that this is completely unsupported by Debian
 68:         ."
 69:       end
 70:       return
 71:     else
 72:       say "Updating installed gems"
 73: 
 74:       hig = {} # highest installed gems
 75: 
 76:       Gem.source_index.each do |name, spec|
 77:         if hig[spec.name].nil? or hig[spec.name].version < spec.version then
 78:           hig[spec.name] = spec
 79:         end
 80:       end
 81:     end
 82: 
 83:     gems_to_update = which_to_update hig, options[:args]
 84: 
 85:     updated = update_gems gems_to_update
 86: 
 87:     if updated.empty? then
 88:       say "Nothing to update"
 89:     else
 90:       say "Gems updated: #{updated.map { |spec| spec.name }.join ', '}"
 91: 
 92:       if options[:generate_ri] then
 93:         updated.each do |gem|
 94:           Gem::DocManager.new(gem, options[:rdoc_args]).generate_ri
 95:         end
 96: 
 97:         Gem::DocManager.update_ri_cache
 98:       end
 99: 
100:       if options[:generate_rdoc] then
101:         updated.each do |gem|
102:           Gem::DocManager.new(gem, options[:rdoc_args]).generate_rdoc
103:         end
104:       end
105:     end
106:   end

[Source]

     # File lib/rubygems/commands/update_command.rb, line 108
108:   def update_gem name, version = Gem::Requirement.default
109:     return if @updated.any? { |spec| spec.name == name }
110:     success = false
111: 
112:     say "Updating #{name}"
113:     begin
114:       @installer.install name, version
115:       success = true
116:     rescue Gem::InstallError => e
117:       alert_error "Error installing #{name}:\n\t#{e.message}"
118:       success = false
119:     end
120: 
121:     @installer.installed_gems.each do |spec|
122:       @updated << spec
123:       say "Successfully installed #{spec.full_name}" if success
124:     end
125:   end

[Source]

     # File lib/rubygems/commands/update_command.rb, line 127
127:   def update_gems gems_to_update
128:     gems_to_update.uniq.sort.each do |name|
129:       update_gem name
130:     end
131: 
132:     @updated
133:   end

Update RubyGems software to the latest version.

[Source]

     # File lib/rubygems/commands/update_command.rb, line 138
138:   def update_rubygems
139:     unless options[:args].empty? then
140:       alert_error "Gem names are not allowed with the --system option"
141:       terminate_interaction 1
142:     end
143: 
144:     options[:user_install] = false
145: 
146:     version = options[:system]
147:     if version == true then
148:       version     = Gem::Version.new     Gem::VERSION
149:       requirement = Gem::Requirement.new ">= #{Gem::VERSION}"
150:     else
151:       version     = Gem::Version.new     version
152:       requirement = Gem::Requirement.new version
153:     end
154: 
155:     rubygems_update         = Gem::Specification.new
156:     rubygems_update.name    = 'rubygems-update'
157:     rubygems_update.version = version
158: 
159:     hig = {
160:       'rubygems-update' => rubygems_update
161:     }
162: 
163:     gems_to_update = which_to_update hig, options[:args]
164: 
165:     if gems_to_update.empty? then
166:       say "Latest version currently installed. Aborting."
167:       terminate_interaction
168:     end
169: 
170:     update_gem gems_to_update.first, requirement
171: 
172:     Gem.source_index.refresh!
173: 
174:     installed_gems = Gem.source_index.find_name 'rubygems-update', requirement
175:     version        = installed_gems.last.version
176: 
177:     args = []
178:     args << '--prefix' << Gem.prefix if Gem.prefix
179:     args << '--no-rdoc' unless options[:generate_rdoc]
180:     args << '--no-ri' unless options[:generate_ri]
181:     args << '--no-format-executable' if options[:no_format_executable]
182: 
183:     update_dir = File.join Gem.dir, 'gems', "rubygems-update-#{version}"
184: 
185:     Dir.chdir update_dir do
186:       say "Installing RubyGems #{version}"
187:       setup_cmd = "#{Gem.ruby} setup.rb #{args.join ' '}"
188: 
189:       # Make sure old rubygems isn't loaded
190:       old = ENV["RUBYOPT"]
191:       ENV.delete("RUBYOPT") if old
192:       installed = system setup_cmd
193:       say "RubyGems system software updated" if installed
194:       ENV["RUBYOPT"] = old if old
195:     end
196:   end

[Source]

     # File lib/rubygems/commands/update_command.rb, line 198
198:   def which_to_update(highest_installed_gems, gem_names)
199:     result = []
200: 
201:     highest_installed_gems.each do |l_name, l_spec|
202:       next if not gem_names.empty? and
203:               gem_names.all? { |name| /#{name}/ !~ l_spec.name }
204: 
205:       dependency = Gem::Dependency.new l_spec.name, "> #{l_spec.version}"
206: 
207:       fetcher = Gem::SpecFetcher.fetcher
208:       spec_tuples = fetcher.find_matching dependency
209: 
210:       matching_gems = spec_tuples.select do |(name, _, platform),|
211:         name == l_name and Gem::Platform.match platform
212:       end
213: 
214:       highest_remote_gem = matching_gems.sort_by do |(_, version),|
215:         version
216:       end.last
217: 
218:       if highest_remote_gem and
219:          l_spec.version < highest_remote_gem.first[1] then
220:         result << l_name
221:       end
222:     end
223: 
224:     result
225:   end

[Validate]