Skip to content

Instantly share code, notes, and snippets.

@t2
Created January 24, 2025 23:23
Show Gist options
  • Save t2/a51989d20746857d1ce571e59bbdc8e1 to your computer and use it in GitHub Desktop.
Save t2/a51989d20746857d1ce571e59bbdc8e1 to your computer and use it in GitHub Desktop.
Consolidate Rails Project Code
require 'fileutils'
# Add the folders you want to consolidate the files from.
TARGETS_TO_CONSOLIDATE = %w[
app/controllers
app/models
app/commands
app/helpers
app/jobs
app/notifiers
app/policies
app/services
app/components
app/views
app/javascript
app/graphql
spec
]
SPECIFIC_FILES = %w[db/schema.rb]
FILE_EXTENSIONS = %w[.rb .js .erb]
OUTPUT_DIR = "output"
FileUtils.mkdir_p(OUTPUT_DIR)
def consolidate_folder(folder_path)
folder_name = File.basename(folder_path)
output_file = File.join(OUTPUT_DIR, "#{folder_name}.txt")
File.open(output_file, 'w') {}
return unless Dir.exist?(folder_path)
Dir.glob(File.join(folder_path, '**', '*')).each do |file|
next unless FILE_EXTENSIONS.include?(File.extname(file))
File.open(output_file, 'a') do |output|
output.puts "===== #{file} ====="
output.puts File.read(file)
output.puts "\n\n"
end
end
end
def consolidate_file(file_path)
return unless File.exist?(file_path)
file_name = File.basename(file_path, '.*') + '.txt'
output_file = File.join(OUTPUT_DIR, file_name)
File.open(output_file, 'w') do |output|
output.puts "===== #{file_path} ====="
output.puts File.read(file_path)
end
end
TARGETS_TO_CONSOLIDATE.each { |folder| consolidate_folder(folder) }
SPECIFIC_FILES.each { |file| consolidate_file(file) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment