Module: Psdk::Cli::VersionUpdate

Defined in:
lib/psdk/helpers/version_update.rb

Overview

Module holding the logic to update the psdk-cli gem

Class Method Summary collapse

Class Method Details

.check_and_updateObject

Check if the psdk-cli gem is up-to-date and update it if needed



12
13
14
15
16
17
18
19
20
21
# File 'lib/psdk/helpers/version_update.rb', line 12

def check_and_update
  puts 'Checking for updates...'
  local_version = Psdk::Cli::VERSION
  remote_version = fetch_remote_version

  compare_and_update_versions(local_version, remote_version)
rescue StandardError => e
  $stderr.puts "Failed to update: #{e.message}"
  exit(1)
end

.compare_and_update_versions(local_version, remote_version) ⇒ Object

Compare local and remote versions and update if necessary

Parameters:

  • local_version (String)

    The currently installed version

  • remote_version (String)

    The latest version available



26
27
28
29
30
31
32
33
# File 'lib/psdk/helpers/version_update.rb', line 26

def compare_and_update_versions(local_version, remote_version)
  if Gem::Version.new(remote_version) > Gem::Version.new(local_version)
    puts "New version available: #{remote_version} (current: #{local_version})"
    update_gem
  else
    puts 'psdk-cli is up-to-date.'
  end
end

.fetch_remote_versionString

Fetch the latest version of psdk-cli from rubygems

Returns:

  • (String)


37
38
39
40
41
42
43
# File 'lib/psdk/helpers/version_update.rb', line 37

def fetch_remote_version
  output = `gem search -r psdk-cli`
  match = output.match(/psdk-cli \(([\d.]+)\)/)
  return match[1] if match

  raise 'Could not find psdk-cli in remote gems'
end

.update_gemObject

Update the psdk-cli gem



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/psdk/helpers/version_update.rb', line 46

def update_gem
  print 'Do you want to update psdk-cli? [Y/n] '
  response = $stdin.gets.chomp
  return unless response.empty? || response.casecmp('y').zero?

  puts 'Updating psdk-cli...'
  result = system('gem install psdk-cli')
  raise 'gem install psdk-cli failed' unless result

  puts 'Update complete.'
  exit
end