summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJosef Šimánek <josef.simanek@gmail.com>2022-09-11 05:40:55 +0200
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2022-11-11 17:24:08 +0900
commitc7d043065c058f20ce30c61bb3ce127cb15cc0a8 (patch)
treedc36ca3f11e09451564500fa241fe3fdc521fde7 /lib
parentceeefb5870c144ddc069b2c9b8a19dbd4947a947 (diff)
[rubygems/rubygems] Add 'call for update' to RubyGems install command.
https://github.com/rubygems/rubygems/commit/05811f8248
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/6715
Diffstat (limited to 'lib')
-rw-r--r--lib/rubygems/commands/install_command.rb4
-rw-r--r--lib/rubygems/config_file.rb12
-rw-r--r--lib/rubygems/update_suggestion.rb70
3 files changed, 86 insertions, 0 deletions
diff --git a/lib/rubygems/commands/install_command.rb b/lib/rubygems/commands/install_command.rb
index 071687c63f..c04c01f258 100644
--- a/lib/rubygems/commands/install_command.rb
+++ b/lib/rubygems/commands/install_command.rb
@@ -5,6 +5,7 @@ require_relative "../dependency_installer"
require_relative "../local_remote_options"
require_relative "../validator"
require_relative "../version_option"
+require_relative "../update_suggestion"
##
# Gem installer command line tool
@@ -17,6 +18,7 @@ class Gem::Commands::InstallCommand < Gem::Command
include Gem::VersionOption
include Gem::LocalRemoteOptions
include Gem::InstallUpdateOptions
+ include Gem::UpdateSuggestion
def initialize
defaults = Gem::DependencyInstaller::DEFAULT_OPTIONS.merge({
@@ -168,6 +170,8 @@ You can use `i` command instead of `install`.
show_installed
+ say update_suggestion if eglible_for_update?
+
terminate_interaction exit_code
end
diff --git a/lib/rubygems/config_file.rb b/lib/rubygems/config_file.rb
index c53e209ae8..b18f4115cc 100644
--- a/lib/rubygems/config_file.rb
+++ b/lib/rubygems/config_file.rb
@@ -371,6 +371,18 @@ if you believe they were disclosed to a third party.
@backtrace || $DEBUG
end
+ # Check config file is writable. Creates empty file if not present to ensure we can write to it.
+ def config_file_writable?
+ if File.exist?(config_file_name)
+ File.writable?(config_file_name)
+ else
+ require "fileutils"
+ FileUtils.mkdir_p File.dirname(config_file_name)
+ File.open(config_file_name, "w") {}
+ true
+ end
+ end
+
# The name of the configuration file.
def config_file_name
@config_file_name || Gem.config_file
diff --git a/lib/rubygems/update_suggestion.rb b/lib/rubygems/update_suggestion.rb
new file mode 100644
index 0000000000..71c44af3af
--- /dev/null
+++ b/lib/rubygems/update_suggestion.rb
@@ -0,0 +1,70 @@
+# frozen_string_literal: true
+
+##
+# Mixin methods for Gem::Command to promote available RubyGems update
+
+module Gem::UpdateSuggestion
+ # list taken from https://github.com/watson/ci-info/blob/7a3c30d/index.js#L56-L66
+ CI_ENV_VARS = [
+ "CI", # Travis CI, CircleCI, Cirrus CI, Gitlab CI, Appveyor, CodeShip, dsari
+ "CONTINUOUS_INTEGRATION", # Travis CI, Cirrus CI
+ "BUILD_NUMBER", # Jenkins, TeamCity
+ "CI_APP_ID", "CI_BUILD_ID", "CI_BUILD_NUMBER", # Applfow
+ "RUN_ID" # TaskCluster, dsari
+ ].freeze
+
+ ONE_WEEK = 7 * 24 * 60 * 60
+
+ ##
+ # Message to promote available RubyGems update with related gem update command.
+
+ def update_suggestion
+ <<-MESSAGE
+
+A new release of RubyGems is available: #{Gem.rubygems_version} → #{Gem.latest_rubygems_version}!
+Run `gem update --system #{Gem.latest_rubygems_version}` to update your installation.
+
+ MESSAGE
+ end
+
+ ##
+ # Determines if current environment is eglible for update suggestion.
+
+ def eglible_for_update?
+ # explicit opt-out
+ return false if Gem.configuration[:prevent_update_suggestion]
+ return false if ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"]
+
+ # focus only on human usage of final RubyGems releases
+ return false unless Gem.ui.tty?
+ return false if Gem.rubygems_version.prerelease?
+ return false if Gem.disable_system_update_message
+ return false if ci?
+
+ # check makes sense only when we can store of last try
+ # otherwise we will not be able to prevent annoying update message
+ # on each command call
+ return unless Gem.configuration.config_file_writable?
+
+ # load time of last check, ensure the difference is enough to repeat the suggestion
+ check_time = Time.now.to_i
+ last_update_check = Gem.configuration[:last_update_check] || 0
+ return false if (check_time - last_update_check) < ONE_WEEK
+
+ # compare current and latest version, this is the part where
+ # latest rubygems spec is fetched from remote
+ (Gem.rubygems_version < Gem.latest_rubygems_version).tap do |eglible|
+ if eglible
+ # store the time of last successful check into config file
+ Gem.configuration[:last_update_check] = check_time
+ Gem.configuration.write
+ end
+ end
+ rescue # don't block install command on any problem
+ false
+ end
+
+ def ci?
+ CI_ENV_VARS.any? {|var| ENV.include?(var) }
+ end
+end