Method: CodeLister.files

Defined in:
lib/code_lister/code_lister.rb

.files(args = {}) ⇒ Array<String>

List files base on multiple simple criteria

Parameters:

  • args (Hash<Symbol>, <Object>) (defaults to: {})

    argument hash

Options Hash (args):

  • :base_dir (String)

    the starting directory

  • :recursive (Boolean)

    flag to indicate if the search will be done recursively

  • :exts (Array<String>)

    list of file extension to search for (without the dot)

  • :non_exts (Array<String>)

    list of file without any extension to search for

Returns:

  • (Array<String>)

    the list of file based on the matching criteria



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/code_lister/code_lister.rb', line 49

def files(args = {})
  opts = {
    base_dir:  Dir.pwd,
    recursive: true,
    exts:      [],
    non_exts:  []
  }.merge(args)

  # always expand the path
  base_dir = File.expand_path(opts[:base_dir])
  fail CustomError, "The directory #{base_dir} is not valid or or not readable!" unless File.exist?(base_dir)

  wildcard = opts[:recursive] ? "**" : ""
  exts     = opts[:exts]
  non_exts = opts[:non_exts]

  files_with_extension    = Dir.glob(File.join(base_dir, wildcard, "*.{#{exts.join(",")}}"))
  files_without_extension = no_extension_files(base_dir, wildcard, non_exts)
  # Replace prefix directory with just "."
  files = (files_with_extension + files_without_extension)
  files.map! { |file| file.gsub(base_dir, ".") }.sort
end