lib/reek/source/source_locator.rb in reek-2.2.1 vs lib/reek/source/source_locator.rb in reek-3.0.0
- old
+ new
@@ -1,10 +1,13 @@
+require 'find'
+
module Reek
module Source
#
# Finds Ruby source files in a filesystem.
#
+ # @api private
class SourceLocator
# Initialize with the paths we want to search.
#
# paths - a list of paths as Strings
def initialize(paths)
@@ -12,27 +15,42 @@
end
# Traverses all paths we initialized the SourceLocator with, finds
# all relevant ruby files and returns them as a list.
#
- # Returns a list of Source::SourceCode.
+ # @return [Array<File>] - Ruby files found
def sources
- find_sources.map { |pathname| Source::SourceCode.from File.new(pathname) }
+ source_paths.map { |pathname| File.new(pathname) }
end
private
- def find_sources(paths = @paths)
- paths.map do |path|
- pathname = Pathname.new(path)
- if pathname.directory?
- find_sources(Dir["#{pathname}/**/*.rb"])
- else
- next pathname if pathname.file?
- $stderr.puts "Error: No such file - #{pathname}"
- nil
+ def source_paths
+ relevant_paths = []
+ @paths.map do |given_path|
+ print_no_such_file_error(given_path) && next unless path_exists?(given_path)
+ Find.find(given_path) do |path|
+ pathname = Pathname.new(path)
+ if pathname.directory?
+ exclude_path?(pathname) ? Find.prune : next
+ else
+ relevant_paths << pathname
+ end
end
- end.flatten.sort
+ end
+ relevant_paths.flatten.sort
+ end
+
+ def exclude_path?(pathname)
+ Configuration::AppConfiguration.exclude_paths.include? pathname.to_s
+ end
+
+ def path_exists?(path)
+ Pathname.new(path).exist?
+ end
+
+ def print_no_such_file_error(path)
+ $stderr.puts "Error: No such file - #{path}"
end
end
end
end