Last active
July 25, 2019 16:23
-
-
Save joekrump/5a1961eea2dc54851f2fa66fec5b5656 to your computer and use it in GitHub Desktop.
Get the file names of files matching an extension, within a directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
directory = ARGV[0] || './client-src' | |
file_extension = ARGV[1] || '.coffee' | |
def get_matching_files_in(directory, file_extension) | |
manage_ui_files = [] | |
accounting_files = [] | |
payment_files = [] | |
billing_files = [] | |
other_files = [] | |
Dir.glob("#{directory}/**/*/*#{file_extension}") do |file| | |
if file.index('apps/_common/ui/') | |
manage_ui_files.push(file[3..-1]) | |
elsif file.index('apps/main/bank-accounts/') || | |
file.index('apps/main/bank-transactions/') | |
accounting_files.push(file[3..-1]) | |
elsif file.index('apps/main/bills/payments/') | |
payment_files.push(file[3..-1]) | |
elsif file.index('apps/main/bills/') | |
billing_files.push(file[3..-1]) | |
else | |
other_files.push(file[3..-1]) | |
end | |
end | |
{ | |
manage_ui_files: manage_ui_files, | |
accounting_files: accounting_files, | |
payment_files: payment_files, | |
billing_files: billing_files, | |
other: other_files | |
} | |
end | |
def output_file_paths(files_by_directory) | |
puts "Files by Section\n" | |
files_by_directory.each do |file_directory, files| | |
puts "\n#{file_directory}\n\n" | |
files.each do |file| | |
puts file.to_s | |
end | |
end | |
end | |
def output_summary(files_by_directory, directory, file_extension) | |
total_file_count = 0 | |
puts "\nSummary\n\n" | |
files_by_directory.each do |file_directory, files| | |
print file_directory.to_s.ljust(20) | |
total_file_count += files.length | |
end | |
puts "\n" | |
files_by_directory.each do |_file_directory, files| | |
print files.length.to_s.ljust(20) | |
end | |
# Summary | |
puts "\n\n#{total_file_count} #{file_extension} files in #{directory}" | |
end | |
# Files Output | |
def output_results(directory, file_extension) | |
files_by_directory = get_matching_files_in(directory, file_extension) | |
output_file_paths(files_by_directory) | |
output_summary(files_by_directory, directory, file_extension) | |
end | |
output_results(directory, file_extension) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment