-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathengine_config.rb
108 lines (89 loc) · 2.9 KB
/
engine_config.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
# frozen_string_literal: true
module CC
module Engine
module Analyzers
class EngineConfig
DEFAULT_COUNT_THRESHOLD = 2
InvalidConfigError = Class.new(StandardError)
def initialize(hash)
@config = normalize(hash)
end
def debug?
config.fetch("config", {}).fetch("debug", "false").to_s.casecmp("true").zero?
end
def include_paths
config.fetch("include_paths", ["./"])
end
def languages
config.fetch("languages", {})
end
def concurrency
config.fetch("config", {}).fetch("concurrency", 2).to_i
end
def filters_for(language)
fetch_language(language).fetch("filters", []).map { |filter|
Sexp::Matcher.parse filter
}
end
def mass_threshold_for(language)
threshold = fetch_language(language).fetch("mass_threshold", nil)
if threshold
threshold.to_i
end
end
def count_threshold_for(language)
threshold = fetch_language(language)["count_threshold"] ||
config.fetch("config", {}).fetch("count_threshold", nil) ||
DEFAULT_COUNT_THRESHOLD
if threshold
threshold.to_i
end
end
def fetch_language(language)
language = config.
fetch("languages", {}).
fetch(language, {})
if language.is_a? Hash
language
else
{}
end
end
def patterns_for(language, fallbacks)
Array(fetch_language(language).fetch("patterns", fallbacks))
end
private
attr_reader :config
def normalize(hash)
hash.tap do |config|
languages = config.fetch("config", {}).fetch("languages", {})
config["languages"] = build_language_config(languages)
end
end
def build_language_config(languages)
if languages.is_a?(Array)
languages.each_with_object({}) do |language, map|
language, config = coerce_array_entry(language)
map[language.downcase] = config
end
elsif languages.is_a?(Hash)
languages.each_with_object({}) do |(key, value), map|
map[key.downcase] = value
end
else
raise InvalidConfigError, "languages config entry is invalid: please check documentation for details of configuring languages"
end
end
def coerce_array_entry(entry)
if entry.is_a?(String)
[entry.downcase, {}]
elsif entry.is_a?(Hash) && entry.keys.count == 1
[entry.keys.first, entry[entry.keys.first]]
else
raise InvalidConfigError, "#{entry.inspect} is not a valid language entry"
end
end
end
end
end
end