2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
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:
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
attr_reader :updated # :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:
|
2015-09-08 18:46:43 -04:00
|
|
|
"GEMNAME name of gem to update"
|
2007-12-20 03:39:12 -05:00
|
|
|
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-09-14 04:59:02 -04:00
|
|
|
def description # :nodoc:
|
|
|
|
<<-EOF
|
|
|
|
The update command will update your gems to the latest version.
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
The update command does not remove the previous version. Use the cleanup
|
2013-09-14 04:59:02 -04:00
|
|
|
command to remove old versions.
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
|
2007-12-20 03:39:12 -05:00
|
|
|
def usage # :nodoc:
|
2015-09-08 18:46:43 -04:00
|
|
|
"#{program_name} GEMNAME [GEMNAME ...]"
|
2007-12-20 03:39:12 -05:00
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def check_latest_rubygems(version) # :nodoc:
|
|
|
|
if Gem.rubygems_version == version
|
2017-10-07 21:32:18 -04:00
|
|
|
say "Latest version already installed. Done."
|
2013-09-14 04:59:02 -04:00
|
|
|
terminate_interaction
|
|
|
|
end
|
|
|
|
|
|
|
|
options[:user_install] = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_update_arguments # :nodoc:
|
2018-11-21 05:20:47 -05:00
|
|
|
unless options[:args].empty?
|
2013-09-14 04:59:02 -04:00
|
|
|
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
|
2018-11-21 05:20:47 -05:00
|
|
|
if options[:system]
|
2011-03-01 04:41:32 -05:00
|
|
|
update_rubygems
|
2011-01-18 19:08:49 -05:00
|
|
|
return
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-09-14 04:59:02 -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
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
if options[:explain]
|
|
|
|
say "Gems to update:"
|
|
|
|
|
2019-03-04 22:32:58 -05:00
|
|
|
gems_to_update.each do |name_tuple|
|
|
|
|
say " #{name_tuple.full_name}"
|
2018-05-30 09:01:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
say "Updating installed gems"
|
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
updated = update_gems gems_to_update
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
updated_names = updated.map { |spec| spec.name }
|
|
|
|
not_updated_names = options[:args].uniq - updated_names
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
if updated.empty?
|
2011-01-18 19:08:49 -05:00
|
|
|
say "Nothing to update"
|
2007-12-20 03:39:12 -05:00
|
|
|
else
|
2014-09-13 23:30:02 -04:00
|
|
|
say "Gems updated: #{updated_names.join(' ')}"
|
|
|
|
say "Gems already up-to-date: #{not_updated_names.join(' ')}" unless not_updated_names.empty?
|
2007-12-20 03:39:12 -05:00
|
|
|
end
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def fetch_remote_gems(spec) # :nodoc:
|
2013-09-14 04:59:02 -04:00
|
|
|
dependency = Gem::Dependency.new spec.name, "> #{spec.version}"
|
|
|
|
dependency.prerelease = options[:prerelease]
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
fetcher = Gem::SpecFetcher.fetcher
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2013-10-15 20:14:16 -04:00
|
|
|
spec_tuples, errors = fetcher.search_for_dependency dependency
|
|
|
|
|
2013-11-10 12:51:40 -05:00
|
|
|
error = errors.find { |e| e.respond_to? :exception }
|
2013-10-18 18:14:56 -04:00
|
|
|
|
|
|
|
raise error if error
|
2013-07-22 18:46:50 -04:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
spec_tuples
|
|
|
|
end
|
2013-07-22 18:46:50 -04:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def highest_installed_gems # :nodoc:
|
|
|
|
hig = {} # highest installed gems
|
|
|
|
|
|
|
|
Gem::Specification.each do |spec|
|
2018-11-21 05:20:47 -05:00
|
|
|
if hig[spec.name].nil? or hig[spec.name].version < spec.version
|
2013-09-14 04:59:02 -04:00
|
|
|
hig[spec.name] = spec
|
|
|
|
end
|
2013-09-13 15:58:57 -04:00
|
|
|
end
|
2013-09-14 04:59:02 -04:00
|
|
|
|
|
|
|
hig
|
2011-03-01 04:41:32 -05:00
|
|
|
end
|
|
|
|
|
2019-03-04 22:32:58 -05:00
|
|
|
def highest_remote_name_tuple(spec) # :nodoc:
|
2013-09-14 04:59:02 -04:00
|
|
|
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
|
|
|
|
|
2019-03-04 22:32:58 -05:00
|
|
|
highest_remote_gem = matching_gems.max
|
2013-09-14 04:59:02 -04:00
|
|
|
|
|
|
|
highest_remote_gem ||= [Gem::NameTuple.null]
|
|
|
|
|
2019-03-04 22:32:58 -05:00
|
|
|
highest_remote_gem.first
|
2011-03-01 04:41:32 -05:00
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def install_rubygems(version) # :nodoc:
|
2013-09-14 04:59:02 -04:00
|
|
|
args = update_rubygems_arguments
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
update_dir = File.join Gem.dir, 'gems', "rubygems-update-#{version}"
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
Dir.chdir update_dir do
|
|
|
|
say "Installing RubyGems #{version}"
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2018-10-21 20:27:02 -04:00
|
|
|
installed = system Gem.ruby, '--disable-gems', 'setup.rb', *args
|
2013-09-14 04:59:02 -04:00
|
|
|
say "RubyGems system software updated" if installed
|
|
|
|
end
|
|
|
|
end
|
2011-05-31 23:45:05 -04:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def rubygems_target_version
|
2011-03-01 04:41:32 -05:00
|
|
|
version = options[:system]
|
2013-09-14 04:59:02 -04:00
|
|
|
update_latest = version == true
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
if update_latest
|
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
|
2019-03-04 22:32:58 -05:00
|
|
|
up_ver = gems_to_update.first.version
|
2011-05-31 23:45:05 -04:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
target = if update_latest
|
2011-05-31 23:45:05 -04:00
|
|
|
up_ver
|
|
|
|
else
|
|
|
|
version
|
|
|
|
end
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
return target, requirement
|
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def update_gem(name, version = Gem::Requirement.default)
|
2013-09-14 04:59:02 -04:00
|
|
|
return if @updated.any? { |spec| spec.name == name }
|
|
|
|
|
2014-10-01 04:30:21 -04:00
|
|
|
update_options = options.dup
|
|
|
|
update_options[:prerelease] = version.prerelease?
|
|
|
|
|
|
|
|
@installer = Gem::DependencyInstaller.new update_options
|
2013-09-14 04:59:02 -04:00
|
|
|
|
|
|
|
say "Updating #{name}"
|
|
|
|
begin
|
|
|
|
@installer.install name, Gem::Requirement.new(version)
|
2014-09-13 23:30:02 -04:00
|
|
|
rescue Gem::InstallError, Gem::DependencyError => e
|
2013-09-14 04:59:02 -04:00
|
|
|
alert_error "Error installing #{name}:\n\t#{e.message}"
|
2013-07-22 18:46:50 -04:00
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
@installer.installed_gems.each do |spec|
|
|
|
|
@updated << spec
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def update_gems(gems_to_update)
|
2019-03-04 22:32:58 -05:00
|
|
|
gems_to_update.uniq.sort.each do |name_tuple|
|
|
|
|
update_gem name_tuple.name, name_tuple.version
|
2013-09-14 04:59:02 -04:00
|
|
|
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
|
2016-06-24 00:13:11 -04:00
|
|
|
version = installed_gems.first.version
|
2008-09-25 06:13:50 -04:00
|
|
|
|
2013-09-14 04:59:02 -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-09-18 17:29:41 -04:00
|
|
|
args << '--previous-version' << Gem::VERSION if
|
|
|
|
options[:system] == true or
|
|
|
|
Gem::Version.new(options[:system]) >= Gem::Version.new(2)
|
2013-09-14 04:59:02 -04:00
|
|
|
args
|
2007-12-20 03:39:12 -05:00
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05: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
|
2014-09-13 23:30:02 -04:00
|
|
|
gem_names.none? { |name| name == l_spec.name }
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2019-03-04 22:32:58 -05:00
|
|
|
highest_remote_tup = highest_remote_name_tuple l_spec
|
|
|
|
highest_remote_ver = highest_remote_tup.version
|
|
|
|
highest_installed_ver = l_spec.version
|
2011-05-31 23:45:05 -04:00
|
|
|
|
2019-03-04 22:32:58 -05:00
|
|
|
if system or (highest_installed_ver < highest_remote_ver)
|
|
|
|
result << Gem::NameTuple.new(l_spec.name, [highest_installed_ver, highest_remote_ver].max, highest_remote_tup.platform)
|
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
|