Module: Deprecool::CLI

Defined in:
lib/deprecool/cli.rb

Overview

Command-line entry point. Scans the given files/directories and reports deprecations.

Defined Under Namespace

Modules: Commands

Class Method Summary collapse

Class Method Details

.colorize(text, color) ⇒ Object



157
158
159
160
161
# File 'lib/deprecool/cli.rb', line 157

def colorize(text, color)
  colors = { red: 31, green: 32, yellow: 33, cyan: 36, bold: 1 }

  "\e[#{colors.fetch(color, nil)}m#{text}\e[0m"
end

.list_finders(finders) ⇒ Object



149
150
151
152
153
154
155
# File 'lib/deprecool/cli.rb', line 149

def list_finders(finders)
  puts 'Active finders:'
  finders.sort_by { [it.gem.to_s, it.deprecated_in.to_s, it.id.to_s] }.each do
    puts " - #{it.classname.ljust(35)} (#{it.gem} #{it.deprecated_in.to_s}) — #{it.title}"
  end
  puts '  (none)' if finders.empty?
end

.report(offenses, format, finders) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/deprecool/cli.rb', line 109

def report(offenses, format, finders)
  if format == 'json'
    puts JSON.pretty_generate(offenses.map(&:to_h))
  else
    text_report(offenses, finders)
  end
end

.ruby_files(paths) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/deprecool/cli.rb', line 94

def ruby_files(paths)
  paths = paths.empty? ? ['.'] : paths

  paths.flat_map do |path|
    if File.directory?(path)
      Dir[File.join(path, '**', '*.rb')]
    elsif File.file?(path)
      [path]
    else
      warn "deprecool: no such file or directory: #{path}"
      []
    end
  end.uniq.sort
end

.start(argv) ⇒ Object



90
91
92
# File 'lib/deprecool/cli.rb', line 90

def start(argv)
  Dry::CLI.new(Commands).call(arguments: argv)
end

.text_report(offenses, finders) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/deprecool/cli.rb', line 117

def text_report(offenses, finders)
  if offenses.empty?
    puts colorize('No deprecations found.', :green)
    puts "(#{finders.size} finder#{'s' unless finders.size == 1} active)"
    return
  end

  offense_count = 0

  offenses.each_value do |offense_array|
    offense = offense_array.first

    puts "\n#{colorize(offense.title, :bold)}\n"
    puts " * #{offense.summary}"
    puts "  #{colorize('fix:', :green)} #{offense.suggestion}" if offense.suggestion
    puts "  #{colorize('source:', :green)} #{offense.reference}" if offense.reference
    puts '  Found At:'
    offense_array.each do |o|
      offense_count   += 1
      confidence       = o.confidence
      confidence_color = confidence == :high ? :red : :yellow
      badge            = colorize("(#{confidence} confidence)", confidence_color)

      puts " (#{offense_count}) #{colorize(o.location, :cyan)} #{badge}"
      puts "    #{o.source_line.strip}" if o.source_line
      puts
    end
  end

  puts colorize("#{offense_count} deprecation#{'s' unless offense_count == 1} found.", :red)
end