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
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2020-04-28 10:28:52 -04:00
|
|
|
def check_oldest_rubygems(version) # :nodoc:
|
|
|
|
if oldest_supported_version > version
|
|
|
|
alert_error "rubygems #{version} is not supported. The oldest supported version is #{oldest_supported_version}"
|
|
|
|
terminate_interaction 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
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
|
|
|
|
2020-01-31 21:14:04 -05:00
|
|
|
gems_to_update = which_to_update(
|
|
|
|
highest_installed_gems,
|
|
|
|
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
|
|
|
|
2020-06-10 13:46:05 -04:00
|
|
|
updated_names = updated.map {|spec| spec.name }
|
2014-09-13 23:30:02 -04:00
|
|
|
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
|
|
|
|
|
2020-06-10 13:46:05 -04: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
|
|
|
|
|
2020-01-31 21:14:04 -05:00
|
|
|
# Get only gem specifications installed as --user-install
|
|
|
|
Gem::Specification.dirs = Gem.user_dir if options[:user_install]
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
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
|
2020-07-30 17:24:05 -04:00
|
|
|
say "Installing RubyGems #{version}" unless options[:silent]
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2020-01-31 21:14:04 -05:00
|
|
|
installed = preparing_gem_layout_for(version) do
|
|
|
|
system Gem.ruby, '--disable-gems', 'setup.rb', *args
|
|
|
|
end
|
|
|
|
|
2020-07-30 17:24:05 -04:00
|
|
|
say "RubyGems system software updated" if installed unless options[:silent]
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
|
|
|
end
|
2011-05-31 23:45:05 -04:00
|
|
|
|
2020-01-31 21:14:04 -05:00
|
|
|
def preparing_gem_layout_for(version)
|
|
|
|
if Gem::Version.new(version) >= Gem::Version.new("3.2.a")
|
|
|
|
yield
|
|
|
|
else
|
|
|
|
require "tmpdir"
|
|
|
|
tmpdir = Dir.mktmpdir
|
2020-03-24 02:39:24 -04:00
|
|
|
FileUtils.mv Gem.plugindir, tmpdir
|
2020-01-31 21:14:04 -05:00
|
|
|
|
|
|
|
status = yield
|
|
|
|
|
|
|
|
if status
|
|
|
|
FileUtils.rm_rf tmpdir
|
|
|
|
else
|
2020-03-24 02:39:24 -04:00
|
|
|
FileUtils.mv File.join(tmpdir, "plugins"), Gem.plugindir
|
2020-01-31 21:14:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
status
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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 = {
|
2020-04-28 10:28:52 -04:00
|
|
|
'rubygems-update' => rubygems_update,
|
2011-03-01 04:41:32 -05:00
|
|
|
}
|
|
|
|
|
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)
|
2020-06-10 13:46:05 -04:00
|
|
|
return if @updated.any? {|spec| spec.name == name }
|
2013-09-14 04:59:02 -04:00
|
|
|
|
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
|
|
|
|
2020-07-30 18:15:15 -04:00
|
|
|
say "Updating #{name}" unless options[:system] && options[:silent]
|
2013-09-14 04:59:02 -04:00
|
|
|
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
|
2020-06-13 18:26:23 -04:00
|
|
|
if Gem.disable_system_update_message
|
|
|
|
alert_error Gem.disable_system_update_message
|
2020-06-14 18:28:25 -04:00
|
|
|
terminate_interaction 1
|
2020-06-13 18:26:23 -04:00
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
check_update_arguments
|
|
|
|
|
|
|
|
version, requirement = rubygems_target_version
|
|
|
|
|
|
|
|
check_latest_rubygems version
|
|
|
|
|
2020-04-28 10:28:52 -04:00
|
|
|
check_oldest_rubygems version
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
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 = []
|
2020-07-04 19:19:27 -04:00
|
|
|
args << '--silent' if options[:silent]
|
2011-03-01 04:41:32 -05:00
|
|
|
args << '--prefix' << Gem.prefix if Gem.prefix
|
2019-06-01 05:45:11 -04:00
|
|
|
args << '--no-document' unless options[:document].include?('rdoc') || 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
|
2020-06-10 13:46:05 -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
|
2020-04-28 10:28:52 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def oldest_supported_version
|
2020-08-06 20:02:40 -04:00
|
|
|
# for Ruby 2.3
|
2020-04-28 10:28:52 -04:00
|
|
|
@oldest_supported_version ||= Gem::Version.new("2.5.2")
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|