Class: Collie::CLI
- Inherits:
-
Thor
- Object
- Thor
- Collie::CLI
- Defined in:
- lib/collie/cli.rb
Overview
Command-line interface
Constant Summary collapse
- PARSE_ERROR_RULE =
Class.new do class << self def rule_name = "ParseError" def description = "Reports grammar parse errors" def severity = :error def autocorrectable = false end end
Class Method Summary collapse
Instance Method Summary collapse
- #ast(file = nil) ⇒ Object
- #config_schema ⇒ Object
- #explain(rule_name) ⇒ Object
- #fmt(*files) ⇒ Object
- #init ⇒ Object
- #lint(*files) ⇒ Object
- #rules ⇒ Object
- #tokens(file = nil) ⇒ Object
- #version ⇒ Object
Class Method Details
.exit_on_failure? ⇒ Boolean
21 22 23 |
# File 'lib/collie/cli.rb', line 21 def self.exit_on_failure? true end |
Instance Method Details
#ast(file = nil) ⇒ Object
177 178 179 180 181 182 183 184 185 |
# File 'lib/collie/cli.rb', line 177 def ast(file = nil) source, filename = debug_source(file) tree = parse_source(source, filename: filename) puts JSON.pretty_generate(Parser::DebugSerializer.ast(tree)) rescue Error => e say e., :red exit 1 end |
#config_schema ⇒ Object
205 206 207 |
# File 'lib/collie/cli.rb', line 205 def config_schema puts JSON.pretty_generate(Config.schema) end |
#explain(rule_name) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/collie/cli.rb', line 136 def explain(rule_name) config = load_config Linter::Registry.load_rules rule = Linter::Registry.find(rule_name) unless rule say "Unknown rule: #{rule_name}", :red exit 1 end rule_config = config.rule_config(rule.rule_name) = (rule, config, rule_config) if [:format] == "json" puts JSON.pretty_generate() else say [:name], :bold say " Description: #{[:description]}" say " Enabled: #{[:enabled]}" say " Severity: #{[:severity]}" say " Autocorrectable: #{[:autocorrectable]}" end end |
#fmt(*files) ⇒ Object
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 |
# File 'lib/collie/cli.rb', line 76 def fmt(*files) config = load_config formatter = Formatter::Formatter.new(Formatter::Options.new(config.)) return fmt_stdin(formatter) if [:stdin] files = resolve_files(files, config) if files.empty? say "No files matched", :red exit 1 end failed = false changed = false files.each do |file| unless File.exist?(file) say "File not found: #{file}", :red failed = true next end result = format_file(file, formatter, check: [:check], diff: [:diff]) failed = true if result == :failed changed = true if result == :changed end exit 1 if failed || changed end |
#init ⇒ Object
193 194 195 196 197 198 199 200 201 202 |
# File 'lib/collie/cli.rb', line 193 def init path = [:path] return if File.exist?(path) && !yes?("#{path} already exists. Overwrite? (y/n)") Config.generate_default(path, profile: [:profile]) say "Generated #{path}", :green rescue Error => e say e., :red exit 1 end |
#lint(*files) ⇒ Object
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 |
# File 'lib/collie/cli.rb', line 37 def lint(*files) config = load_config Linter::Registry.load_rules validate_rule_filters! return lint_stdin(config) if [:stdin] files = resolve_files(files, config) if files.empty? say "No files matched", :red exit 1 end all_offenses = [] failed = false files.each do |file| unless File.exist?(file) say "File not found: #{file}", :red failed = true next end offenses = lint_file(file, config) all_offenses.concat(offenses) end reporter = create_reporter([:format]) puts reporter.report(all_offenses) exit 1 if failed || fail_level_reached?(all_offenses) end |
#rules ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/collie/cli.rb', line 109 def rules config = load_config Linter::Registry.load_rules if [:format] == "json" output = Linter::Registry.all.map do |rule| rule_config = config.rule_config(rule.rule_name) (rule, config, rule_config) end puts JSON.pretty_generate(output) else say "Available lint rules:", :bold Linter::Registry.all.each do |rule| rule_config = config.rule_config(rule.rule_name) severity = configured_rule_severity(rule, rule_config) severity_color = severity_color(severity) autocorrect = rule.autocorrectable ? " [autocorrectable]" : "" enabled = config.rule_enabled?(rule.rule_name) ? "" : " [disabled]" say " #{rule.rule_name} (#{set_color(severity, severity_color)})#{autocorrect}#{enabled}" say " #{rule.description}", :dim end end end |
#tokens(file = nil) ⇒ Object
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/collie/cli.rb', line 163 def tokens(file = nil) source, filename = debug_source(file) lexer = Parser::Lexer.new(source, filename: filename) output = lexer.tokenize.map { |token| Parser::DebugSerializer.token(token) } puts JSON.pretty_generate(output) rescue Error => e say e., :red exit 1 end |