-
Notifications
You must be signed in to change notification settings - Fork 6
/
foodcritic.rb
71 lines (60 loc) · 2.04 KB
/
foodcritic.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
require 'pronto'
require 'foodcritic'
module Pronto
class FoodCritic < Runner
def run
return [] if paths[:cookbook_paths].none? && paths[:role_paths].none?
@linter = ::FoodCritic::Linter.new
@linter.check(options).warnings.flat_map do |warning|
ruby_patches.select { |patch| patch.new_file_full_path.to_s == warning.match[:filename] }
.flat_map(&:added_lines)
.select { |line| line.new_lineno == warning.match[:line] }
.flat_map { |line| new_message(warning, line) }
end
end
def options
@options ||= begin
result = {}.merge(paths)
result[:rule_file] = foodcritic_rule_file if foodcritic_rule_file
result[:search_gems] = foodcritic_search_gems if foodcritic_search_gems
result[:include_rules] = Array(foodcritic_include_rules)
result
end
end
def paths
@paths ||= begin
result = { cookbook_paths: [], role_paths: [] }
ruby_patches.each do |patch|
path = patch.new_file_full_path.to_s
next if foodcritic_exclude.any? { |r| File.fnmatch(r, path) }
if path.include?('cookbook')
result[:cookbook_paths] << path
elsif path.include?('role')
result[:role_paths] << path
end
end
result
end
end
def new_message(warning, line)
path = line.patch.delta.new_file[:path]
message = "#{warning.rule.code} - #{warning.rule.name}"
Message.new(path, line, :warning, message, nil, self.class)
end
def pronto_foodcritic_config
@pronto_foodcritic_config ||= Pronto::ConfigFile.new.to_h['foodcritic'] || {}
end
def foodcritic_search_gems
pronto_foodcritic_config.fetch('search_gems', nil)
end
def foodcritic_rule_file
pronto_foodcritic_config.fetch('rule_file', nil)
end
def foodcritic_include_rules
pronto_foodcritic_config.fetch('include_rules', [])
end
def foodcritic_exclude
Array(pronto_foodcritic_config.fetch('exclude', []))
end
end
end