Class: Jets::Thor::VersionCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/thor/version_check.rb

Instance Method Summary collapse

Instance Method Details

#check!Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jets/thor/version_check.rb', line 6

def check!
  return unless check_needed?

  remote_version = Gems.info("jets")["version"]
  local_version = Gem.loaded_specs["jets"].version

  return if remote_version.nil?

  if Gem::Version.new(remote_version) > Gem::Version.new(local_version)
    puts <<~EOL
      jets has a newer version available.

      installed version: #{local_version}
      latest version:    #{remote_version}

      Please update jets
    EOL
  end

  save_last_checked_time
end

#check_needed?Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/jets/thor/version_check.rb', line 28

def check_needed?
  check_interval = 24 * 60 * 60  # 24 hours in seconds
  Time.now - last_checked_time >= check_interval
end

#last_check_fileObject



43
44
45
46
47
48
49
# File 'lib/jets/thor/version_check.rb', line 43

def last_check_file
  # Do not define last_check_file as a LAST_CHECK_FILE constant
  # On AWS lambda, Jets eager load errors since ENV["HOME"] is nil
  # Note: Added an extra safeguard in case ENV["HOME"] is nil
  home = ENV["HOME"] || "/root"
  File.join(home, ".jets/tmp/last-checked.txt")
end

#last_checked_timeObject



33
34
35
36
# File 'lib/jets/thor/version_check.rb', line 33

def last_checked_time
  last_time = File.exist?(last_check_file) ? File.read(last_check_file) : "1970-01-01 00:00:00 UTC"
  Time.parse(last_time)
end

#save_last_checked_timeObject



38
39
40
41
# File 'lib/jets/thor/version_check.rb', line 38

def save_last_checked_time
  FileUtils.mkdir_p(File.dirname(last_check_file))
  File.write(last_check_file, Time.now)
end