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

[Source]

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

[Validate]