summaryrefslogtreecommitdiff
path: root/lib/rubygems/commands/update_command.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rubygems/commands/update_command.rb')
-rw-r--r--lib/rubygems/commands/update_command.rb145
1 files changed, 63 insertions, 82 deletions
diff --git a/lib/rubygems/commands/update_command.rb b/lib/rubygems/commands/update_command.rb
index d104488c94..3d6fecaa40 100644
--- a/lib/rubygems/commands/update_command.rb
+++ b/lib/rubygems/commands/update_command.rb
@@ -1,13 +1,14 @@
# frozen_string_literal: true
-require_relative '../command'
-require_relative '../command_manager'
-require_relative '../dependency_installer'
-require_relative '../install_update_options'
-require_relative '../local_remote_options'
-require_relative '../spec_fetcher'
-require_relative '../version_option'
-require_relative '../install_message' # must come before rdoc for messaging
-require_relative '../rdoc'
+
+require_relative "../command"
+require_relative "../command_manager"
+require_relative "../dependency_installer"
+require_relative "../install_update_options"
+require_relative "../local_remote_options"
+require_relative "../spec_fetcher"
+require_relative "../version_option"
+require_relative "../install_message" # must come before rdoc for messaging
+require_relative "../rdoc"
class Gem::Commands::UpdateCommand < Gem::Command
include Gem::InstallUpdateOptions
@@ -20,26 +21,26 @@ class Gem::Commands::UpdateCommand < Gem::Command
def initialize
options = {
- :force => false,
+ force: false,
}
options.merge!(install_update_options)
- super 'update', 'Update installed gems to the latest version', options
+ super "update", "Update installed gems to the latest version", options
add_install_update_options
- OptionParser.accept Gem::Version do |value|
+ Gem::OptionParser.accept Gem::Version do |value|
Gem::Version.new value
value
end
- add_option('--system [VERSION]', Gem::Version,
- 'Update the RubyGems system software') do |value, options|
- value = true unless value
+ add_option("--system [VERSION]", Gem::Version,
+ "Update the RubyGems system software") do |value, opts|
+ value ||= true
- options[:system] = value
+ opts[:system] = value
end
add_local_remote_options
@@ -56,7 +57,7 @@ class Gem::Commands::UpdateCommand < Gem::Command
def defaults_str # :nodoc:
"--no-force --install-dir #{Gem.dir}\n" +
- install_update_defaults_str
+ install_update_defaults_str
end
def description # :nodoc:
@@ -118,15 +119,19 @@ command to remove old versions.
updated = update_gems gems_to_update
- updated_names = updated.map {|spec| spec.name }
+ installed_names = highest_installed_gems.keys
+ updated_names = updated.map(&:name)
not_updated_names = options[:args].uniq - updated_names
+ not_installed_names = not_updated_names - installed_names
+ up_to_date_names = not_updated_names - not_installed_names
if updated.empty?
say "Nothing to update"
else
- say "Gems updated: #{updated_names.join(' ')}"
- say "Gems already up-to-date: #{not_updated_names.join(' ')}" unless not_updated_names.empty?
+ say "Gems updated: #{updated_names.join(" ")}"
end
+ say "Gems already up-to-date: #{up_to_date_names.join(" ")}" unless up_to_date_names.empty?
+ say "Gems not currently installed: #{not_installed_names.join(" ")}" unless not_installed_names.empty?
end
def fetch_remote_gems(spec) # :nodoc:
@@ -151,7 +156,7 @@ command to remove old versions.
Gem::Specification.dirs = Gem.user_dir if options[:user_install]
Gem::Specification.each do |spec|
- if hig[spec.name].nil? or hig[spec.name].version < spec.version
+ if hig[spec.name].nil? || hig[spec.name].version < spec.version
hig[spec.name] = spec
end
end
@@ -162,30 +167,28 @@ command to remove old versions.
def highest_remote_name_tuple(spec) # :nodoc:
spec_tuples = fetch_remote_gems spec
- matching_gems = spec_tuples.select do |g,_|
- g.name == spec.name and g.match_platform?
- end
-
- highest_remote_gem = matching_gems.max
-
- highest_remote_gem ||= [Gem::NameTuple.null]
+ highest_remote_gem = spec_tuples.max
+ return unless highest_remote_gem
highest_remote_gem.first
end
- def install_rubygems(version) # :nodoc:
+ def install_rubygems(spec) # :nodoc:
args = update_rubygems_arguments
+ version = spec.version
- update_dir = File.join Gem.dir, 'gems', "rubygems-update-#{version}"
+ update_dir = File.join spec.base_dir, "gems", "rubygems-update-#{version}"
Dir.chdir update_dir do
say "Installing RubyGems #{version}" unless options[:silent]
installed = preparing_gem_layout_for(version) do
- system Gem.ruby, '--disable-gems', 'setup.rb', *args
+ system Gem.ruby, "--disable-gems", "setup.rb", *args
end
- say "RubyGems system software updated" if installed unless options[:silent]
+ unless options[:silent]
+ say "RubyGems system software updated" if installed
+ end
end
end
@@ -213,32 +216,24 @@ command to remove old versions.
version = options[:system]
update_latest = version == true
- if update_latest
- version = Gem::Version.new Gem::VERSION
- requirement = Gem::Requirement.new ">= #{Gem::VERSION}"
- else
+ unless update_latest
version = Gem::Version.new version
requirement = Gem::Requirement.new version
+
+ return version, requirement
end
+ version = Gem::Version.new Gem::VERSION
+ requirement = Gem::Requirement.new ">= #{Gem::VERSION}"
+
rubygems_update = Gem::Specification.new
- rubygems_update.name = 'rubygems-update'
+ rubygems_update.name = "rubygems-update"
rubygems_update.version = version
- hig = {
- 'rubygems-update' => rubygems_update,
- }
-
- gems_to_update = which_to_update hig, options[:args], :system
- up_ver = gems_to_update.first.version
+ highest_remote_tup = highest_remote_name_tuple(rubygems_update)
+ target = highest_remote_tup ? highest_remote_tup.version : version
- target = if update_latest
- up_ver
- else
- version
- end
-
- return target, requirement
+ [target, requirement]
end
def update_gem(name, version = Gem::Requirement.default)
@@ -249,7 +244,7 @@ command to remove old versions.
@installer = Gem::DependencyInstaller.new update_options
- say "Updating #{name}" unless options[:system] && options[:silent]
+ say "Updating #{name}" unless options[:system]
begin
@installer.install name, Gem::Requirement.new(version)
rescue Gem::InstallError, Gem::DependencyError => e
@@ -286,40 +281,34 @@ command to remove old versions.
check_oldest_rubygems version
- update_gem 'rubygems-update', version
-
- installed_gems = Gem::Specification.find_all_by_name 'rubygems-update', requirement
- version = installed_gems.first.version
+ installed_gems = Gem::Specification.find_all_by_name "rubygems-update", requirement
+ installed_gems = update_gem("rubygems-update", requirement) if installed_gems.empty? || installed_gems.first.version != version
+ return if installed_gems.empty?
- install_rubygems version
+ install_rubygems installed_gems.first
end
def update_rubygems_arguments # :nodoc:
args = []
- args << '--silent' if options[:silent]
- args << '--prefix' << Gem.prefix if Gem.prefix
- args << '--no-document' unless options[:document].include?('rdoc') || options[:document].include?('ri')
- args << '--no-format-executable' if options[:no_format_executable]
- args << '--previous-version' << Gem::VERSION if
- options[:system] == true or
- Gem::Version.new(options[:system]) >= Gem::Version.new(2)
+ args << "--silent" if options[:silent]
+ args << "--prefix" << Gem.prefix if Gem.prefix
+ args << "--no-document" unless options[:document].include?("rdoc") || options[:document].include?("ri")
+ args << "--no-format-executable" if options[:no_format_executable]
+ args << "--previous-version" << Gem::VERSION
args
end
- def which_to_update(highest_installed_gems, gem_names, system = false)
+ def which_to_update(highest_installed_gems, gem_names)
result = []
- highest_installed_gems.each do |l_name, l_spec|
- next if not gem_names.empty? and
+ highest_installed_gems.each do |_l_name, l_spec|
+ next if !gem_names.empty? &&
gem_names.none? {|name| name == l_spec.name }
highest_remote_tup = highest_remote_name_tuple l_spec
- highest_remote_ver = highest_remote_tup.version
- highest_installed_ver = l_spec.version
+ next unless highest_remote_tup
- 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)
- end
+ result << highest_remote_tup
end
result
@@ -335,18 +324,10 @@ command to remove old versions.
#
def oldest_supported_version
@oldest_supported_version ||=
- if Gem.ruby_version > Gem::Version.new("3.0.a")
- Gem::Version.new("3.2.3")
- elsif Gem.ruby_version > Gem::Version.new("2.7.a")
- Gem::Version.new("3.1.2")
- elsif Gem.ruby_version > Gem::Version.new("2.6.a")
- Gem::Version.new("3.0.1")
- elsif Gem.ruby_version > Gem::Version.new("2.5.a")
- Gem::Version.new("2.7.3")
- elsif Gem.ruby_version > Gem::Version.new("2.4.a")
- Gem::Version.new("2.6.8")
+ if Gem.ruby_version > Gem::Version.new("3.1.a")
+ Gem::Version.new("3.3.3")
else
- Gem::Version.new("2.5.2")
+ Gem::Version.new("3.2.3")
end
end
end