Module: CodeLister
- Defined in:
- lib/code_lister/cli.rb,
lib/code_lister/main.rb,
lib/code_lister/logger.rb,
lib/code_lister/version.rb,
lib/code_lister/code_lister.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
"0.1.7"
- CustomError =
Class.new(StandardError)
Class Attribute Summary collapse
-
.logger ⇒ Logger
The Logger for the project.
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 Attribute Details
.logger ⇒ Logger
Returns the Logger for the project.
6 7 8 |
# File 'lib/code_lister/logger.rb', line 6 def logger @logger ||= Logger.new STDOUT end |
Class Method Details
.files(args = {}) ⇒ Array<String>
List files base on multiple simple criteria
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/code_lister/code_lister.rb', line 50 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’
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/code_lister/code_lister.rb', line 15 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 => e # return empty list for invalid command return [] end |
.files_from_shell(command, prefix) ⇒ Array<String>
Extract list of files from a given shell command
33 34 35 36 37 38 39 40 |
# File 'lib/code_lister/code_lister.rb', line 33 def files_from_shell(command, prefix) prefix = File.(prefix) if prefix unless prefix && File.directory?(prefix) raise "'#{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
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/code_lister/code_lister.rb', line 81 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
97 98 99 100 101 |
# File 'lib/code_lister/code_lister.rb', line 97 def remove_prefix(files, prefix) prefix = File.(prefix) if prefix files.map! { |file| prefix ? file.gsub(prefix, ".") : file } files end |