Module: AiCli::Commands::Update

Defined in:
lib/aicli/commands/update.rb

Constant Summary collapse

RUBYGEMS_GEM_URI =
'https://rubygems.org/api/v1/gems/aicli.json'

Class Method Summary collapse

Class Method Details

.runObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/aicli/commands/update.rb', line 15

def run
  pastel = Pastel.new
  current = Gem::Version.new(AiCli::VERSION)

  puts ''
  puts pastel.dim("Current version: #{current}")

  latest_str = fetch_latest_version
  latest = Gem::Version.new(latest_str)
  puts pastel.dim("Latest on RubyGems: #{latest}")
  puts ''

  if latest < current
    puts pastel.yellow(
      "Installed #{current} is newer than RubyGems #{latest}. Skipping update."
    )
    puts pastel.dim('Publish a new version, then run `ai update` again.')
    return
  end

  if latest == current
    puts pastel.green("Already up to date (#{current}).")
    return
  end

  command = ['gem', 'install', 'aicli', '-v', latest_str]
  puts pastel.dim("#{Helpers::I18n.t('Running')}: #{command.join(' ')}")
  puts ''

  ok = system(*command)
  puts ''

  unless ok
    raise Helpers::KnownError,
          'Failed to update aicli. Try manually: gem install aicli'
  end

  puts pastel.green("Updated aicli to #{latest}.")
  puts pastel.dim('Open a new shell if `ai version` still shows the old number.')
rescue Helpers::KnownError => e
  puts "\n#{pastel.red('')} #{e.message}"
  exit 1
rescue StandardError => e
  puts "\n#{pastel.red('')} Could not check RubyGems for updates: #{e.message}"
  Helpers::Error.handle_cli_error(e)
  exit 1
end