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



159
160
161
162
163
# File 'lib/deprecool/cli.rb', line 159

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

.colorize_effort(effort) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File 'lib/deprecool/cli.rb', line 165

def colorize_effort(effort)
  # the effort method raises if you set anything but these
  # values so it's safe to assume, it would also be a hassle
  # to remove duplication here so oh well
  effort_colors = { low:    :green,
                    medium: :yellow,
                    high: :red }

  colorize(effort, effort_colors[effort])
end

.list_finders(finders) ⇒ Object



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

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



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

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



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

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



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

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

.text_report(offenses, finders) ⇒ Object



116
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
148
149
# File 'lib/deprecool/cli.rb', line 116

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

  offense_count = 0

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

    puts "\n#{colorize(offense.title, :bold)}"
    puts
    puts " * #{offense.summary}"
    puts
    puts "  #{colorize('effort:', :green).ljust(16)} #{colorize_effort(offense.effort)}"
    puts "  #{colorize('fix:', :green).ljust(16)} #{offense.suggestion}"   if offense.suggestion
    puts "  #{colorize('source(s):', :green).ljust(16)} #{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