require 'excon' require 'json' module SctCore class UpdateChecker def self.start_looking_for_update(gem_name) begin server_results[gem_name] = fetch_latest(gem_name) rescue => e # ignore possible exceptions UI.verbose("error occured fetching latest version #{e.message}") end end def self.server_results @results ||= {} end def self.update_available?(gem_name, current_version) latest = server_results[gem_name] return (latest and Gem::Version.new(latest) > Gem::Version.new(current_version)) end def self.show_update_status(gem_name, current_version) if update_available?(gem_name, current_version) show_update_message(gem_name, current_version) end end def self.show_update_message(gem_name, current_version) available = server_results[gem_name] puts("") UI.important('#######################################################################') if available UI.important("# #{gem_name} #{available} is available. You are on #{current_version}.") else UI.important("# An update for #{gem_name} is available. You are on #{current_version}.") end UI.important("# You should use the latest version.") UI.important("# Please update by running `#{self.update_command(gem_name: gem_name)}`.") UI.important('#######################################################################') ensure_rubygems_source end # 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 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 def self.update_command(gem_name: "sct") "sudo gem update #{gem_name.downcase}" end def self.fetch_latest(gem_name) JSON.parse(Excon.get(generate_fetch_url(gem_name)).body)["version"] end def self.generate_fetch_url(gem_name) "https://rubygems.org/api/v1/gems/#{gem_name}.json" end end end