Class: U3dCore::UpdateChecker
- Inherits:
-
Object
- Object
- U3dCore::UpdateChecker
- Defined in:
- lib/u3d_core/update_checker/update_checker.rb
Overview
Verifies the user runs the latest version of this gem
Class Attribute Summary collapse
-
.start_time ⇒ Object
readonly
Returns the value of attribute start_time.
Class Method Summary collapse
-
.ensure_rubygems_source ⇒ Object
Check if RubyGems is set as a gem source on some machines that might not be the case and then users can’t find the update when running the specified command.
- .fetch_latest(gem_name) ⇒ Object
- .generate_fetch_url(gem_name) ⇒ Object
- .server_results ⇒ Object
-
.show_update_message(gem_name, current_version) ⇒ Object
Show a message to the user to update to a new version of u3d Use this method, as this will detect the current Ruby environment and show an appropriate message to the user.
- .show_update_status(gem_name, current_version) ⇒ Object
- .start_looking_for_update(gem_name) ⇒ Object
- .update_available?(gem_name, current_version) ⇒ Boolean
-
.update_command(gem_name) ⇒ Object
The command that the user should use to update the gem.
-
.wait_for_results ⇒ Object
wait a abit for results when commands run real quick.
Class Attribute Details
.start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
53 54 55 |
# File 'lib/u3d_core/update_checker/update_checker.rb', line 53 def start_time @start_time end |
Class Method Details
.ensure_rubygems_source ⇒ Object
Check if RubyGems is set as a gem source on some machines that might not be the case and then users can’t find the update when running the specified command
113 114 115 116 117 118 119 120 |
# File 'lib/u3d_core/update_checker/update_checker.rb', line 113 def self.ensure_rubygems_source return if `gem sources`.include?("https://rubygems.org") puts("") UI.error("RubyGems is not listed as your Gem source") UI.error("You can run `gem sources` to see all your sources") UI.error("Please run the following command to fix this:") UI.command("gem sources --add https://rubygems.org") end |
.fetch_latest(gem_name) ⇒ Object
122 123 124 |
# File 'lib/u3d_core/update_checker/update_checker.rb', line 122 def self.fetch_latest(gem_name) JSON.parse(U3d::Utils.page_content(generate_fetch_url(gem_name)))["version"] end |
.generate_fetch_url(gem_name) ⇒ Object
126 127 128 |
# File 'lib/u3d_core/update_checker/update_checker.rb', line 126 def self.generate_fetch_url(gem_name) "https://rubygems.org/api/v1/gems/#{gem_name}.json" end |
.server_results ⇒ Object
48 49 50 |
# File 'lib/u3d_core/update_checker/update_checker.rb', line 48 def self.server_results @results ||= {} end |
.show_update_message(gem_name, current_version) ⇒ Object
Show a message to the user to update to a new version of u3d Use this method, as this will detect the current Ruby environment and show an appropriate message to the user
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/u3d_core/update_checker/update_checker.rb', line 74 def self.(gem_name, current_version) available = server_results[gem_name] puts("") puts('#######################################################################') if available puts("# #{gem_name} #{available} is available. You are on #{current_version}.") else puts("# An update for #{gem_name} is available. You are on #{current_version}.") end puts("# You should use the latest version.") puts("# Please update using `#{update_command(gem_name: gem_name)}`.") # this could be fetched from the gem puts("# To see what's new, open https://github.com/DragonBox/#{gem_name}/releases.") if U3dCore::Env.truthy?("U3D_HIDE_CHANGELOG") if !Helper.bundler? && Random.rand(5) == 1 # We want to show this message from time to time, if the user doesn't use bundler puts('#######################################################################') puts("# Run `sudo gem cleanup` from time to time to speed up u3d") end puts('#######################################################################') Changelog.show_changes(gem_name, current_version, update_gem_command: UpdateChecker.update_command(gem_name)) unless U3dCore::Env.truthy?("U3D_HIDE_CHANGELOG") ensure_rubygems_source end |
.show_update_status(gem_name, current_version) ⇒ Object
66 67 68 69 |
# File 'lib/u3d_core/update_checker/update_checker.rb', line 66 def self.show_update_status(gem_name, current_version) wait_for_results unless update_available?(gem_name, current_version) (gem_name, current_version) if update_available?(gem_name, current_version) end |
.start_looking_for_update(gem_name) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/u3d_core/update_checker/update_checker.rb', line 31 def self.start_looking_for_update(gem_name) return if Helper.test? return if U3dCore::Env.truthy?("U3D_SKIP_UPDATE_CHECK") @start_time = Time.now Thread.new do begin server_results[gem_name] = fetch_latest(gem_name) # rubocop:disable Lint/HandleExceptions rescue StandardError # rubocop:enable Lint/HandleExceptions # we don't want to show a stack trace if something goes wrong end end end |
.update_available?(gem_name, current_version) ⇒ Boolean
56 57 58 59 |
# File 'lib/u3d_core/update_checker/update_checker.rb', line 56 def self.update_available?(gem_name, current_version) latest = server_results[gem_name] return (latest && (Gem::Version.new(latest) > Gem::Version.new(current_version))) end |
.update_command(gem_name) ⇒ Object
The command that the user should use to update the gem
101 102 103 104 105 106 107 |
# File 'lib/u3d_core/update_checker/update_checker.rb', line 101 def self.update_command(gem_name) if Helper.bundler? "bundle update #{gem_name.downcase}" else "sudo gem install #{gem_name.downcase}" end end |
.wait_for_results ⇒ Object
wait a abit for results when commands run real quick
62 63 64 |
# File 'lib/u3d_core/update_checker/update_checker.rb', line 62 def self.wait_for_results sleep([0, 0.4 - (Time.now - @start_time)].max) if @start_time end |