-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode2llm.sh
executable file
·74 lines (64 loc) · 1.74 KB
/
code2llm.sh
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
#!/bin/bash
# Function to check if a file is text
is_text_file() {
local file="$1"
local mime_type=$(file -b --mime-type "$file")
[[ $mime_type == text/* || $mime_type == application/json || $mime_type == application/xml ]]
}
# Function to print file contents with filename as comment
print_file_contents() {
local file="$1"
echo "# File: $file"
echo "------------------------"
cat "$file"
echo -e "\n------------------------\n"
}
# Function to check if a file matches the pattern
matches_pattern() {
local file="$1"
local patterns="$2"
IFS='|' read -ra ADDR <<< "$patterns"
for i in "${ADDR[@]}"; do
if [[ $file == *$i ]]; then
return 0
fi
done
return 1
}
# Initialize variables
directory=""
pattern=""
# Parse command line options
while getopts ":P:d:" opt; do
case $opt in
P) pattern="$OPTARG"
;;
d) directory="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2
exit 1
;;
esac
done
# Check if a directory is provided
if [ -z "$directory" ]; then
echo "Usage: $0 -d <directory> [-P <pattern1>|<pattern2>|...]"
exit 1
fi
# Change to the specified directory
cd "$directory" || exit 1
# Print directory structure
echo "Directory Structure:"
if [ -n "$pattern" ]; then
tree -L 10 -P "${pattern//|/ }"
else
tree -L 10
fi
echo -e "\nFile Contents:"
# Iterate through all files in the directory and subdirectories
while IFS= read -r -d '' file; do
# Check if it's a regular file (not a directory), a text file, and matches the pattern (if provided)
if [ -f "$file" ] && is_text_file "$file" && { [ -z "$pattern" ] || matches_pattern "$file" "$pattern"; }; then
print_file_contents "$file"
fi
done < <(find . -type f -print0)