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:       update_rubygems
62:       return
63:     else
64:       say "Updating installed gems"
65: 
66:       hig = {} # highest installed gems
67: 
68:       Gem::Specification.each do |spec|
69:         if hig[spec.name].nil? or hig[spec.name].version < spec.version then
70:           hig[spec.name] = spec
71:         end
72:       end
73:     end
74: 
75:     gems_to_update = which_to_update hig, options[:args].uniq
76: 
77:     updated = update_gems gems_to_update
78: 
79:     if updated.empty? then
80:       say "Nothing to update"
81:     else
82:       say "Gems updated: #{updated.map { |spec| spec.name }.join ', '}"
83: 
84:       if options[:generate_ri] then
85:         updated.each do |gem|
86:           Gem::DocManager.new(gem, options[:rdoc_args]).generate_ri
87:         end
88: 
89:         Gem::DocManager.update_ri_cache
90:       end
91: 
92:       if options[:generate_rdoc] then
93:         updated.each do |gem|
94:           Gem::DocManager.new(gem, options[:rdoc_args]).generate_rdoc
95:         end
96:       end
97:     end
98:   end

[Source]

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

[Source]

     # File lib/rubygems/commands/update_command.rb, line 119
119:   def update_gems gems_to_update
120:     gems_to_update.uniq.sort.each do |(name, version)|
121:       update_gem name, version
122:     end
123: 
124:     @updated
125:   end

Update RubyGems software to the latest version.

[Source]

     # File lib/rubygems/commands/update_command.rb, line 130
130:   def update_rubygems
131:     if ENV.include?('REALLY_GEM_UPDATE_SYSTEM')
132:       say "Updating RubyGems"
133:     else
134:       alert_error "gem update --system is disabled on Debian, because it will overwrite the content of the rubygems Debian package, and might break your Debian system in subtle ways. The Debian-supported way to update rubygems is through apt-get, using Debian official repositories.\nIf you really know what you are doing, you can still update rubygems by setting the REALLY_GEM_UPDATE_SYSTEM environment variable, but please remember that this is completely unsupported by Debian."
135:       terminate_interaction 1
136:     end
137:     unless options[:args].empty? then
138:       alert_error "Gem names are not allowed with the --system option"
139:       terminate_interaction 1
140:     end
141: 
142:     options[:user_install] = false
143: 
144:     # TODO: rename version and other variable name conflicts
145:     # TODO: get rid of all this indirection on name and other BS
146: 
147:     version = options[:system]
148:     if version == true then
149:       version     = Gem::Version.new     Gem::VERSION
150:       requirement = Gem::Requirement.new ">= #{Gem::VERSION}"
151:     else
152:       version     = Gem::Version.new     version
153:       requirement = Gem::Requirement.new version
154:     end
155: 
156:     rubygems_update         = Gem::Specification.new
157:     rubygems_update.name    = 'rubygems-update'
158:     rubygems_update.version = version
159: 
160:     hig = {
161:       'rubygems-update' => rubygems_update
162:     }
163: 
164:     gems_to_update = which_to_update hig, options[:args], :system
165:     name, up_ver   = gems_to_update.first
166:     current_ver    = Gem::Version.new Gem::VERSION
167: 
168:     target = if options[:system] == true then
169:                up_ver
170:              else
171:                version
172:              end
173: 
174:     if current_ver == target then
175:       # if options[:system] != true and version == current_ver then
176:       say "Latest version currently installed. Aborting."
177:       terminate_interaction
178:     end
179: 
180:     update_gem name, target
181: 
182:     installed_gems = Gem::Specification.find_all_by_name 'rubygems-update', requirement
183:     version        = installed_gems.last.version
184: 
185:     args = []
186:     args << '--prefix' << Gem.prefix if Gem.prefix
187:     args << '--no-rdoc' unless options[:generate_rdoc]
188:     args << '--no-ri' unless options[:generate_ri]
189:     args << '--no-format-executable' if options[:no_format_executable]
190: 
191:     update_dir = File.join Gem.dir, 'gems', "rubygems-update-#{version}"
192: 
193:     Dir.chdir update_dir do
194:       say "Installing RubyGems #{version}"
195:       setup_cmd = "#{Gem.ruby} setup.rb #{args.join ' '}"
196: 
197:       # Make sure old rubygems isn't loaded
198:       old = ENV["RUBYOPT"]
199:       ENV.delete("RUBYOPT") if old
200:       installed = system setup_cmd
201:       say "RubyGems system software updated" if installed
202:       ENV["RUBYOPT"] = old if old
203:     end
204:   end

[Source]

     # File lib/rubygems/commands/update_command.rb, line 206
206:   def which_to_update highest_installed_gems, gem_names, system = false
207:     result = []
208: 
209:     highest_installed_gems.each do |l_name, l_spec|
210:       next if not gem_names.empty? and
211:               gem_names.all? { |name| /#{name}/ !~ l_spec.name }
212: 
213:       dependency = Gem::Dependency.new l_spec.name, "> #{l_spec.version}"
214: 
215:       fetcher = Gem::SpecFetcher.fetcher
216:       spec_tuples = fetcher.find_matching dependency
217: 
218:       matching_gems = spec_tuples.select do |(name, _, platform),|
219:         name == l_name and Gem::Platform.match platform
220:       end
221: 
222:       highest_remote_gem = matching_gems.sort_by do |(_, version),|
223:         version
224:       end.last
225: 
226:       highest_remote_gem ||= [[nil, Gem::Version.new(0), nil]] # "null" object
227:       highest_remote_ver = highest_remote_gem.first[1]
228: 
229:       if system or (l_spec.version < highest_remote_ver) then
230:         result << [l_spec.name, [l_spec.version, highest_remote_ver].max]
231:       end
232:     end
233: 
234:     result
235:   end

[Validate]