Skip to content

Instantly share code, notes, and snippets.

@pshapiro
Created September 6, 2024 13:16
Show Gist options
  • Save pshapiro/b0cf0b6cede48934e71501f7d5e17a35 to your computer and use it in GitHub Desktop.
Save pshapiro/b0cf0b6cede48934e71501f7d5e17a35 to your computer and use it in GitHub Desktop.
Compile project files into a single txt for LLM context to help with debugging
import os
import fnmatch
def read_gitignore(directory):
gitignore_path = os.path.join(directory, '.gitignore')
if os.path.exists(gitignore_path):
with open(gitignore_path, 'r') as file:
return file.read().splitlines()
return []
def is_excluded(file_path, gitignore_patterns):
for pattern in gitignore_patterns:
if fnmatch.fnmatch(file_path, pattern):
return True
return False
def is_license_file(file_name):
license_names = ["LICENSE", "LICENSE.txt", "LICENSE.md"]
return file_name in license_names
def is_excluded_directory(dir_name):
excluded_dirs = ["rankenberry", "node_modules", ".git"]
return dir_name in excluded_dirs
def compile_files(directory, output_file):
gitignore_patterns = read_gitignore(directory)
with open(output_file, 'w', encoding='latin-1') as outfile:
for root, dirs, files in os.walk(directory):
dirs[:] = [d for d in dirs if not is_excluded_directory(d)]
for file in files:
file_path = os.path.join(root, file)
relative_path = os.path.relpath(file_path, directory)
if not is_excluded(relative_path, gitignore_patterns) and not is_license_file(file):
# Write file path and name as a header
outfile.write(f"FILE: {file_path}\n")
outfile.write("=" * (len(file_path) + 6) + "\n")
# Write file contents
with open(file_path, 'r', encoding='latin-1') as infile:
outfile.write(infile.read())
outfile.write("\n\n")
# Specify the project directory and output file name
project_directory = "/Users/paul/Documents/Development/rankenberry/"
output_file = "compiled_project.txt"
# Call the function to compile files
compile_files(project_directory, output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment