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 =
"#{__dir__}/../../config"
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.



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
# File 'lib/theme_check/config.rb', line 68

def initialize(root: nil, configuration: nil, should_resolve_requires: true)
  @configuration = if configuration
    # TODO: Do we need to handle extends here? What base configuration
    # should we validate against once Theme App Extensions has its own
    # checks? :all?
    validate_configuration(configuration)
  else
    {}
  end

  # Follow extends
  extends = @configuration["extends"] || ":default"
  while extends
    extended_configuration = self.class.load_config(extends)
    extends = extended_configuration["extends"]
    @configuration = merge_configurations!(@configuration, extended_configuration)
  end

  @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_path(name) ⇒ Object



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

def bundled_config_path(name)
  "#{BUNDLED_CONFIGS_DIR}/#{name}"
end

.defaultObject



63
64
65
# File 'lib/theme_check/config.rb', line 63

def default
  @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_file(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



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

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

.load_config(path) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/theme_check/config.rb', line 53

def load_config(path)
  if path[0] == ":"
    load_bundled_config("#{path[1..]}.yml")
  elsif path.is_a?(Symbol)
    load_bundled_config("#{path}.yml")
  else
    load_file(path)
  end
end

.load_file(absolute_path) ⇒ Object



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

def load_file(absolute_path)
  @last_loaded_config = absolute_path
  YAML.load_file(absolute_path)
end

Instance Method Details

#[](name) ⇒ Object



97
98
99
# File 'lib/theme_check/config.rb', line 97

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

#check_configurationsObject



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

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

#enabled_checksObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/theme_check/config.rb', line 109

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



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

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

#exclude_categories=(categories) ⇒ Object



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

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

#ignored_patternsObject



134
135
136
# File 'lib/theme_check/config.rb', line 134

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

#include_categoriesObject



138
139
140
# File 'lib/theme_check/config.rb', line 138

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

#include_categories=(categories) ⇒ Object



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

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

#to_hObject



101
102
103
# File 'lib/theme_check/config.rb', line 101

def to_h
  @configuration
end