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

new(config) click to toggle source

Create a file finder using the specified configuration.

@param config [HamlLint::Configuration]

# File lib/haml_lint/file_finder.rb, line 16
def initialize(config)
  @config = config
end

Public Instance Methods

find(patterns, excluded_patterns) click to toggle source

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 26
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_files_from(patterns) click to toggle source

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 43
def extract_files_from(patterns) # rubocop:disable Metrics
  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
haml_file?(file) click to toggle source

Whether the given file should be treated as a Haml file.

@param file [String] @return [Boolean]

# File lib/haml_lint/file_finder.rb, line 84
def haml_file?(file)
  return false unless ::FileTest.file?(file)

  VALID_EXTENSIONS.include?(::File.extname(file))
end
normalize_path(path) click to toggle source

Trim “./” from the front of relative paths.

@param path [String] @return [String]

# File lib/haml_lint/file_finder.rb, line 76
def normalize_path(path)
  path.start_with?(".#{File::SEPARATOR}") ? path[2..] : path
end