forked from Shopify/erb_lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.rb
416 lines (335 loc) · 12 KB
/
cli.rb
1
2
3
4
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# frozen_string_literal: true
require "erb_lint/all"
require "active_support"
require "active_support/inflector"
require "optparse"
require "psych"
require "yaml"
require "rainbow"
require "erb_lint/utils/severity_levels"
module ERBLint
class CLI
include Utils::SeverityLevels
DEFAULT_CONFIG_FILENAME = ".erb-lint.yml"
DEFAULT_LINT_ALL_GLOB = "**/*.html{+*,}.erb"
class ExitWithFailure < RuntimeError; end
class ExitWithSuccess < RuntimeError; end
def initialize
@options = {}
@config = nil
@files = []
@stats = Stats.new
end
def run(args = ARGV)
dupped_args = args.dup
load_options(dupped_args)
if with_cache? && autocorrect?
failure!("cannot run autocorrect mode with cache")
end
@files = @options[:stdin] || dupped_args
load_config
@cache = Cache.new(@config, file_loader, prune_cache?) if with_cache? || clear_cache?
if clear_cache?
if cache.cache_dir_exists?
cache.clear
success!("cache directory cleared")
else
failure!("cache directory doesn't exist, skipping deletion.")
end
end
if !@files.empty? && lint_files.empty?
if allow_no_files?
success!("no files found...\n")
else
failure!("no files found...\n")
end
elsif lint_files.empty?
failure!("no files found or given, specify files or config...\n#{option_parser}")
end
ensure_files_exist(lint_files)
if enabled_linter_classes.empty?
failure!("no linter available with current configuration")
end
@options[:format] ||= :multiline
@options[:fail_level] ||= severity_level_for_name(:refactor)
@stats.files = lint_files.size
@stats.linters = enabled_linter_classes.size
@stats.autocorrectable_linters = enabled_linter_classes.count(&:support_autocorrect?)
reporter = Reporter.create_reporter(@options[:format], @stats, autocorrect?)
reporter.preview
runner = ERBLint::Runner.new(file_loader, @config)
file_content = nil
lint_files.each do |filename|
runner.clear_offenses
begin
file_content = run_on_file(runner, filename)
rescue => e
@stats.exceptions += 1
puts "Exception occurred when processing: #{relative_filename(filename)}"
puts "If this file cannot be processed by erb-lint, "\
"you can exclude it in your configuration file."
puts e.message
puts Rainbow(e.backtrace.join("\n")).red
puts
end
end
cache.close if with_cache? || clear_cache?
reporter.show
if stdin? && autocorrect?
# When running from stdin, we only lint a single file
puts "================ #{lint_files.first} ==================\n"
puts file_content
end
@stats.found == 0 && @stats.exceptions == 0
rescue OptionParser::InvalidOption, OptionParser::InvalidArgument, ExitWithFailure => e
warn(Rainbow(e.message).red)
false
rescue ExitWithSuccess => e
puts e.message
true
rescue => e
warn(Rainbow("#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}").red)
false
end
private
attr_reader :cache, :config
def run_on_file(runner, filename)
file_content = read_content(filename)
if with_cache? && !autocorrect?
run_using_cache(runner, filename, file_content)
else
file_content = run_with_corrections(runner, filename, file_content)
end
log_offense_stats(runner, filename)
file_content
end
def run_using_cache(runner, filename, file_content)
if (cache_result_offenses = cache.get(filename, file_content))
runner.restore_offenses(cache_result_offenses)
else
run_with_corrections(runner, filename, file_content)
cache.set(filename, file_content, runner.offenses.map(&:to_cached_offense_json_format).to_json)
end
end
def autocorrect?
@options[:autocorrect]
end
def with_cache?
@options[:with_cache]
end
def prune_cache?
@options[:prune_cache]
end
def clear_cache?
@options[:clear_cache]
end
def run_with_corrections(runner, filename, file_content)
7.times do
processed_source = ERBLint::ProcessedSource.new(filename, file_content)
runner.run(processed_source)
break unless autocorrect? && runner.offenses.any?
corrector = correct(processed_source, runner.offenses)
break if corrector.corrections.empty?
break if processed_source.file_content == corrector.corrected_content
@stats.corrected += corrector.corrections.size
# Don't overwrite the file if the input comes from stdin
unless stdin?
File.open(filename, "wb") do |file|
file.write(corrector.corrected_content)
end
end
file_content = corrector.corrected_content
runner.clear_offenses
end
file_content
end
def log_offense_stats(runner, filename)
offenses_filename = relative_filename(filename)
offenses = runner.offenses || []
@stats.ignored, @stats.found = offenses.partition do |offense|
severity_level_for_name(offense.severity) < @options[:fail_level]
end.map(&:size)
.zip([@stats.ignored, @stats.found])
.map(&:sum)
@stats.processed_files[offenses_filename] ||= []
@stats.processed_files[offenses_filename] |= offenses
end
def read_content(filename)
return File.read(filename, encoding: Encoding::UTF_8) unless stdin?
$stdin.binmode.read.force_encoding(Encoding::UTF_8)
end
def correct(processed_source, offenses)
corrector = ERBLint::Corrector.new(processed_source, offenses)
failure!(corrector.diagnostics.join(", ")) if corrector.diagnostics.any?
corrector
end
def config_filename
@config_filename ||= @options[:config] || DEFAULT_CONFIG_FILENAME
end
def load_config
if File.exist?(config_filename)
config = RunnerConfig.new(file_loader.yaml(config_filename), file_loader)
@config = RunnerConfig.default_for(config)
else
warn(Rainbow("#{config_filename} not found: using default config").yellow)
@config = RunnerConfig.default
end
rescue Psych::SyntaxError => e
failure!("error parsing config: #{e.message}")
ensure
@config.merge!(runner_config_override)
end
def file_loader
@file_loader ||= ERBLint::FileLoader.new(Dir.pwd)
end
def load_options(args)
option_parser.parse!(args)
end
def lint_files
@lint_files ||=
if @options[:lint_all]
pattern = File.expand_path(glob, Dir.pwd)
Dir[pattern].select { |filename| !excluded?(filename) }
else
@files
.map { |f| Dir.exist?(f) ? Dir[File.join(f, glob)] : f }
.map { |f| f.include?("*") ? Dir[f] : f }
.flatten
.map { |f| File.expand_path(f, Dir.pwd) }
.select { |filename| !excluded?(filename) }
end
end
def glob
@config.to_hash["glob"] || DEFAULT_LINT_ALL_GLOB
end
def excluded?(filename)
@config.global_exclude.any? do |path|
expanded_path = File.expand_path(path, Dir.pwd)
File.fnmatch?(expanded_path, filename)
end
end
def failure!(msg)
raise ExitWithFailure, msg
end
def success!(msg)
raise ExitWithSuccess, msg
end
def ensure_files_exist(files)
files.each do |filename|
unless File.exist?(filename)
failure!("#{filename}: does not exist")
end
end
end
def known_linter_names
@known_linter_names ||= ERBLint::LinterRegistry.linters
.map(&:simple_name)
.map(&:underscore)
end
def enabled_linter_names
@enabled_linter_names ||=
@options[:enabled_linters] ||
known_linter_names
.select { |name| @config.for_linter(name.camelize).enabled? }
end
def enabled_linter_classes
@enabled_linter_classes ||= ERBLint::LinterRegistry.linters
.select { |klass| enabled_linter_names.include?(klass.simple_name.underscore) }
end
def relative_filename(filename)
filename.sub("#{File.expand_path(".", Dir.pwd)}/", "")
end
def runner_config_override
RunnerConfig.new(
linters: {}.tap do |linters|
ERBLint::LinterRegistry.linters.map do |klass|
linters[klass.simple_name] = { "enabled" => enabled_linter_classes.include?(klass) }
end
end
)
end
def option_parser
OptionParser.new do |opts|
opts.banner = "Usage: erblint [options] [file1, file2, ...]"
opts.on("--config FILENAME", "Config file [default: #{DEFAULT_CONFIG_FILENAME}]") do |config|
if File.exist?(config)
@options[:config] = config
else
failure!("#{config}: does not exist")
end
end
opts.on("-f", "--format FORMAT", format_options_help) do |format|
unless Reporter.available_format?(format)
error_message = invalid_format_error_message(format)
failure!(error_message)
end
@options[:format] = format
end
opts.on("--lint-all", "Lint all files matching configured glob [default: #{DEFAULT_LINT_ALL_GLOB}]") do |config|
@options[:lint_all] = config
end
opts.on("--enable-all-linters", "Enable all known linters") do
@options[:enabled_linters] = known_linter_names
end
opts.on("--with-cache", "Enable caching") do |config|
@options[:with_cache] = config
end
opts.on("--prune-cache", "Prune cache") do |config|
@options[:prune_cache] = config
end
opts.on("--clear-cache", "Clear cache") do |config|
@options[:clear_cache] = config
end
opts.on("--enable-linters LINTER[,LINTER,...]", Array,
"Only use specified linter", "Known linters are: #{known_linter_names.join(", ")}") do |linters|
linters.each do |linter|
unless known_linter_names.include?(linter)
failure!("#{linter}: not a valid linter name (#{known_linter_names.join(", ")})")
end
end
@options[:enabled_linters] = linters
end
opts.on("--fail-level SEVERITY", "Minimum severity for exit with error code") do |level|
parsed_severity = SEVERITY_CODE_TABLE[level.upcase.to_sym] || (SEVERITY_NAMES & [level.downcase]).first
if parsed_severity.nil?
failure!("#{level}: not a valid failure level (#{SEVERITY_NAMES.join(", ")})")
end
@options[:fail_level] = severity_level_for_name(parsed_severity)
end
opts.on("-a", "--autocorrect", "Correct offenses automatically if possible (default: false)") do |config|
@options[:autocorrect] = config
end
opts.on("--allow-no-files", "When no matching files found, exit successfully (default: false)") do |config|
@options[:allow_no_files] = config
end
opts.on(
"-sFILE",
"--stdin FILE",
"Pipe source from STDIN. Takes the path to be used to check which rules to apply."
) do |file|
@options[:stdin] = [file]
end
opts.on_tail("-h", "--help", "Show this message") do
success!(opts)
end
opts.on_tail("--version", "Show version") do
success!(ERBLint::VERSION)
end
end
end
def format_options_help
"Report offenses in the given format: "\
"(#{Reporter.available_formats.join(", ")}) (default: multiline)"
end
def invalid_format_error_message(given_format)
formats = Reporter.available_formats.map { |format| " - #{format}\n" }
"#{given_format}: is not a valid format. Available formats:\n#{formats.join}"
end
def stdin?
@options[:stdin].present?
end
def allow_no_files?
@options[:allow_no_files]
end
end
end