Class: Roodi::Core::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/roodi/core/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

  • value

    the value to set the attribute config to.


11
12
13
# File 'lib/roodi/core/runner.rb', line 11

def config=(value)
  @config = value
end

#files_checkedObject (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_configObject


19
20
21
# File 'lib/roodi/core/runner.rb', line 19

def default_config
  project_config ? project_config : roodi_gem_config
end

#errorsObject


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

88
89
90
91
# File 'lib/roodi/core/runner.rb', line 88

def print(filename, content)
  node = parse(filename, content)
  pp node
end

93
94
95
# File 'lib/roodi/core/runner.rb', line 93

def print_content(content)
  print("dummy-file.rb", content)
end

97
98
99
# File 'lib/roodi/core/runner.rb', line 97

def print_file(filename)
  print(filename, File.read(filename))
end

#project_configObject


27
28
29
# File 'lib/roodi/core/runner.rb', line 27

def project_config
  File.exists?("roodi.yml") ? "roodi.yml" : nil
end

#roodi_gem_configObject


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