require 'open-uri' module FastlaneCore # Verifies, the user runs the latest version of this gem class UpdateChecker # This web service is fully open source: https://github.com/fastlane/refresher UPDATE_URL = "https://fastlane-refresher.herokuapp.com/" def self.start_looking_for_update(gem_name) Thread.new do begin server_results[gem_name] = fetch_latest(gem_name) rescue end end end def self.show_update_status(gem_name, current_version) latest = server_results[gem_name] if latest and Gem::Version.new(latest) > Gem::Version.new(current_version) show_update_message(gem_name, latest, current_version) end end def self.show_update_message(gem_name, available, current_version) v = fetch_latest(gem_name) puts '#######################################################################'.green puts "# #{gem_name} #{available} is available. You are on #{current_version}.".green puts "# It is recommended to use the latest version.".green puts "# Update using 'sudo gem update #{gem_name.downcase}'.".green puts "# To see what's new, open https://github.com/KrauseFx/#{gem_name}/releases.".green puts '#######################################################################'.green end def self.server_results @@results ||= {} end # Old code # This method will check if the latest version is installed and show a warning if that's not the case def self.verify_latest_version(gem_name, current_version) return true unless self.update_available?(gem_name, current_version) v = fetch_latest(gem_name) puts '#######################################################################'.green puts "# #{gem_name} #{v} is available. You are on #{current_version}.".green puts "# It is recommended to use the latest version.".green puts "# Update using 'sudo gem update #{gem_name.downcase}'.".green puts "# To see what's new, open https://github.com/KrauseFx/#{gem_name}/releases.".green puts '#######################################################################'.green false end # Is a new official release available (this does not include pre-releases) def self.update_available?(gem_name, current_version) begin latest = fetch_latest(gem_name) if latest and Gem::Version.new(latest) > Gem::Version.new(current_version) return true end rescue => ex Helper.log.error("Could not check if '#{gem_name}' is up to date.") end return false end # Relevant code private def self.fetch_latest(gem_name) url = UPDATE_URL + gem_name JSON.parse(open(url).read)["version"] end end end