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
Constant Summary collapse
- VERSION =
"0.2.6"
- CustomError =
Class.new(StandardError)
Class Method Summary collapse
-
.files(args = {}) ⇒ Array<String>
List files base on multiple simple criteria.
-
.files_from_command(command) ⇒ Array<String>
Execute the command in the shell and extract the output to be used.
-
.files_from_shell(command, prefix) ⇒ Array<String>
Extract list of files from a given shell command.
-
.filter(file_list, args = {}) ⇒ Array<String>
Filter out the list based on simple criteria.
-
.remove_prefix(files, prefix) ⇒ Array<String>
Remove each prefix string from a given list of string.
Class Method Details
.files(args = {}) ⇒ Array<String>
List files base on multiple simple 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.(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’
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.(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
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.(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.(prefix), ".") } end |
.filter(file_list, args = {}) ⇒ Array<String>
Filter out the list based on simple criteria
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
96 97 98 99 100 |
# File 'lib/code_lister/code_lister.rb', line 96 def remove_prefix(files, prefix) prefix = File.(prefix) if prefix files.map! { |file| prefix ? file.gsub(prefix, ".") : file } files end |