Skip to content

Instantly share code, notes, and snippets.

@ehzawad
Created February 24, 2025 16:45
Show Gist options
  • Save ehzawad/cc30ffe1d6ff388d3ffa8a9654a0d25e to your computer and use it in GitHub Desktop.
Save ehzawad/cc30ffe1d6ff388d3ffa8a9654a0d25e to your computer and use it in GitHub Desktop.
Merge All Python Files
find . -name "*.py" | while IFS= read -r file; do
echo "===== $file =====" >> merged.txt
cat "$file" >> merged.txt
echo -e "\n" >> merged.txt
done
@ehzawad
Copy link
Author

ehzawad commented Mar 7, 2025

compile_files() {
  if [ $# -ne 2 ]; then
    echo "Usage: compile_files OUTPUT_FILE EXTENSION"
    echo "Example: compile_files combined.txt py"
    return 1
  fi

  # Get absolute path of output file to ensure reliable comparison
  local output_file=$(realpath -s "$1")
  local extension=${2#.}
  
  # Use macOS's standard temporary directory
  local tmpdir="${TMPDIR:-/tmp}"
  local temp_output="$tmpdir/compile_output_$$_$(date +%s).tmp"
  local temp_file_list="$tmpdir/compile_files_$$_$(date +%s).tmp"
  
  # Make sure we can write to the output file location
  touch "$output_file" 2>/dev/null || {
    echo "Error: Cannot write to '$output_file'"
    return 1
  }
  
  # Find all matching files
  find . -type f -name "*.$extension" | while read -r file; do
    # Skip the output file itself using absolute paths for comparison
    if [ "$(realpath -s "$file")" != "$output_file" ]; then
      echo "$file"
    fi
  done > "$temp_file_list"
  
  local file_count=$(wc -l < "$temp_file_list" | tr -d ' ')
  
  if [ "$file_count" -eq 0 ]; then
    echo "No files with extension '.$extension' found (excluding the output file itself)."
    rm -f "$temp_file_list"
    return 0
  fi
  
  # Clear output temp file
  echo -n > "$temp_output"
  
  # Process files and write to temporary output
  while IFS= read -r file; do
    if [ ! -r "$file" ]; then
      echo "Warning: Cannot read '$file', skipping"
      continue
    fi
    
    echo "# $file " >> "$temp_output"
    cat "$file" >> "$temp_output" || echo "Warning: Error while reading '$file'"
    echo -e "\n" >> "$temp_output"
  done < "$temp_file_list"
  
  # Copy the temporary output to the final location
  cp "$temp_output" "$output_file" || { 
    echo "Error: Failed to write to '$output_file'"
    rm -f "$temp_file_list" "$temp_output"
    return 1
  }
  
  # Clean up temporary files
  rm -f "$temp_file_list" "$temp_output"
  
  echo "Compiled $file_count files with extension '.$extension' into '$output_file'"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment