Skip to content

Instantly share code, notes, and snippets.

@tzmartin
Created February 26, 2025 17:18
Show Gist options
  • Save tzmartin/49bfbf16a2948731fca8e75f3484b1da to your computer and use it in GitHub Desktop.
Save tzmartin/49bfbf16a2948731fca8e75f3484b1da to your computer and use it in GitHub Desktop.
Cursor Rules: "Create a global agent rule to run a bash script every time new files are created" - @iannuttall

Here is the refactored version of @iannuttall's update_structure.sh script.

This version no longer depends on a git repository and provides a fallback mechanism using the provided SEDMAGIC if the tree command is unavailable.

Original post: https://x.com/iannuttall/status/1894699976732594459

Update .cursor/rules/agent.mdc

---
description: Rules for the agent to follow
globs: 
alwaysApply: true
---
# Agent Instructions

If you create a new file(s), pleas run the following command afterwards to update the project documentation.

```bash ./.scripts/update_structure-nodeps.sh```
#!/bin/bash
# save to .scripts/update_structure.sh
# Generates a folder tree and saves it to a markdown file.
# Best used with tree: `brew install tree`
# Output file
OUTPUT_FILE=".cursor/rules/structure.mdc"
# Create the output file with header
mkdir -p "$(dirname "$OUTPUT_FILE")" # Ensure the directory exists
echo "# Project Structure" > "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
echo "\`\`\`" >> "$OUTPUT_FILE"
# Check if tree command is available
if command -v tree &> /dev/null; then
# Use tree command for better visualization
tree -a > "$OUTPUT_FILE"
echo "Using tree command for structure visualization."
else
# Fallback to the alternative approach if tree is not available
echo "Tree command not found. Using fallback approach."
# Define SEDMAGIC for creating the tree-like structure
SEDMAGIC='s;[^/]*/;|-- ;g;s;-- |; |;g'
# Set directory list to argument(s) or current directory
if [ "$#" -gt 0 ]; then
dirlist="$@"
else
dirlist="."
fi
# Generate the tree structure using find and sed
for dir in $dirlist; do
find "$dir" -print | sed -e "$SEDMAGIC" >> "$OUTPUT_FILE"
done
fi
# Close the code block
echo "\`\`\`" >> "$OUTPUT_FILE"
echo "Project structure has been updated in $OUTPUT_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment