Class: I18n::Processes::Scanners::Files::FileFinder

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/i18n/processes/scanners/files/file_finder.rb

Overview

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

Since:

  • 0.9.0

Direct Known Subclasses

CachingFileFinder

Constant Summary

Constants included from Logging

Logging::MUTEX, Logging::PROGRAM_NAME

Instance Method Summary collapse

Methods included from Logging

log_error, log_stderr, log_verbose, log_warn, program_name, warn_deprecated

Constructor Details

#initialize(paths: ['.'], only: nil, exclude: []) ⇒ FileFinder

Returns a new instance of FileFinder.

Parameters:

  • paths (Array<String>) (defaults to: ['.'])

    Find.find-compatible paths to traverse, absolute or relative to the working directory.

  • only (Array<String>, nil) (defaults to: nil)

    File.fnmatch-compatible patterns files to include. Files not matching any of the inclusion patterns will be excluded.

  • exclude (Arry<String>) (defaults to: [])

    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.

Since:

  • 0.9.0



16
17
18
19
20
21
# File 'lib/i18n/processes/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

Instance Method Details

#find_filesArray<String>

Returns found files.

Returns:

  • (Array<String>)

    found files

Since:

  • 0.9.0



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/i18n/processes/scanners/files/file_finder.rb', line 33

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 {|path| ... } ⇒ Array<of block results>

Traverse the paths and yield the matching ones.

Yields:

  • (path)

Yield Parameters:

  • path (String)

    the path of the found file.

Returns:

  • (Array<of block results>)

Since:

  • 0.9.0



28
29
30
# File 'lib/i18n/processes/scanners/files/file_finder.rb', line 28

def traverse_files
  find_files.map { |path| yield path }
end