Module: Polytrix::Core::FileSystemHelper

Defined Under Namespace

Classes: FileNotFound

Instance Method Summary collapse

Methods included from StringHelpers

included

Methods included from StringHelpers::ClassMethods

#slugify

Instance Method Details

#find_file(search_path, scenario_name, ignored_patterns = read_gitignore(search_path)) ⇒ Object

Finds a file by loosely matching the file name to a scenario name



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/polytrix/core/file_system_helper.rb', line 9

def find_file(search_path, scenario_name, ignored_patterns = read_gitignore(search_path))
  glob_string = "#{search_path}/**/*#{slugify(scenario_name)}.*"
  potential_files = Dir.glob(glob_string, File::FNM_CASEFOLD)
  potential_files.concat Dir.glob(glob_string.gsub('_', '-'), File::FNM_CASEFOLD)
  potential_files.concat Dir.glob(glob_string.gsub('_', ''), File::FNM_CASEFOLD)

  # Filter out ignored filesFind the first file, not including generated files
  files = potential_files.select do |f|
    !ignored? ignored_patterns, search_path, f
  end

  # Select the shortest path, likely the best match
  file = files.min_by(&:length)

  fail FileNotFound, "No file was found for #{scenario_name} within #{search_path}" if file.nil?
  Pathname.new file
end

#recursive_parent_search(path, file_name = nil, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/polytrix/core/file_system_helper.rb', line 27

def recursive_parent_search(path, file_name = nil, &block)
  if block_given?
    obj = yield path
    return obj if obj
  elsif file_name
    file = File.expand_path(file_name, path)
    logger.debug "Checking for #{file}"
    found = File.exists? file
  else
    fail ArgumentError, 'Provide either a file_name to search for, or a block to check directories'
  end

  parent_dir = File.dirname(path)
  return path if found
  return nil if parent_dir == path # we've reached the top
  recursive_parent_search(parent_dir, file_name, &block)
end