-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodebase_dump_script.sh
More file actions
239 lines (212 loc) · 6.56 KB
/
codebase_dump_script.sh
File metadata and controls
239 lines (212 loc) · 6.56 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/bin/bash
# Script to aggregate all code files from specified folders into a single file
# Usage: ./aggregate_code.sh [output_file] [folder1] [folder2] [folder3] ...
# Default values
OUTPUT_FILE="${1:-codebase_dump.txt}"
shift # Remove first argument (output file) from $@
CLI_FOLDERS=("$@") # Folders specified via command line
# =============================================================================
# CONFIGURATION: Edit these arrays to set default folders and files to scan
# =============================================================================
# Default folders to scan (relative paths from where script is run)
# These will be used if no folders are specified on command line
DEFAULT_FOLDERS=(
"src/main/java"
# Add your project-specific folders here
# "frontend/src"
# "backend/api"
# "shared"
)
# Specific files to always include when found within the scanned folders
# (these will be included even if they don't match code file extensions)
INCLUDE_FILES=(
"README.md"
"package.json"
"requirements.txt"
"Cargo.toml"
"go.mod"
"pom.xml"
"build.gradle"
"CMakeLists.txt"
"Makefile"
"Dockerfile"
".env.example"
# Add your specific files here
# "config/database.yml"
# "docs/api.md"
)
# =============================================================================
# Determine which folders to scan
if [ ${#CLI_FOLDERS[@]} -gt 0 ]; then
SCAN_FOLDERS=("${CLI_FOLDERS[@]}")
echo "Using command-line specified folders"
else
SCAN_FOLDERS=("${DEFAULT_FOLDERS[@]}")
echo "Using default configured folders"
echo "Tip: You can override by specifying folders: $0 [output_file] folder1 folder2 ..."
fi
# Common code file extensions
CODE_EXTENSIONS=(
"*.py" # Python
"*.js" # JavaScript
"*.ts" # TypeScript
"*.jsx" # React JSX
"*.tsx" # React TSX
"*.java" # Java
"*.c" # C
"*.cpp" # C++
"*.cc" # C++
"*.cxx" # C++
"*.h" # Header files
"*.hpp" # C++ headers
"*.cs" # C#
"*.php" # PHP
"*.rb" # Ruby
"*.go" # Go
"*.rs" # Rust
"*.swift" # Swift
"*.kt" # Kotlin
"*.scala" # Scala
"*.sh" # Shell scripts
"*.bash" # Bash scripts
"*.zsh" # Zsh scripts
"*.fish" # Fish scripts
"*.ps1" # PowerShell
"*.r" # R
"*.R" # R
"*.m" # Objective-C/MATLAB
"*.mm" # Objective-C++
"*.pl" # Perl
"*.pm" # Perl modules
"*.lua" # Lua
"*.vim" # Vim script
"*.sql" # SQL
"*.html" # HTML
"*.htm" # HTML
"*.css" # CSS
"*.scss" # Sass
"*.sass" # Sass
"*.less" # Less CSS
"*.xml" # XML
"*.json" # JSON
"*.yaml" # YAML
"*.yml" # YAML
"*.toml" # TOML
"*.ini" # INI files
"*.conf" # Configuration files
"*.config" # Configuration files
"*.md" # Markdown
"*.txt" # Text files
"*.dockerfile" # Dockerfile
"Dockerfile" # Dockerfile (no extension)
"Makefile" # Makefile
"*.mk" # Makefile
"*.cmake" # CMake
"*.gradle" # Gradle
"*.sbt" # SBT
"*.clj" # Clojure
"*.cljs" # ClojureScript
"*.ex" # Elixir
"*.exs" # Elixir
"*.erl" # Erlang
"*.hrl" # Erlang headers
"*.dart" # Dart
"*.f90" # Fortran
"*.f95" # Fortran
"*.asm" # Assembly
"*.s" # Assembly
)
# Function to check if a folder exists and is accessible
check_folder() {
local folder="$1"
if [ ! -d "$folder" ]; then
echo "Warning: Folder '$folder' does not exist, skipping..."
return 1
fi
if [ ! -r "$folder" ]; then
echo "Warning: Folder '$folder' is not readable, skipping..."
return 1
fi
return 0
}
# Function to check if file is a code file
is_code_file() {
local file="$1"
local filename=$(basename "$file")
# Check for exact matches first (like Dockerfile, Makefile)
for ext in "${CODE_EXTENSIONS[@]}"; do
if [[ "$filename" == "$ext" ]]; then
return 0
fi
done
# Check for pattern matches
for ext in "${CODE_EXTENSIONS[@]}"; do
if [[ "$filename" == $ext ]]; then
return 0
fi
done
return 1
}
# Function to check if file is binary
is_binary_file() {
if file "$1" | grep -q "text"; then
return 1 # Not binary
else
return 0 # Binary
fi
}
echo "Starting code aggregation..."
echo "Folders to scan: ${SCAN_FOLDERS[*]}"
echo "Output file: $(realpath "$OUTPUT_FILE")"
echo "----------------------------------------"
# Clear the output file
> "$OUTPUT_FILE"
# Counter for processed files
file_count=0
# Process each specified folder
for folder in "${SCAN_FOLDERS[@]}"; do
echo "Scanning folder: $folder"
# Check if folder exists and is accessible
if ! check_folder "$folder"; then
continue
fi
# Find and process all code files in this folder
while IFS= read -r -d '' file; do
# Check if it's a code file or in the include files list
filename=$(basename "$file")
is_include_file=false
# Check if this file matches any of the include files (just filename)
for include_file in "${INCLUDE_FILES[@]}"; do
include_filename=$(basename "$include_file")
if [[ "$filename" == "$include_filename" ]]; then
is_include_file=true
break
fi
done
if is_code_file "$file" || [[ "$is_include_file" == true ]]; then
# Skip binary files
if is_binary_file "$file"; then
echo "Skipping binary file: $file"
continue
fi
# Get relative path from current directory (where script is run)
if command -v realpath >/dev/null 2>&1; then
rel_path=$(realpath --relative-to="." "$file")
else
# Fallback for systems without realpath
rel_path="$file"
fi
echo "Processing: $rel_path"
# Write the path and content to output file
echo "$rel_path" >> "$OUTPUT_FILE"
cat "$file" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
((file_count++))
fi
done < <(find "$folder" -type f -print0)
done
echo "----------------------------------------"
echo "Code aggregation completed!"
echo "Processed $file_count files"
echo "Output written to: $(realpath "$OUTPUT_FILE")"