lib/fasterer/file_traverser.rb in fasterer-0.2.1 vs lib/fasterer/file_traverser.rb in fasterer-0.3.0
- old
+ new
@@ -1,8 +1,9 @@
require 'pathname'
require 'colorize'
require 'English'
+require 'pry'
require_relative 'analyzer'
require_relative 'config'
module Fasterer
@@ -14,23 +15,19 @@
attr_reader :config
attr_reader :parse_error_paths
attr_accessor :offenses_total_count
def initialize(path)
- @path = Pathname(path)
+ @path = Pathname(path || '.')
@parse_error_paths = []
@config = Config.new
@offenses_total_count = 0
end
def traverse
- if @path.directory?
- scannable_files.each { |ruby_file| scan_file(ruby_file) }
- else
- scan_file(@path)
- end
- output_parse_errors if parse_error_paths.any?
+ traverse_files
+ output_parse_errors
output_statistics
end
def config_file
config.file
@@ -46,10 +43,18 @@
private
attr_accessor :offenses_found
+ def traverse_files
+ if @path.exist?
+ scannable_files.each { |ruby_file| scan_file(ruby_file) }
+ else
+ output_unable_to_find_file(@path)
+ end
+ end
+
def scan_file(path)
analyzer = Analyzer.new(path)
analyzer.scan
rescue RubyParser::SyntaxError, Racc::ParseError, Timeout::Error => e
parse_error_paths.push(ErrorData.new(path, e.class, e.message).to_s)
@@ -60,15 +65,23 @@
self.offenses_total_count += analyzer.errors.count
end
end
def all_files
- Dir["#{@path}/**/*.rb"].map do |ruby_file_path|
- Pathname(ruby_file_path).relative_path_from(@path).to_s
+ if @path.directory?
+ Dir[File.join(@path, '**', '*.rb')].map do |ruby_file_path|
+ Pathname(ruby_file_path).relative_path_from(root_dir).to_s
+ end
+ else
+ [@path.to_s]
end
end
+ def root_dir
+ @root_dir ||= Pathname('.')
+ end
+
def output(analyzer)
puts analyzer.file_path.colorize(:red)
offenses_grouped_by_type(analyzer).each do |error_group_name, error_occurences|
puts "#{Fasterer::Offense::EXPLANATIONS[error_group_name]}."\
@@ -83,19 +96,25 @@
ignored_speedups.include?(offense_name)
end
end
def output_parse_errors
+ return if parse_error_paths.none?
+
puts 'Fasterer was unable to process some files because the'
puts 'internal parser is not able to read some characters or'
puts 'has timed out. Unprocessable files were:'
puts '-----------------------------------------------------'
puts parse_error_paths
puts
end
def output_statistics
puts Statistics.new(self)
+ end
+
+ def output_unable_to_find_file(path)
+ puts "No such file or directory - #{path}".colorize(:red)
end
def ignored_speedups
config.ignored_speedups
end