Skip to content

Instantly share code, notes, and snippets.

@BrianSigafoos
Last active January 15, 2025 15:42
Show Gist options
  • Save BrianSigafoos/92b71811a532b760f197b4e98f5a5815 to your computer and use it in GitHub Desktop.
Save BrianSigafoos/92b71811a532b760f197b4e98f5a5815 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "fileutils"
require "tempfile"
# Function to display usage information and exit
def usage
puts <<~USAGE
Usage: #{File.basename(__FILE__)} [directory_path] [file_extension] [--no-subdirs]
- directory_path: Optional. Defaults to 'app/services/scheduling' if not provided.
- file_extension: Optional. Specify the file extension to filter files (e.g., .rb, .js).
- --no-subdirs: Optional. Do not include files from subdirectories.
USAGE
exit 1
end
# Function to process a file and append to the temporary file
def process_file(file, temp_file, strip_schema)
temp_file.puts "-------"
temp_file.puts "# #{file}"
temp_file.puts "\n"
File.open(file, "r") do |f|
skip = false
in_schema = false
f.each_line do |line|
line.chomp!
if skip
if line.strip.empty?
skip = false
end
next
end
# Write the processed line to the temp file
temp_file.puts line
end
end
temp_file.puts "\n"
end
# Default directory if none is provided
DEFAULT_DIRECTORY = "."
# Initialize variables
directory = DEFAULT_DIRECTORY
extension = ""
strip_schema = false
no_subdirs = false
# Argument parsing
args = ARGV.dup
# Check if the first argument is a directory
if args.any? && Dir.exist?(args.first)
directory = args.shift
end
# Parse the remaining arguments
args.each do |arg|
case arg
when "--no-subdirs"
no_subdirs = true
when /^\.{1,2}\/?.+/
extension = arg
else
# Assume it's a file extension if it starts with a dot
if arg.start_with?(".")
extension = arg
else
puts "Unknown option: #{arg}"
usage
end
end
end
# Validate directory existence
unless Dir.exist?(directory)
puts "The directory '#{directory}' does not exist."
exit 1
end
# Create a temporary file to store the output
Tempfile.create("gather_files") do |temp|
# First, check if README.md exists at the root and process it
readme_file = File.join(directory, "README.md")
process_file(readme_file, temp, strip_schema) if File.file?(readme_file)
# Gather files based on the provided options
pattern = extension.empty? ? "*" : "*#{extension}"
find_options = no_subdirs ? "#{directory}/*" : "#{directory}/**/#{pattern}"
Dir.glob(find_options).each do |file|
next if File.directory?(file)
next if File.basename(file) == "README.md"
# If extension is specified, ensure the file matches
if !extension.empty? && File.extname(file) != extension
next
end
process_file(file, temp, strip_schema)
end
# Rewind the temp file to read its content
temp.rewind
content = temp.read
# Copy the gathered content to the clipboard using pbcopy
IO.popen("pbcopy", "w") { |io| io.write(content) }
puts "File contents copied to clipboard."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment