Simple reporter

This commit is contained in:
Vitalii Elenhaupt 2017-10-26 22:45:48 +03:00
parent 4e84ac871a
commit ffd44dc77b
No known key found for this signature in database
GPG Key ID: 7558EF3A4056C706
2 changed files with 56 additions and 5 deletions

View File

@ -8,14 +8,20 @@ module Ameba
Rule::LineLength,
]
def run
run Dir["**/*.cr"]
def run(formatter = DotFormatter.new)
run Dir["**/*.cr"], formatter
end
def run(files)
files.each do |path|
catch Source.new(File.read path)
def run(files, formatter : Formatter)
sources = files.map { |path| Source.new(File.read path) }
reporter = Reporter.new formatter
reporter.start sources
sources.each do |source|
catch(source)
reporter.report source
end
reporter.try &.finish sources
end
def catch(source : Source)

45
src/ameba/formatter.cr Normal file
View File

@ -0,0 +1,45 @@
module Ameba
abstract class Formatter
abstract def before(sources)
abstract def format(source : Source)
abstract def after(sources)
end
class Reporter
property formatter : Formatter
def initialize(@formatter : Formatter)
end
def start(sources)
puts formatter.before sources
end
def report(source)
print formatter.format source
end
def finish(sources)
puts
puts formatter.after sources
end
end
class DotFormatter < Formatter
def before(sources)
if (len = sources.size) == 1
"Inspecting 1 file."
else
"Inspecting #{len} files."
end
end
def format(source : Source)
source.errors.size == 0 ? "." : "F"
end
def after(sources)
"Done!"
end
end
end