Advertisement
Guest User

claude_context.sh

a guest
Sep 20th, 2024
577
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.55 KB | None | 1 0
  1. #!/bin/bash
  2.  
  3. # Default directory to list
  4. dir="."
  5. # Array to hold directories to ignore
  6. ignore_dirs=(".git" ".vscode" "node_modules" "coverage" "dist" "docker" "vendor" "logs" "volume" "build" "var")
  7. # Array to hold file names to ignore
  8. ignore_files=("package-lock.json" "composer.lock" ".env" "config/bundles.php")
  9. dirs_only=false
  10. specific_files=()
  11.  
  12. # Function to display script usage
  13. usage() {
  14.     echo "Usage: $0 [-i <dir_to_ignore>] [-f <file_to_ignore>] [-s <specific_file>] [-d] [-h]"
  15.     echo "Options:"
  16.     echo "  -i    Specify a directory to ignore (can be used multiple times)"
  17.     echo "  -f    Specify a file name to ignore (can be used multiple times)"
  18.     echo "  -s    Specify a file to list contents of (can be used multiple times)"
  19.     echo "  -d    Only list directories"
  20.     echo "  -h    Display this help message and exit"
  21. }
  22.  
  23. # Parsing command line options
  24. while getopts ":i:f:s:dh" opt; do
  25.   case $opt in
  26.     i) ignore_dirs+=("$OPTARG") ;;
  27.     f) ignore_files+=("$OPTARG") ;;
  28.     s) specific_files+=("$OPTARG") ;;
  29.     d) dirs_only=true ;;
  30.     h) usage; exit 0 ;;
  31.     \?) echo "Invalid option: -$OPTARG" >&2; usage; exit 1 ;;
  32.   esac
  33. done
  34.  
  35. # Check if we should ignore/omit a directory or file
  36. should_ignore() {
  37.     for ignore in "${ignore_dirs[@]}"; do
  38.         [[ $1 == *"$ignore"* ]] && return 0
  39.     done
  40.  
  41.     for ignore_file in "${ignore_files[@]}"; do
  42.         [[ $(basename "$1") == "$ignore_file" ]] && return 0
  43.     done
  44.  
  45.     [[ $(basename "$1") == "context.txt" ]] && return 0
  46.     [[ $(file --mime-encoding -b "$1") == "binary" ]] && return 0
  47.  
  48.     return 1
  49. }
  50.  
  51. # Function to list file contents in XML format
  52. list_file_contents() {
  53.     local file_path="$1"
  54.     echo "  <document index=\"$document_index\" type=\"file\">"
  55.     echo "    <source>$file_path</source>"
  56.     echo "    <document_content><![CDATA["
  57.     cat "$file_path"
  58.     echo ""
  59.     echo "]]></document_content>"
  60.     echo "  </document>"
  61.     ((document_index++))
  62. }
  63.  
  64. # Function to generate tree structure using the 'tree' command
  65. generate_tree_structure() {
  66.     local current_dir=$1
  67.     local ignore_pattern=$(IFS='|'; echo "${ignore_dirs[*]}")
  68.  
  69.     if [ "$dirs_only" = true ]; then
  70.         tree -d -I "$ignore_pattern" "$current_dir"
  71.     else
  72.         tree -I "$ignore_pattern" "$current_dir"
  73.     fi
  74. }
  75.  
  76.  
  77. # Main function to list directories and files
  78. list_dir_contents() {
  79.     local current_dir=$1
  80.  
  81.     echo "<documents>"
  82.  
  83.     # Output tree structure
  84.     echo "  <document index=\"0\" type=\"directory_structure\">"
  85.     echo "    <source>tree_structure</source>"
  86.     echo "    <document_content><![CDATA["
  87.     echo "$current_dir"
  88.     generate_tree_structure "$current_dir"
  89.     echo "]]></document_content>"
  90.     echo "  </document>"
  91.  
  92.     # If we only want to list directories, return
  93.     [ "$dirs_only" = true ] && echo "</documents>" && return
  94.  
  95.     # If specific files are provided, only list their contents
  96.     if [ ${#specific_files[@]} -gt 0 ]; then
  97.         for file in "${specific_files[@]}"; do
  98.             if [ -f "$file" ] && ! should_ignore "$file"; then
  99.                 list_file_contents "$file"
  100.             fi
  101.         done
  102.         echo "</documents>"
  103.         return
  104.     fi
  105.  
  106.     # Loop through all files
  107.     while IFS= read -r -d $'\0' file; do
  108.         if [ -f "$file" ] && ! should_ignore "$file"; then
  109.             list_file_contents "$file"
  110.         fi
  111.     done < <(find "$current_dir" -type f -print0)
  112.  
  113.     echo "</documents>"
  114. }
  115.  
  116. # Initialize document index
  117. document_index=1
  118.  
  119. # Main execution
  120. list_dir_contents "$dir"
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement