forked from roodi/roodi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.rb
77 lines (63 loc) · 1.83 KB
/
runner.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
require 'pp'
require 'yaml'
require 'roodi/core/checking_visitor'
require 'roodi/core/parser'
require 'roodi/core/visitable_sexp'
module Roodi
module Core
class Runner
DEFAULT_CONFIG = File.join(File.dirname(__FILE__), "..", "..", "..", "roodi.yml")
attr_writer :config
def initialize(*checks)
@config = DEFAULT_CONFIG
@checks = checks unless checks.empty?
@parser = Parser.new
end
def check(filename, content)
@checks ||= load_checks
node = parse(filename, content)
node.accept(CheckingVisitor.new(@checks)) if node
end
def check_content(content)
check("dummy-file.rb", content)
end
def check_file(filename)
check(filename, File.read(filename))
end
def print(filename, content)
node = @parser.parse(content, filename)
puts "Line: #{node.line}"
pp node
end
def print_content(content)
print("dummy-file.rb", content)
end
def print_file(filename)
print(filename, File.read(filename))
end
def errors
@checks ||= []
all_errors = @checks.collect {|check| check.errors}
all_errors.flatten
end
private
def parse(filename, content)
begin
@parser.parse(content, filename)
rescue Exception => e
puts "#{filename} looks like it's not a valid Ruby file. Skipping..." if ENV["ROODI_DEBUG"]
nil
end
end
def load_checks
check_objects = []
checks = YAML.load_file @config
checks.each do |check|
klass = eval("Roodi::Checks::#{check[0]}")
check_objects << (check[1].empty? ? klass.new : klass.new(check[1]))
end
check_objects
end
end
end
end