Module: CodeLister

Defined in:
lib/code_lister/cli.rb,
lib/code_lister/main.rb,
lib/code_lister/version.rb,
lib/code_lister/code_lister.rb

Defined Under Namespace

Classes: CLI, Main

Constant Summary collapse

VERSION =
"0.2.6"
CustomError =
Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.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

.files_from_command(command) ⇒ Array<String>

Execute the command in the shell and extract the output to be used

Example

Use the file list from the ‘find | grep’ command

‘find ~/Desktop/pdfkit -type f -iname “*.rb” | grep -v spec’

Parameters:

  • command (String)

    the input command to be executed in the shell

Returns:

  • (Array<String>)

    file list or empty list if the shell command is not valid



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/code_lister/code_lister.rb', line 14

def files_from_command(command)
  files = AgileUtils::Helper.shell(command.split(" ")).split(/\n/)
  files.map! { |file| File.expand_path(file) }
  # Some command result in the deleted files (e.g. git diff --name-only HEAD~n)
  files.delete_if do |file|
    !File.exist?(file)
  end
  files
rescue RuntimeError
  # return empty list for invalid command
  return []
end

.files_from_shell(command, prefix) ⇒ Array<String>

Extract list of files from a given shell command

Parameters:

  • command (String)

    the input command e.g. ‘find . -type f -iname ..“

  • prefix (String)

    the prefix directory that will be replaced by ‘.’ (dot string)

Returns:

  • (Array<String>)

    empty list or file list of the form [“./Gemfile”, “./lib/codelister.rb”, ..]



32
33
34
35
36
37
38
39
# File 'lib/code_lister/code_lister.rb', line 32

def files_from_shell(command, prefix)
  prefix = File.expand_path(prefix) if prefix
  unless prefix && File.directory?(prefix)
    fail "'#{prefix}' is not valid prefix directory!"
  end
  files = files_from_command(command)
  files.map! { |file| file.gsub(File.expand_path(prefix), ".") }
end

.filter(file_list, args = {}) ⇒ Array<String>

Filter out the list based on simple criteria

Parameters:

  • file_list (Array<String>)

    the input file list

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

    the option hash

Options Hash (args):

  • :inc_words (Array<String>)

    list of words that must be match to be included in the result

  • :exc_words (Array<String>)

    list of words that will be excluded fromt the result

  • :ignore_case (Boolean)

    flag to indicate how the string comparision should be performed

Returns:

  • (Array<String>)

    the original list with the result filtered



80
81
82
83
84
85
86
87
88
89
# File 'lib/code_lister/code_lister.rb', line 80

def filter(file_list, args = {})
  opts = {
    inc_words: [],
    exc_words: [],
    ignore_case: true
  }.merge(args)
  take_any!(file_list, opts)
  drop_any!(file_list, opts)
  file_list
end

.remove_prefix(files, prefix) ⇒ Array<String>

Remove each prefix string from a given list of string

Parameters:

  • files (Array<String>)

    list of file path/name

  • base_dir (String)

Returns:

  • (Array<String>)

    list of files with the prefix replaced by “.”



96
97
98
99
100
# File 'lib/code_lister/code_lister.rb', line 96

def remove_prefix(files, prefix)
  prefix = File.expand_path(prefix) if prefix
  files.map! { |file| prefix ? file.gsub(prefix, ".") : file }
  files
end