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
|
|
|
|
2013-02-19 19:34:58 -05:00
|
|
|
attr_reader :installer # :nodoc:
|
|
|
|
|
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 = []
|
2013-02-19 19:34:58 -05:00
|
|
|
@installer = nil
|
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
|
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
def description # :nodoc:
|
|
|
|
<<-EOF
|
|
|
|
The update command will update your gems to the latest version.
|
|
|
|
|
|
|
|
The update comamnd does not remove the previous version. Use the cleanup
|
|
|
|
command to remove old versions.
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
|
2007-12-20 03:39:12 -05:00
|
|
|
def usage # :nodoc:
|
|
|
|
"#{program_name} GEMNAME [GEMNAME ...]"
|
|
|
|
end
|
|
|
|
|
2013-07-22 18:46:50 -04:00
|
|
|
def check_latest_rubygems version # :nodoc:
|
|
|
|
if Gem.rubygems_version == version then
|
|
|
|
say "Latest version currently installed. Aborting."
|
|
|
|
terminate_interaction
|
|
|
|
end
|
|
|
|
|
|
|
|
options[:user_install] = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_update_arguments # :nodoc:
|
|
|
|
unless options[:args].empty? then
|
|
|
|
alert_error "Gem names are not allowed with the --system option"
|
|
|
|
terminate_interaction 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-12-20 03:39:12 -05:00
|
|
|
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
|
2013-07-22 18:46:50 -04:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-07-22 18:46:50 -04:00
|
|
|
say "Updating installed gems"
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-07-22 18:46:50 -04:00
|
|
|
hig = highest_installed_gems
|
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
|
|
|
|
2013-07-22 18:46:50 -04:00
|
|
|
def fetch_remote_gems spec # :nodoc:
|
|
|
|
dependency = Gem::Dependency.new spec.name, "> #{spec.version}"
|
|
|
|
dependency.prerelease = options[:prerelease]
|
2013-02-19 19:34:58 -05:00
|
|
|
|
2013-07-22 18:46:50 -04:00
|
|
|
fetcher = Gem::SpecFetcher.fetcher
|
2013-02-19 19:34:58 -05:00
|
|
|
|
2013-07-22 18:46:50 -04:00
|
|
|
spec_tuples, _ = fetcher.search_for_dependency dependency
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2013-07-22 18:46:50 -04:00
|
|
|
spec_tuples
|
|
|
|
end
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2013-07-22 18:46:50 -04:00
|
|
|
def highest_installed_gems # :nodoc:
|
|
|
|
hig = {} # highest installed gems
|
|
|
|
|
|
|
|
Gem::Specification.each do |spec|
|
|
|
|
if hig[spec.name].nil? or hig[spec.name].version < spec.version then
|
|
|
|
hig[spec.name] = spec
|
|
|
|
end
|
2011-03-01 04:41:32 -05:00
|
|
|
end
|
2013-07-22 18:46:50 -04:00
|
|
|
|
|
|
|
hig
|
2011-03-01 04:41:32 -05:00
|
|
|
end
|
|
|
|
|
2013-07-22 18:46:50 -04:00
|
|
|
def highest_remote_version spec # :nodoc:
|
|
|
|
spec_tuples = fetch_remote_gems spec
|
|
|
|
|
|
|
|
matching_gems = spec_tuples.select do |g,_|
|
|
|
|
g.name == spec.name and g.match_platform?
|
2011-03-01 04:41:32 -05:00
|
|
|
end
|
|
|
|
|
2013-07-22 18:46:50 -04:00
|
|
|
highest_remote_gem = matching_gems.sort_by { |g,_| g.version }.last
|
|
|
|
|
|
|
|
highest_remote_gem ||= [Gem::NameTuple.null]
|
|
|
|
|
|
|
|
highest_remote_gem.first.version
|
2011-03-01 04:41:32 -05:00
|
|
|
end
|
|
|
|
|
2013-07-22 18:46:50 -04:00
|
|
|
def install_rubygems version # :nodoc:
|
|
|
|
args = update_rubygems_arguments
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2013-07-22 18:46:50 -04:00
|
|
|
update_dir = File.join Gem.dir, 'gems', "rubygems-update-#{version}"
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2013-07-22 18:46:50 -04:00
|
|
|
Dir.chdir update_dir do
|
|
|
|
say "Installing RubyGems #{version}"
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2013-07-22 18:46:50 -04:00
|
|
|
# Make sure old rubygems isn't loaded
|
|
|
|
old = ENV["RUBYOPT"]
|
|
|
|
ENV.delete("RUBYOPT") if old
|
|
|
|
installed = system Gem.ruby, 'setup.rb', *args
|
|
|
|
say "RubyGems system software updated" if installed
|
|
|
|
ENV["RUBYOPT"] = old if old
|
|
|
|
end
|
|
|
|
end
|
2011-05-31 23:45:05 -04:00
|
|
|
|
2013-07-22 18:46:50 -04:00
|
|
|
def rubygems_target_version
|
2011-03-01 04:41:32 -05:00
|
|
|
version = options[:system]
|
2013-07-22 18:46:50 -04:00
|
|
|
update_latest = version == true
|
|
|
|
|
|
|
|
if update_latest then
|
2011-03-01 04:41:32 -05:00
|
|
|
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
|
2013-07-22 18:46:50 -04:00
|
|
|
_, up_ver = gems_to_update.first
|
2011-05-31 23:45:05 -04:00
|
|
|
|
2013-07-22 18:46:50 -04:00
|
|
|
target = if update_latest then
|
2011-05-31 23:45:05 -04:00
|
|
|
up_ver
|
|
|
|
else
|
|
|
|
version
|
|
|
|
end
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2013-07-22 18:46:50 -04:00
|
|
|
return target, requirement
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_gem name, version = Gem::Requirement.default
|
|
|
|
return if @updated.any? { |spec| spec.name == name }
|
|
|
|
|
|
|
|
@installer ||= Gem::DependencyInstaller.new options
|
|
|
|
|
|
|
|
success = false
|
|
|
|
|
|
|
|
say "Updating #{name}"
|
|
|
|
begin
|
|
|
|
@installer.install name, Gem::Requirement.new(version)
|
|
|
|
success = true
|
|
|
|
rescue Gem::InstallError => e
|
|
|
|
alert_error "Error installing #{name}:\n\t#{e.message}"
|
|
|
|
success = false
|
2011-03-01 04:41:32 -05:00
|
|
|
end
|
|
|
|
|
2013-07-22 18:46:50 -04:00
|
|
|
@installer.installed_gems.each do |spec|
|
|
|
|
@updated << spec
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_gems gems_to_update
|
|
|
|
gems_to_update.uniq.sort.each do |(name, version)|
|
|
|
|
update_gem name, version
|
|
|
|
end
|
|
|
|
|
|
|
|
@updated
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Update RubyGems software to the latest version.
|
|
|
|
|
|
|
|
def update_rubygems
|
|
|
|
check_update_arguments
|
|
|
|
|
|
|
|
version, requirement = rubygems_target_version
|
|
|
|
|
|
|
|
check_latest_rubygems version
|
|
|
|
|
|
|
|
update_gem 'rubygems-update', version
|
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
|
|
|
|
2013-07-22 18:46:50 -04:00
|
|
|
install_rubygems version
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_rubygems_arguments # :nodoc:
|
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]
|
2013-07-22 18:46:50 -04:00
|
|
|
args
|
2007-12-20 03:39:12 -05:00
|
|
|
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 }
|
|
|
|
|
2013-07-22 18:46:50 -04:00
|
|
|
highest_remote_ver = highest_remote_version l_spec
|
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
|
|
|
|