2007-11-10 02:48:56 -05:00
|
|
|
require 'rubygems/command'
|
2008-03-31 18:40:06 -04:00
|
|
|
require 'rubygems/command_manager'
|
2012-11-29 01:52:18 -05:00
|
|
|
require 'rubygems/dependency_installer'
|
2007-11-10 02:48:56 -05:00
|
|
|
require 'rubygems/install_update_options'
|
|
|
|
require 'rubygems/local_remote_options'
|
2008-06-17 18:04:18 -04:00
|
|
|
require 'rubygems/spec_fetcher'
|
2007-11-10 02:48:56 -05:00
|
|
|
require 'rubygems/version_option'
|
2012-11-29 01:52:18 -05:00
|
|
|
require 'rubygems/install_message' # must come before rdoc for messaging
|
|
|
|
require 'rubygems/rdoc'
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2007-12-20 03:39:12 -05:00
|
|
|
class Gem::Commands::UpdateCommand < Gem::Command
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2007-12-20 03:39:12 -05:00
|
|
|
include Gem::InstallUpdateOptions
|
|
|
|
include Gem::LocalRemoteOptions
|
|
|
|
include Gem::VersionOption
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2007-12-20 03:39:12 -05:00
|
|
|
def initialize
|
2012-11-29 01:52:18 -05:00
|
|
|
super 'update', 'Update installed gems to the latest version',
|
|
|
|
:document => %w[rdoc ri],
|
|
|
|
:force => false
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2007-12-20 03:39:12 -05:00
|
|
|
add_install_update_options
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
OptionParser.accept Gem::Version do |value|
|
|
|
|
Gem::Version.new value
|
|
|
|
|
|
|
|
value
|
|
|
|
end
|
|
|
|
|
|
|
|
add_option('--system [VERSION]', Gem::Version,
|
2007-12-20 03:39:12 -05:00
|
|
|
'Update the RubyGems system software') do |value, options|
|
2011-03-01 04:41:32 -05:00
|
|
|
value = true unless value
|
|
|
|
|
2007-12-20 03:39:12 -05:00
|
|
|
options[:system] = value
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2007-12-20 03:39:12 -05:00
|
|
|
add_local_remote_options
|
|
|
|
add_platform_option
|
2010-02-21 21:52:35 -05:00
|
|
|
add_prerelease_option "as update targets"
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
@updated = []
|
|
|
|
@installer = Gem::DependencyInstaller.new options
|
2007-12-20 03:39:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def arguments # :nodoc:
|
|
|
|
"GEMNAME name of gem to update"
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2007-12-20 03:39:12 -05:00
|
|
|
def defaults_str # :nodoc:
|
2012-11-29 01:52:18 -05:00
|
|
|
"--document --no-force --install-dir #{Gem.dir}"
|
2007-12-20 03:39:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def usage # :nodoc:
|
|
|
|
"#{program_name} GEMNAME [GEMNAME ...]"
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2008-09-25 06:13:50 -04:00
|
|
|
hig = {}
|
|
|
|
|
2007-12-20 03:39:12 -05:00
|
|
|
if options[:system] then
|
2011-03-01 04:41:32 -05:00
|
|
|
update_rubygems
|
2011-01-18 19:08:49 -05:00
|
|
|
return
|
2007-12-20 03:39:12 -05:00
|
|
|
else
|
2008-03-31 18:40:06 -04:00
|
|
|
say "Updating installed gems"
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-09-25 06:13:50 -04:00
|
|
|
hig = {} # highest installed gems
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
Gem::Specification.each do |spec|
|
2008-09-25 06:13:50 -04:00
|
|
|
if hig[spec.name].nil? or hig[spec.name].version < spec.version then
|
|
|
|
hig[spec.name] = spec
|
|
|
|
end
|
2007-12-20 03:39:12 -05:00
|
|
|
end
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
gems_to_update = which_to_update hig, options[:args].uniq
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
updated = update_gems gems_to_update
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
if updated.empty? then
|
|
|
|
say "Nothing to update"
|
2007-12-20 03:39:12 -05:00
|
|
|
else
|
2012-11-29 01:52:18 -05:00
|
|
|
say "Gems updated: #{updated.map { |spec| spec.name }.join ' '}"
|
2007-12-20 03:39:12 -05:00
|
|
|
end
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
def update_gem name, version = Gem::Requirement.default
|
|
|
|
return if @updated.any? { |spec| spec.name == name }
|
|
|
|
success = false
|
|
|
|
|
|
|
|
say "Updating #{name}"
|
|
|
|
begin
|
|
|
|
@installer.install name, version
|
|
|
|
success = true
|
|
|
|
rescue Gem::InstallError => e
|
|
|
|
alert_error "Error installing #{name}:\n\t#{e.message}"
|
|
|
|
success = false
|
|
|
|
end
|
|
|
|
|
|
|
|
@installer.installed_gems.each do |spec|
|
|
|
|
@updated << spec
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_gems gems_to_update
|
2011-05-31 23:45:05 -04:00
|
|
|
gems_to_update.uniq.sort.each do |(name, version)|
|
|
|
|
update_gem name, version
|
2011-03-01 04:41:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@updated
|
|
|
|
end
|
|
|
|
|
2008-09-25 06:13:50 -04:00
|
|
|
##
|
2011-03-01 04:41:32 -05:00
|
|
|
# Update RubyGems software to the latest version.
|
|
|
|
|
|
|
|
def update_rubygems
|
|
|
|
unless options[:args].empty? then
|
|
|
|
alert_error "Gem names are not allowed with the --system option"
|
|
|
|
terminate_interaction 1
|
|
|
|
end
|
|
|
|
|
|
|
|
options[:user_install] = false
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
# TODO: rename version and other variable name conflicts
|
|
|
|
# TODO: get rid of all this indirection on name and other BS
|
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
version = options[:system]
|
|
|
|
if version == true then
|
|
|
|
version = Gem::Version.new Gem::VERSION
|
|
|
|
requirement = Gem::Requirement.new ">= #{Gem::VERSION}"
|
|
|
|
else
|
|
|
|
version = Gem::Version.new version
|
|
|
|
requirement = Gem::Requirement.new version
|
|
|
|
end
|
|
|
|
|
|
|
|
rubygems_update = Gem::Specification.new
|
|
|
|
rubygems_update.name = 'rubygems-update'
|
|
|
|
rubygems_update.version = version
|
|
|
|
|
|
|
|
hig = {
|
|
|
|
'rubygems-update' => rubygems_update
|
|
|
|
}
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
gems_to_update = which_to_update hig, options[:args], :system
|
|
|
|
name, up_ver = gems_to_update.first
|
2012-12-22 19:35:09 -05:00
|
|
|
current_ver = Gem.rubygems_version
|
2011-05-31 23:45:05 -04:00
|
|
|
|
|
|
|
target = if options[:system] == true then
|
|
|
|
up_ver
|
|
|
|
else
|
|
|
|
version
|
|
|
|
end
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
if current_ver == target then
|
|
|
|
# if options[:system] != true and version == current_ver then
|
2011-03-01 04:41:32 -05:00
|
|
|
say "Latest version currently installed. Aborting."
|
|
|
|
terminate_interaction
|
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
update_gem name, target
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
installed_gems = Gem::Specification.find_all_by_name 'rubygems-update', requirement
|
2011-03-01 04:41:32 -05:00
|
|
|
version = installed_gems.last.version
|
2008-09-25 06:13:50 -04:00
|
|
|
|
2007-12-20 03:39:12 -05:00
|
|
|
args = []
|
2011-03-01 04:41:32 -05:00
|
|
|
args << '--prefix' << Gem.prefix if Gem.prefix
|
2012-11-29 01:52:18 -05:00
|
|
|
# TODO use --document for >= 1.9 , --no-rdoc --no-ri < 1.9
|
|
|
|
args << '--no-rdoc' unless options[:document].include? 'rdoc'
|
|
|
|
args << '--no-ri' unless options[:document].include? 'ri'
|
2008-04-11 16:57:02 -04:00
|
|
|
args << '--no-format-executable' if options[:no_format_executable]
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2007-12-20 03:39:12 -05:00
|
|
|
update_dir = File.join Gem.dir, 'gems', "rubygems-update-#{version}"
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2007-12-20 03:39:12 -05:00
|
|
|
Dir.chdir update_dir do
|
|
|
|
say "Installing RubyGems #{version}"
|
|
|
|
setup_cmd = "#{Gem.ruby} setup.rb #{args.join ' '}"
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2007-12-20 03:39:12 -05:00
|
|
|
# Make sure old rubygems isn't loaded
|
2007-12-27 08:33:04 -05:00
|
|
|
old = ENV["RUBYOPT"]
|
2011-03-01 04:41:32 -05:00
|
|
|
ENV.delete("RUBYOPT") if old
|
|
|
|
installed = system setup_cmd
|
|
|
|
say "RubyGems system software updated" if installed
|
2007-12-27 08:33:04 -05:00
|
|
|
ENV["RUBYOPT"] = old if old
|
2007-12-20 03:39:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
def which_to_update highest_installed_gems, gem_names, system = false
|
2007-12-20 03:39:12 -05:00
|
|
|
result = []
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2007-12-20 03:39:12 -05:00
|
|
|
highest_installed_gems.each do |l_name, l_spec|
|
2008-06-17 18:04:18 -04:00
|
|
|
next if not gem_names.empty? and
|
|
|
|
gem_names.all? { |name| /#{name}/ !~ l_spec.name }
|
|
|
|
|
|
|
|
dependency = Gem::Dependency.new l_spec.name, "> #{l_spec.version}"
|
2012-11-29 01:52:18 -05:00
|
|
|
dependency.prerelease = options[:prerelease]
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
fetcher = Gem::SpecFetcher.fetcher
|
2007-12-20 03:39:12 -05:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
spec_tuples, _ = fetcher.search_for_dependency dependency
|
|
|
|
|
|
|
|
matching_gems = spec_tuples.select do |g,_|
|
|
|
|
g.name == l_name and g.match_platform?
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
highest_remote_gem = matching_gems.sort_by { |g,_| g.version }.last
|
2007-12-20 03:39:12 -05:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
highest_remote_gem ||= [Gem::NameTuple.null]
|
|
|
|
highest_remote_ver = highest_remote_gem.first.version
|
2011-05-31 23:45:05 -04:00
|
|
|
|
|
|
|
if system or (l_spec.version < highest_remote_ver) then
|
|
|
|
result << [l_spec.name, [l_spec.version, highest_remote_ver].max]
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
2007-12-20 03:39:12 -05:00
|
|
|
|
|
|
|
result
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2007-12-20 03:39:12 -05:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2007-12-20 03:39:12 -05:00
|
|
|
|