Class: ThemeCheck::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/theme_check/config.rb

Constant Summary collapse

DOTFILE =
'.theme-check.yml'
BUNDLED_CONFIGS_DIR =
Pathname.new("#{__dir__}/../../config").realpath
BOOLEAN =
[true, false]

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root: nil, configuration: nil, should_resolve_requires: true) ⇒ Config

Returns a new instance of Config.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/theme_check/config.rb', line 84

def initialize(root: nil, configuration: nil, should_resolve_requires: true)
  @configuration = if configuration
    validate_configuration(configuration)
  else
    self.class.default
  end

  extends = @configuration["extends"] || :default
  @configuration = self.class.merge_configurations!(self.class.load_config(extends), @configuration)

  @root = if root && @configuration.key?("root")
    Pathname.new(root).join(@configuration["root"])
  elsif root
    Pathname.new(root)
  end

  @auto_correct = false

  resolve_requires if @root && should_resolve_requires
end

Class Attribute Details

.last_loaded_configObject (readonly)

Returns the value of attribute last_loaded_config.



13
14
15
# File 'lib/theme_check/config.rb', line 13

def last_loaded_config
  @last_loaded_config
end

Instance Attribute Details

#auto_correctObject

Returns the value of attribute auto_correct.



10
11
12
# File 'lib/theme_check/config.rb', line 10

def auto_correct
  @auto_correct
end

#rootObject (readonly)

Returns the value of attribute root.



9
10
11
# File 'lib/theme_check/config.rb', line 9

def root
  @root
end

Class Method Details

.bundled_config?(name) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/theme_check/config.rb', line 50

def bundled_config?(name)
  name.is_a?(Symbol) || (name.is_a?(String) && name[0] == ":")
end

.bundled_config_path(name) ⇒ Object



46
47
48
# File 'lib/theme_check/config.rb', line 46

def bundled_config_path(name)
  "#{BUNDLED_CONFIGS_DIR}/#{name.to_s.sub(/^:/, '')}.yml"
end

.defaultObject



79
80
81
# File 'lib/theme_check/config.rb', line 79

def default
  load_config(":default")
end

.find(root, needle = DOTFILE) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/theme_check/config.rb', line 32

def find(root, needle = DOTFILE)
  Pathname.new(root).descend.reverse_each do |path|
    pathname = path.join(needle)
    return pathname if pathname.exist?
  end
  nil
end

.from_hash(config) ⇒ Object



28
29
30
# File 'lib/theme_check/config.rb', line 28

def from_hash(config)
  new(configuration: config, should_resolve_requires: false)
end

.from_path(path) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/theme_check/config.rb', line 15

def from_path(path)
  if (filename = find(path))
    new(root: filename.dirname, configuration: load_config(filename))
  else
    # No configuration file
    new(root: path)
  end
end

.from_string(config) ⇒ Object



24
25
26
# File 'lib/theme_check/config.rb', line 24

def from_string(config)
  new(configuration: YAML.load(config), should_resolve_requires: false)
end

.load_bundled_config(name) ⇒ Object



54
55
56
# File 'lib/theme_check/config.rb', line 54

def load_bundled_config(name)
  load_file(bundled_config_path(name))
end

.load_config(name, pwd = Pathname.pwd) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/theme_check/config.rb', line 58

def load_config(name, pwd = Pathname.pwd)
  return load_bundled_config(name) if bundled_config?(name)
  path = name.is_a?(Pathname) ? name : Pathname.new(name)
  path = pwd.join(path) if path.relative?
  return {} unless path.exist?
  config = load_file(path)
  extends = config["extends"] || :default
  merge_configurations!(load_config(extends, path.realpath.dirname), config)
end

.load_file(absolute_path) ⇒ Object



40
41
42
43
44
# File 'lib/theme_check/config.rb', line 40

def load_file(absolute_path)
  @last_loaded_config = absolute_path
  # An empty file returns false, so we || {}.
  YAML.load_file(absolute_path) || {}
end

.merge_configurations!(config, other) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/theme_check/config.rb', line 68

def merge_configurations!(config, other)
  config.merge(other) do |_key, old_value, new_value|
    case old_value
    when Hash
      merge_configurations!(old_value, new_value)
    else
      new_value
    end
  end
end

Instance Method Details

#[](name) ⇒ Object



105
106
107
# File 'lib/theme_check/config.rb', line 105

def [](name)
  @configuration[name]
end

#check_configurationsObject



113
114
115
# File 'lib/theme_check/config.rb', line 113

def check_configurations
  @check_configurations ||= @configuration.select { |name, _| check_name?(name) }
end

#enabled_checksObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/theme_check/config.rb', line 117

def enabled_checks
  @enabled_checks ||= check_configurations.map do |check_name, options|
    next unless options["enabled"]

    check_class = ThemeCheck.const_get(check_name)

    next if check_class.categories.any? { |category| exclude_categories.include?(category) }
    next if include_categories.any? && !include_categories.all? { |category| check_class.categories.include?(category) }

    options_for_check = options.transform_keys(&:to_sym)
    options_for_check.delete(:enabled)
    severity = options_for_check.delete(:severity)
    ignored_patterns = options_for_check.delete(:ignore) || []
    check = if options_for_check.empty?
      check_class.new
    else
      check_class.new(**options_for_check)
    end
    check.severity = severity.to_sym if severity
    check.ignored_patterns = ignored_patterns
    check.options = options_for_check
    check
  end.compact
end

#exclude_categoriesObject



154
155
156
# File 'lib/theme_check/config.rb', line 154

def exclude_categories
  self["exclude_categories"] || []
end

#exclude_categories=(categories) ⇒ Object



158
159
160
# File 'lib/theme_check/config.rb', line 158

def exclude_categories=(categories)
  @configuration["exclude_categories"] = categories
end

#ignored_patternsObject



142
143
144
# File 'lib/theme_check/config.rb', line 142

def ignored_patterns
  self["ignore"] || []
end

#include_categoriesObject



146
147
148
# File 'lib/theme_check/config.rb', line 146

def include_categories
  self["include_categories"] || []
end

#include_categories=(categories) ⇒ Object



150
151
152
# File 'lib/theme_check/config.rb', line 150

def include_categories=(categories)
  @configuration["include_categories"] = categories
end

#to_hObject



109
110
111
# File 'lib/theme_check/config.rb', line 109

def to_h
  @configuration
end