Class: ThemeCheck::Cli

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

Defined Under Namespace

Classes: Abort

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCli

Returns a new instance of Cli.



10
11
12
13
14
15
16
17
18
# File 'lib/theme_check/cli.rb', line 10

def initialize
  @path = "."
  @command = :check
  @include_categories = []
  @exclude_categories = []
  @auto_correct = false
  @config_path = nil
  @fail_level = :error
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/theme_check/cli.rb', line 8

def path
  @path
end

Class Method Details

.parse_and_run(argv) ⇒ Object



130
131
132
133
134
# File 'lib/theme_check/cli.rb', line 130

def self.parse_and_run(argv)
  cli = new
  cli.parse(argv)
  cli.run
end

.parse_and_run!(argv) ⇒ Object



124
125
126
127
128
# File 'lib/theme_check/cli.rb', line 124

def self.parse_and_run!(argv)
  cli = new
  cli.parse(argv)
  cli.run!
end

Instance Method Details

#checkObject

Raises:



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/theme_check/cli.rb', line 168

def check
  puts "Checking #{@config.root} ..."
  storage = ThemeCheck::FileSystemStorage.new(@config.root, ignored_patterns: @config.ignored_patterns)
  theme = ThemeCheck::Theme.new(storage)
  if theme.all.empty?
    raise Abort, "No templates found."
  end
  analyzer = ThemeCheck::Analyzer.new(theme, @config.enabled_checks, @config.auto_correct)
  analyzer.analyze_theme
  analyzer.correct_offenses
  ThemeCheck::Printer.new.print(theme, analyzer.offenses, @config.auto_correct)
  raise Abort, "" if analyzer.uncorrectable_offenses.any? do |offense|
    offense.check.severity_value <= Check.severity_value(@fail_level)
  end
end

#helpObject



164
165
166
# File 'lib/theme_check/cli.rb', line 164

def help
  puts option_parser.to_s
end

#initObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/theme_check/cli.rb', line 144

def init
  dotfile_path = ThemeCheck::Config.find(@path)
  if dotfile_path.nil?
    config_name = if @config_path && @config_path[0] == ":"
      "#{@config_path[1..]}.yml"
    else
      "default.yml"
    end
    File.write(File.join(@path, ThemeCheck::Config::DOTFILE), File.read(ThemeCheck::Config.bundled_config_path(config_name)))

    puts "Writing new #{ThemeCheck::Config::DOTFILE} to #{@path}"
  else
    raise Abort, "#{ThemeCheck::Config::DOTFILE} already exists at #{@path}"
  end
end

#listObject



136
137
138
# File 'lib/theme_check/cli.rb', line 136

def list
  puts @config.enabled_checks
end

#option_parser(parser = OptionParser.new, help: true) ⇒ Object



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

def option_parser(parser = OptionParser.new, help: true)
  return @option_parser if defined?(@option_parser)
  @option_parser = parser
  @option_parser.banner = "Usage: theme-check [options] [/path/to/your/theme]"

  @option_parser.separator("")
  @option_parser.separator("Basic Options:")
  @option_parser.on(
    "-C", "--config PATH",
    "Use the config provided, overriding .theme-check.yml if present",
    "Use :theme_app_extension to use default checks for theme app extensions"
  ) { |path| @config_path = path }
  @option_parser.on(
    "-c", "--category CATEGORY", Check::CATEGORIES, "Only run this category of checks",
    "Runs checks matching all categories when specified more than once"
  ) { |category| @include_categories << category.to_sym }
  @option_parser.on(
    "-x", "--exclude-category CATEGORY", Check::CATEGORIES, "Exclude this category of checks",
    "Excludes checks matching any category when specified more than once"
  ) { |category| @exclude_categories << category.to_sym }
  @option_parser.on(
    "-a", "--auto-correct",
    "Automatically fix offenses"
  ) { @auto_correct = true }
  @option_parser.on(
    "--fail-level SEVERITY", Check::SEVERITIES,
    "Minimum severity (error|suggestion|style) for exit with error code"
  ) do |severity|
    @fail_level = severity.to_sym
  end

  @option_parser.separator("")
  @option_parser.separator("Miscellaneous:")
  @option_parser.on(
    "--init",
    "Generate a .theme-check.yml file"
  ) { @command = :init }
  @option_parser.on(
    "--print",
    "Output active config to STDOUT"
  ) { @command = :print }
  @option_parser.on(
    "-h", "--help",
    "Show this. Hi!"
  ) { @command = :help } if help
  @option_parser.on(
    "-l", "--list",
    "List enabled checks"
  ) { @command = :list }
  @option_parser.on(
    "-v", "--version",
    "Print Theme Check version"
  ) { @command = :version }

  @option_parser.separator("")
  @option_parser.separator(<<~EOS)
    Description:
        Theme Check helps you follow Shopify Themes & Liquid best practices by analyzing the
        Liquid & JSON inside your theme.

        You can configure checks in the .theme-check.yml file of your theme root directory.
  EOS

  @option_parser
end

#parse(argv) ⇒ Object



86
87
88
89
90
# File 'lib/theme_check/cli.rb', line 86

def parse(argv)
  @path = option_parser.parse(argv).first || "."
rescue OptionParser::InvalidArgument => e
  abort(e.message)
end


160
161
162
# File 'lib/theme_check/cli.rb', line 160

def print
  puts YAML.dump(@config.to_h)
end

#runObject



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/theme_check/cli.rb', line 110

def run
  run!
  exit(0)
rescue Abort => e
  if e.message.empty?
    exit(1)
  else
    abort(e.message)
  end
rescue ThemeCheckError => e
  STDERR.puts(e.message)
  exit(2)
end

#run!Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/theme_check/cli.rb', line 92

def run!
  unless [:version, :init, :help].include?(@command)
    @config = if @config_path
      ThemeCheck::Config.new(
        root: @path,
        configuration: ThemeCheck::Config.load_config(@config_path)
      )
    else
      ThemeCheck::Config.from_path(@path)
    end
    @config.include_categories = @include_categories unless @include_categories.empty?
    @config.exclude_categories = @exclude_categories unless @exclude_categories.empty?
    @config.auto_correct = @auto_correct
  end

  send(@command)
end

#versionObject



140
141
142
# File 'lib/theme_check/cli.rb', line 140

def version
  puts ThemeCheck::VERSION
end