class HamlLint::FileFinder
Finds Haml files that should be linted given a specified list of paths, glob patterns, and configuration.
Constants
- VALID_EXTENSIONS
List of extensions of files to include under a directory when a directory is specified instead of a file.
Public Class Methods
Create a file finder using the specified configuration.
@param config [HamlLint::Configuration]
# File lib/haml_lint/file_finder.rb, line 15 def initialize(config) @config = config end
Public Instance Methods
Return list of files to lint given the specified set of paths and glob patterns. @param patterns [Array<String>] @param excluded_patterns [Array<String>] @raise [HamlLint::Exceptions::InvalidFilePath] @return [Array<String>] list of actual files
# File lib/haml_lint/file_finder.rb, line 25 def find(patterns, excluded_patterns) excluded_patterns = excluded_patterns.map { |pattern| normalize_path(pattern) } extract_files_from(patterns).reject do |file| excluded_patterns.any? do |exclusion_glob| HamlLint::Utils.any_glob_matches?(exclusion_glob, file) end end end
Private Instance Methods
Extract the list of matching files given the list of glob patterns, file paths, and directories.
@param patterns [Array<String>] @return [Array<String>]
# File lib/haml_lint/file_finder.rb, line 42 def extract_files_from(patterns) # rubocop:disable Metrics/MethodLength files = [] patterns.each do |pattern| if File.file?(pattern) files << pattern else begin ::Find.find(pattern) do |file| files << file if haml_file?(file) end rescue ::Errno::ENOENT # File didn't exist; it might be a file glob pattern matches = ::Dir.glob(pattern) if matches.any? files += matches else # One of the paths specified does not exist; raise a more # descriptive exception so we know which one raise HamlLint::Exceptions::InvalidFilePath, "File path '#{pattern}' does not exist" end end end end files.uniq.sort.map { |file| normalize_path(file) } end
Whether the given file should be treated as a Haml file.
@param file [String] @return [Boolean]
# File lib/haml_lint/file_finder.rb, line 83 def haml_file?(file) return false unless ::FileTest.file?(file) VALID_EXTENSIONS.include?(::File.extname(file)) end
Trim “./” from the front of relative paths.
@param path [String] @return [String]
# File lib/haml_lint/file_finder.rb, line 75 def normalize_path(path) path.start_with?(".#{File::SEPARATOR}") ? path[2..-1] : path end