class I18n::Tasks::Scanners::Files::FileFinder

Finds the files in the specified search paths with support for exclusion / inclusion patterns.

@since 0.9.0

Public Class Methods

new(paths: ['.'], only: nil, exclude: []) click to toggle source

@param paths [Array<String>] {Find.find}-compatible paths to traverse,

absolute or relative to the working directory.

@param only [Array<String>, nil] {File.fnmatch}-compatible patterns files to include.

Files not matching any of the inclusion patterns will be excluded.

@param exclude [Arry<String>] {File.fnmatch}-compatible patterns of files to exclude.

Files matching any of the exclusion patterns will be excluded even if they match an inclusion pattern.
# File lib/i18n/tasks/scanners/files/file_finder.rb, line 16
def initialize(paths: ['.'], only: nil, exclude: [])
  fail 'paths argument is required' if paths.nil?

  @paths   = paths
  @include = only
  @exclude = exclude || []
end

Public Instance Methods

find_files() click to toggle source

@return [Array<String>] found files

# File lib/i18n/tasks/scanners/files/file_finder.rb, line 34
def find_files # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  results = []
  paths = @paths.select { |p| File.exist?(p) }
  log_warn "None of the search.paths exist #{@paths.inspect}" if paths.empty?
  Find.find(*paths) do |path|
    is_dir   = File.directory?(path)
    hidden   = File.basename(path).start_with?('.') && !%w[. ./].include?(path)
    not_incl = @include && !path_fnmatch_any?(path, @include)
    excl     = path_fnmatch_any?(path, @exclude)
    if is_dir || hidden || not_incl || excl
      Find.prune if is_dir && (hidden || excl)
    else
      results << path
    end
  end
  results
end
traverse_files(&block) click to toggle source

Traverse the paths and yield the matching ones.

@yield [path] @yieldparam path [String] the path of the found file. @return [Array<of block results>]

# File lib/i18n/tasks/scanners/files/file_finder.rb, line 29
def traverse_files(&block)
  find_files.map(&block)
end

Private Instance Methods

path_fnmatch_any?(path, globs) click to toggle source

@param path [String] @param globs [Array<String>] @return [Boolean]

# File lib/i18n/tasks/scanners/files/file_finder.rb, line 57
def path_fnmatch_any?(path, globs)
  globs.any? { |glob| File.fnmatch(glob, path) }
end