-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext.sh
More file actions
executable file
·136 lines (119 loc) · 3.42 KB
/
context.sh
File metadata and controls
executable file
·136 lines (119 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash
ROOT=$(pwd)
source "scripts/util/validate_backend.sh"
echo "Creating context file for $ROOT"
CODEBASE_NAME="rcam"
OUTPUT_FILE="$ROOT/output/context.txt"
echo "Output file: $OUTPUT_FILE"
if [ -f "$OUTPUT_FILE" ]; then
echo "Removing existing context file"
rm -f "$OUTPUT_FILE"
fi
declare -A IGNORE_FILES=(
# ----------------------------- ADD FILES TO IGNORE HERE
["Cargo.lock"]=""
["*.env"]=""
)
declare -A IGNORE_DIRS=(
# ----------------------------- ADD DIRECTORIES TO IGNORE HERE
["output"]=""
["target"]=""
["design"]=""
)
declare -A DIRECTORIES=(
# ----------------------------- ADD DIRECTORIES HERE
["config"]=""
["src"]=""
["docs"]=""
)
declare -A FILES=(
# ----------------------------- ADD FILES HERE
["README.md"]=""
["Cargo.toml"]=""
[".env.example"]=""
)
echo "Below is a list of files for the $CODEBASE_NAME codebase." >> "$OUTPUT_FILE"
# Function to check if a file should be ignored
should_ignore_file() {
local file="$1"
for pattern in "${!IGNORE_FILES[@]}"; do
if [[ "$file" == $pattern ]]; then
return 0
fi
done
return 1
}
# Function to check if a directory should be ignored
should_ignore_dir() {
local dir="$1"
for pattern in "${!IGNORE_DIRS[@]}"; do
if [[ "$dir" == $pattern ]]; then
return 0
fi
done
return 1
}
process_file() {
local file="$1"
if should_ignore_file "$(basename "$file")"; then
echo "Ignoring file: $file"
return
fi
echo "Processing: $file"
echo -e "\n\n--- BEGIN FILE: $file ---\n" >> "$OUTPUT_FILE"
cat "$file" >> "$OUTPUT_FILE"
echo -e "\n--- END FILE: $file ---\n" >> "$OUTPUT_FILE"
}
for specific_file in "${!FILES[@]}"; do
if [ -f "$specific_file" ]; then
process_file "$specific_file"
else
echo "File not found: $specific_file"
fi
done
for dir in "${!DIRECTORIES[@]}"; do
if [ -d "$dir" ]; then
if should_ignore_dir "$dir"; then
echo "Ignoring directory: $dir"
continue
fi
base_selection_args="-type f -not -name \"*.env\""
if [[ -n "${DIRECTORIES[$dir]}" ]]; then
base_selection_args+=" ${DIRECTORIES[$dir]}"
fi
prune_conditions_str=""
has_prune_conditions="false"
for ignored_basename in "${!IGNORE_DIRS[@]}"; do
path_to_prune_for_find="\"$dir/$ignored_basename\""
if [ "$has_prune_conditions" = "true" ]; then
prune_conditions_str+=" -o -path $path_to_prune_for_find"
else
prune_conditions_str="\\( -path $path_to_prune_for_find"
has_prune_conditions="true"
fi
done
find_command_to_eval="find \"$dir\""
if [ "$has_prune_conditions" = "true" ]; then
prune_conditions_str+=" \\) -prune -o"
find_command_to_eval+=" $prune_conditions_str"
fi
find_command_to_eval+=" \\( $base_selection_args -print \\)"
eval "$find_command_to_eval" | while IFS= read -r file; do
process_file "$file"
done
else
echo "Directory not found: $dir"
fi
done
echo -e "\n\n--- END OF CONTEXT ---\n" >> "$OUTPUT_FILE"
TOTAL_FILES=$(grep -c "^--- BEGIN FILE:" "$OUTPUT_FILE")
TOTAL_SIZE=$(du -h "$OUTPUT_FILE" | awk '{print $1}')
echo "Context file created at $OUTPUT_FILE"
echo "Total files: $TOTAL_FILES"
echo "Total size: $TOTAL_SIZE"
if command -v xclip >/dev/null 2>&1; then
xclip -selection clipboard < "$OUTPUT_FILE"
echo "Contents of $OUTPUT_FILE copied to clipboard."
else
echo "xclip not found. Please install xclip to copy to clipboard."
fi