Class: PlatformosCheck::Cli
- Inherits:
-
Object
- Object
- PlatformosCheck::Cli
- Defined in:
- lib/platformos_check/cli.rb
Defined Under Namespace
Classes: Abort
Constant Summary collapse
- FORMATS =
%i[text json]
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #check(out_stream = STDOUT) ⇒ Object
- #help ⇒ Object
- #init ⇒ Object
-
#initialize ⇒ Cli
constructor
A new instance of Cli.
- #list ⇒ Object
- #option_parser(parser = OptionParser.new, help: true) ⇒ Object
- #parse(argv) ⇒ Object
- #print ⇒ Object
- #print_with_format(platformos_app, analyzer, out_stream) ⇒ Object
- #profile ⇒ Object
- #run ⇒ Object
- #run! ⇒ Object
- #update_docs ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize ⇒ Cli
Returns a new instance of Cli.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/platformos_check/cli.rb', line 13 def initialize @path = "." @command = :check @include_categories = [] @exclude_categories = [] @auto_correct = false @update_docs = false @config_path = nil @fail_level = :error @format = :text end |
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
11 12 13 |
# File 'lib/platformos_check/cli.rb', line 11 def path @path end |
Class Method Details
.parse_and_run(argv) ⇒ Object
153 154 155 156 157 |
# File 'lib/platformos_check/cli.rb', line 153 def self.parse_and_run(argv) cli = new cli.parse(argv) cli.run end |
.parse_and_run!(argv) ⇒ Object
147 148 149 150 151 |
# File 'lib/platformos_check/cli.rb', line 147 def self.parse_and_run!(argv) cli = new cli.parse(argv) cli.run! end |
Instance Method Details
#check(out_stream = STDOUT) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/platformos_check/cli.rb', line 188 def check(out_stream = STDOUT) update_docs warn "Checking #{@config.root}:" storage = PlatformosCheck::FileSystemStorage.new(@config.root, ignored_patterns: @config.ignored_patterns) raise Abort, "No platformos_app files found." if storage.platformos_app.all.empty? analyzer = PlatformosCheck::Analyzer.new(storage.platformos_app, @config.enabled_checks, @config.auto_correct) analyzer.analyze_platformos_app analyzer.correct_offenses print_with_format(storage.platformos_app, analyzer, out_stream) # corrections are committed after printing so that the # source_excerpts are still pointing to the uncorrected source. analyzer.write_corrections raise Abort, "" if analyzer.uncorrectable_offenses.any? do |offense| offense.check.severity_value <= Check.severity_value(@fail_level) end end |
#help ⇒ Object
184 185 186 |
# File 'lib/platformos_check/cli.rb', line 184 def help puts option_parser end |
#init ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/platformos_check/cli.rb', line 167 def init dotfile_path = PlatformosCheck::Config.find(@path) raise Abort, "#{PlatformosCheck::Config::DOTFILE} already exists at #{@path}" unless dotfile_path.nil? config_name = @config_path || "default" File.write( File.join(@path, PlatformosCheck::Config::DOTFILE), File.read(PlatformosCheck::Config.bundled_config_path(config_name)) ) puts "Writing new #{PlatformosCheck::Config::DOTFILE} to #{@path}" end |
#list ⇒ Object
159 160 161 |
# File 'lib/platformos_check/cli.rb', line 159 def list puts @config.enabled_checks end |
#option_parser(parser = OptionParser.new, help: true) ⇒ Object
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 |
# File 'lib/platformos_check/cli.rb', line 25 def option_parser(parser = OptionParser.new, help: true) return @option_parser if defined?(@option_parser) @option_parser = parser @option_parser. = "Usage: platformos-check [options] [/path/to/your/platformos_app]" @option_parser.separator("") @option_parser.separator("Basic Options:") @option_parser.on( "-C", "--config PATH", "Use the config provided, overriding .platformos-check.yml if present", "Use :platformos_app_app_extension to use default checks for app extensions" ) { |path| @config_path = path } @option_parser.on( "-o", "--output FORMAT", FORMATS, "The output format to use. (text|json, default: text)" ) { |format| @format = format.to_sym } @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", [:crash] + 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 .platformos-check.yml file" ) { @command = :init } @option_parser.on( "--print", "Output active config to STDOUT" ) { @command = :print } @option_parser.on( "--update-docs", "Update PlatformOS Check docs (objects, filters, and tags)" ) { @update_docs = true } @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 PlatformOS Check version" ) { @command = :version } if ENV["PLATFORMOS_CHECK_DEBUG"] @option_parser.separator("") @option_parser.separator("Debugging:") @option_parser.on( "--profile", "Output a profile to STDOUT compatible with FlameGraph." ) { @command = :profile } end @option_parser.separator("") @option_parser.separator(<<~EOS) Description: PlatformOS Check helps you follow platformOS best practices by analyzing the Liquid & JSON inside your app. You can configure checks in the .platformos-check.yml file of your platformos_app root directory. EOS @option_parser end |
#parse(argv) ⇒ Object
109 110 111 112 113 |
# File 'lib/platformos_check/cli.rb', line 109 def parse(argv) @path = option_parser.parse(argv).first || "." rescue OptionParser::InvalidArgument => e abort(e.) end |
#print ⇒ Object
180 181 182 |
# File 'lib/platformos_check/cli.rb', line 180 def print puts YAML.dump(@config.to_h) end |
#print_with_format(platformos_app, analyzer, out_stream) ⇒ Object
229 230 231 232 233 234 235 236 |
# File 'lib/platformos_check/cli.rb', line 229 def print_with_format(platformos_app, analyzer, out_stream) case @format when :text PlatformosCheck::Printer.new(out_stream).print(platformos_app, analyzer.offenses, @config.auto_correct) when :json PlatformosCheck::JsonPrinter.new(out_stream).print(analyzer.offenses) end end |
#profile ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/platformos_check/cli.rb', line 215 def profile require 'ruby-prof-flamegraph' result = RubyProf.profile do check(STDERR) end # Print a graph profile to text printer = RubyProf::FlameGraphPrinter.new(result) printer.print(STDOUT, {}) rescue LoadError warn "Profiling is only available in development" end |
#run ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/platformos_check/cli.rb', line 133 def run run! exit(0) rescue Abort => e if e..empty? exit(1) else abort(e.) end rescue PlatformosCheckError => e warn(e.) exit(2) end |
#run! ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/platformos_check/cli.rb', line 115 def run! unless %i[version init help].include?(@command) @config = if @config_path PlatformosCheck::Config.new( root: @path, configuration: PlatformosCheck::Config.load_config(@config_path) ) else PlatformosCheck::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 |
#update_docs ⇒ Object
207 208 209 210 211 212 213 |
# File 'lib/platformos_check/cli.rb', line 207 def update_docs return unless @update_docs warn 'Updating documentation...' PlatformosCheck::PlatformosLiquid::SourceManager.download end |
#version ⇒ Object
163 164 165 |
# File 'lib/platformos_check/cli.rb', line 163 def version puts PlatformosCheck::VERSION end |