Module: Outdated::CLI

Defined in:
lib/outdated/cli.rb

Class Method Summary collapse

Class Method Details

.runObject



5
6
7
8
9
10
11
12
13
14
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/outdated/cli.rb', line 5

def self.run
  filename = '.outdated.json'
  options = File.exist?(filename) ? JSON.parse(File.read(filename)) : {}
  exclusions = options["exclusions"] || []

  Bundler.ui = Bundler::UI::Shell.new
  current_specs = Bundler.definition.resolve
  definition = Bundler.definition(true)
  definition.resolve_remotely!
  exit_status = 0

  # Bundler.load.dependencies does not include non dev dependencies, not
  # sure why. TODO figure out.
  # current_dependencies = Bundler.load.dependencies.map { |dep| [dep.name, dep] }.to_h
  gemfile_specs = current_specs # .select { |spec| current_dependencies.key? spec.name }

  print "Inspecting gem versions"

  gemfile_specs.sort_by(&:name).each do |used|
    name = used.name
    gem_exclusions = exclusions.find { |exc| exc['gem'] == name } || {}
    excluded_rules = gem_exclusions['rules'] || []

    gem = Outdated::RubyGems.gem(name)
    next if gem.specs.empty?

    used = gem.get(used.version)
    next unless used

    recommended_spec, code = gem.recommend(used, 1.week.ago)

    if code == Outdated::OUTDATED
      next if excluded_rules.include? Outdated::OUTDATED

      puts "\n#{name} #{used.version} is outdated. " \
           "#{recommended_spec.version} published #{recommended_spec.created_at}."
      exit_status = 1
      next
    end

    if code == Outdated::IMMATURE
      next if excluded_rules.include? Outdated::IMMATURE

      puts "\n#{name} #{used.version} is too new and may contain bugs or " \
           'vulnerabilities that are as yet unknown. It was published ' \
           "#{used.created_at}."
      puts " For now use #{recommended_spec.version} instead." if recommended_spec.present?
      exit_status = 1
      next
    end

    putc '.'
  end

  puts "\nGem versions deemed to be sufficiently up-to-date." if exit_status.zero?
  exit_status
end