Class: Deprecool::CLI::Commands::Scan

Inherits:
Dry::CLI::Command
  • Object
show all
Defined in:
lib/deprecool/cli.rb

Instance Method Summary collapse

Instance Method Details

#call(paths:, format:, gems: [], all:, lockfile:) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/deprecool/cli.rb', line 42

def call(paths:, format:, gems: [], all:, lockfile:, **)
  json_output = format == 'json'
  files   = CLI.ruby_files(paths)

  if files.empty?
    if paths.empty?
      warn CLI.colorize('deprecool: please supply a path/to/file/or/directory', :red)
      exit 2
    end

    warn CLI.colorize("deprecool: no Ruby files found in #{paths.join(', ')}", :red)
    exit 2
  end

  if gems.any?
    puts "Scanning for deprecations from: #{gems.join(', ')}" unless json_output
    # we won't be scanning a lockfile
    gem_versions = []
  elsif all
    gem_versions = []
    gems = []
  else
    puts "Scanning #{lockfile}..." unless json_output
    gem_versions = LockfileParser.parse!(lockfile)

    # we don't need to look for individual gems when scanning gemfiles
    gems = []
  end

  finders  = Registry.applicable(gems:, gem_versions:, include_all: all)
  scanner  = Scanner.new(finders)
  offenses = files.flat_map { |file| scanner.scan_file(file) }
                  .sort_by  { |offense| [offense.file_path, offense.line, offense.column] }

  offenses = offenses.group_by(&:id) unless json_output

  CLI.report(offenses, format, finders)
  exit(offenses.empty? ? 0 : 1)
end