Class: Roodi::Core::Runner
- Inherits:
-
Object
- Object
- Roodi::Core::Runner
- Defined in:
- lib/roodi/core/runner.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
writeonly
Sets the attribute config.
-
#files_checked ⇒ Object
readonly
Returns the value of attribute files_checked.
Instance Method Summary collapse
- #check(filename, content) ⇒ Object
- #check_content(content, filename = "dummy-file.rb") ⇒ Object
- #check_file(filename) ⇒ Object
- #collect_files(paths) ⇒ Object
- #default_config ⇒ Object
- #errors ⇒ Object
-
#initialize(*checks) ⇒ Runner
constructor
A new instance of Runner.
- #output_result(errors, files_checked) ⇒ Object
- #print(filename, content) ⇒ Object
- #print_content(content) ⇒ Object
- #print_file(filename) ⇒ Object
- #project_config ⇒ Object
- #roodi_gem_config ⇒ Object
- #start(paths) ⇒ Object
Constructor Details
#initialize(*checks) ⇒ Runner
Returns a new instance of Runner.
14 15 16 17 |
# File 'lib/roodi/core/runner.rb', line 14 def initialize(*checks) @config = default_config @checks = checks unless checks.empty? end |
Instance Attribute Details
#config=(value) ⇒ Object (writeonly)
Sets the attribute config
11 12 13 |
# File 'lib/roodi/core/runner.rb', line 11 def config=(value) @config = value end |
#files_checked ⇒ Object (readonly)
Returns the value of attribute files_checked.
12 13 14 |
# File 'lib/roodi/core/runner.rb', line 12 def files_checked @files_checked end |
Instance Method Details
#check(filename, content) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/roodi/core/runner.rb', line 70 def check(filename, content) @checks ||= load_checks @checker ||= CheckingVisitor.new(@checks) @checks.each {|check| check.start_file(filename)} node = parse(filename, content) node.accept(@checker) if node @checks.each {|check| check.end_file(filename)} end |
#check_content(content, filename = "dummy-file.rb") ⇒ Object
79 80 81 |
# File 'lib/roodi/core/runner.rb', line 79 def check_content(content, filename = "dummy-file.rb") check(filename, content) end |
#check_file(filename) ⇒ Object
83 84 85 86 |
# File 'lib/roodi/core/runner.rb', line 83 def check_file(filename) return unless File.exists?(filename) check(filename, File.read(filename)) end |
#collect_files(paths) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/roodi/core/runner.rb', line 56 def collect_files(paths) files = [] paths.each do |path| if File.file?(path) files << path elsif File.directory?(path) files += Dir.glob(File.join(path, '**/*.{rb}')) else files += Dir.glob(path) end end files end |
#default_config ⇒ Object
19 20 21 |
# File 'lib/roodi/core/runner.rb', line 19 def default_config project_config ? project_config : roodi_gem_config end |
#errors ⇒ Object
101 102 103 104 105 106 |
# File 'lib/roodi/core/runner.rb', line 101 def errors @checks ||= [] all_errors = @checks.collect {|check| check.errors} all_errors.flatten all_errors.flatten + parsing_errors end |
#output_result(errors, files_checked) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/roodi/core/runner.rb', line 44 def output_result(errors, files_checked) errors.each {|error| puts "\e[31m#{error}\e[0m"} puts "\nChecked #{files_checked} files" result = "Found #{errors.size} errors." if errors.empty? puts "\e[32m#{result}\e[0m" else raise "\e[31m#{result}\e[0m" end end |
#print(filename, content) ⇒ Object
88 89 90 91 |
# File 'lib/roodi/core/runner.rb', line 88 def print(filename, content) node = parse(filename, content) pp node end |
#print_content(content) ⇒ Object
93 94 95 |
# File 'lib/roodi/core/runner.rb', line 93 def print_content(content) print("dummy-file.rb", content) end |
#print_file(filename) ⇒ Object
97 98 99 |
# File 'lib/roodi/core/runner.rb', line 97 def print_file(filename) print(filename, File.read(filename)) end |
#project_config ⇒ Object
27 28 29 |
# File 'lib/roodi/core/runner.rb', line 27 def project_config File.exists?("roodi.yml") ? "roodi.yml" : nil end |
#roodi_gem_config ⇒ Object
23 24 25 |
# File 'lib/roodi/core/runner.rb', line 23 def roodi_gem_config File.join(File.dirname(__FILE__), "..", "..", "..", "roodi.yml") end |
#start(paths) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/roodi/core/runner.rb', line 31 def start(paths) puts "\nRunning Roodi checks" paths = ['.'] if paths == [] all_files = collect_files(paths) @files_checked = all_files.count all_files.each do |path| check_file(path) end output_result(errors, @files_checked) end |