-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc2m
More file actions
executable file
·240 lines (214 loc) · 7.72 KB
/
c2m
File metadata and controls
executable file
·240 lines (214 loc) · 7.72 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
240
#!/bin/bash
# ======================================================
# 🎋 c2m - Code2Merge in eine Textdatei (FIXED)
# Autor: Levogne
# Version: 1.7 (Output-File Exclusion Fix)
# Beschreibung: Fasst Dateien mit einer oder mehreren Endungen in eine Textdatei zusammen
# Lizenz: MIT
# ======================================================
show_help() {
echo "Verwendung: code2merge <dateierweiterungen> <verzeichnis> [optionen]"
echo " code2merge <projekt-typ> [optionen]"
echo ""
echo "Optionen:"
echo " --export <ausgabedatei> Benutzerdefinierte Ausgabedatei festlegen"
echo " --exec <ordner1,ordner2> Ordner, die ignoriert werden sollen"
echo " --root <datei1,datei2> Wichtige Dateien im Stammverzeichnis"
echo " --exclude-files <datei1,datei2> Dateien, die explizit ausgeschlossen werden"
echo ""
echo "Projekt-Typen:"
echo " fastapi | vue | react | nuxt"
echo ""
echo "Beispiele:"
echo " c2m rs ."
echo " c2m py,js ./ordner"
echo " c2m css,html /pfad/zum/projekt"
echo " c2m rs,js . --export meine_datei.txt"
echo " c2m js,ts,vue ./ --exec node_modules,build,dist"
echo " c2m py ./ --root pyproject.toml,README.md"
echo " c2m nuxt --exclude-files package-lock.json"
echo ""
echo "Standard-Ausgabedatei ist 'code2merge.txt', wenn kein --export angegeben wird."
exit 1
}
if [ $# -lt 1 ]; then
show_help
fi
handle_project_type() {
local type="$1"
shift
case "$type" in
"fastapi")
set -- "py,html,css" "./" \
--exec ".venv" \
--export "c2mFASTAPI.txt" \
--root "pyproject.toml,README.md,Dockerfile,main.py,*.json" "$@"
;;
"vue")
set -- "vue,js,ts,css,scss,html" "./" \
--exec "node_modules,dist" \
--export "c2mVUE.txt" \
--root "package.json,vite.config.js,vue.config.js,README.md,*.json" "$@"
;;
"react")
set -- "jsx,js,ts,tsx,css,scss,html" "./" \
--exec "node_modules,build" \
--export "c2mREACT.txt" \
--root "package.json,webpack.config.js,README.md,*.json" "$@"
;;
"nuxt")
set -- "vue,js,ts,css,scss,html" "./" \
--exec "node_modules,.nuxt,dist" \
--export "c2mNUXT.txt" \
--root "nuxt.config.ts,nuxt.config.js,package.json,README.md,*.json" \
--exclude-files "package-lock.json,yarn-lock.json" "$@"
;;
*)
echo "❌ Unbekannter Projekt-Typ: $type"
show_help
;;
esac
"$0" "$@"
exit 0
}
# Projekt-Typ erkennen
if [[ ! "$1" =~ ^- ]]; then
case "$1" in
fastapi|vue|react|nuxt)
handle_project_type "$@"
;;
esac
fi
if [ $# -lt 2 ]; then
show_help
fi
# === Initialwerte ===
extensions=$1
directory=$2
output_file="code2merge.txt"
exclude_dirs=""
root_files=""
exclude_files=""
shift 2
while [ "$#" -gt 0 ]; do
case "$1" in
--export)
output_file="$2"; shift 2 ;;
--exec)
exclude_dirs="$2"; shift 2 ;;
--root)
root_files="$2"; shift 2 ;;
--exclude-files)
exclude_files="$2"; shift 2 ;;
*)
echo "❌ Unbekanntes Argument: $1"
show_help ;;
esac
done
# === FIX: Output-File automatisch zu exclude_files hinzufügen ===
output_basename=$(basename "$output_file")
if [ -n "$exclude_files" ]; then
exclude_files="$exclude_files,$output_basename"
else
exclude_files="$output_basename"
fi
# === Zusätzliche Standard-Excludes für Merge-Files ===
exclude_files="$exclude_files,code2merge.txt,c2mNUXT.txt,c2mVUE.txt,c2mREACT.txt,c2mFASTAPI.txt"
echo "🔍 Excludierte Dateien: $exclude_files"
# === Start Ausgabe ===
> "$output_file"
# === Stammdateien ===
if [ -n "$root_files" ]; then
echo "// ===== WICHTIGE DATEIEN IM STAMMVERZEICHNIS =====" >> "$output_file"
echo "" >> "$output_file"
IFS=',' read -ra root_array <<< "$root_files"
for pattern in "${root_array[@]}"; do
for file in $directory/$pattern; do
[ -f "$file" ] || continue
skip=0
if [ -n "$exclude_files" ]; then
IFS=',' read -ra exclude_array <<< "$exclude_files"
for ex in "${exclude_array[@]}"; do
[[ "$file" == *"$ex"* ]] && skip=1 && break
done
fi
[ "$skip" -eq 1 ] && echo "🔵 Datei wird übersprungen: $file" && continue
echo "// ## FILE: ${file#./}" >> "$output_file"
echo "// ## BEGIN ##" >> "$output_file"
cat "$file" >> "$output_file"
echo -e "\n// ## END ##\n" >> "$output_file"
done
done
echo "// ===== ENDE WICHTIGE DATEIEN =====" >> "$output_file"
echo "" >> "$output_file"
fi
# === Dateisuche nach Erweiterungen ===
echo "// ===== DATEIEN NACH ERWEITERUNGEN =====" >> "$output_file"
IFS=',' read -ra ext_array <<< "$extensions"
# === Klare find-Befehl Konstruktion ohne Array-Fuckerei ===
build_find_command() {
local ext="$1"
local cmd="find $directory"
# Ordner excludieren
if [ -n "$exclude_dirs" ]; then
IFS=',' read -ra exdirs <<< "$exclude_dirs"
for exdir in "${exdirs[@]}"; do
# Normalisiere den Pfad
if [[ "$directory" == "./" ]]; then
cmd="$cmd ! -path \"./$exdir/*\""
else
cmd="$cmd ! -path \"$directory/$exdir/*\""
fi
done
fi
# Standard-Excludes für Build-Ordner
if [[ "$directory" == "./" ]]; then
cmd="$cmd ! -path \"./node_modules/*\""
cmd="$cmd ! -path \"./.git/*\""
cmd="$cmd ! -path \"./.nuxt/*\""
cmd="$cmd ! -path \"./dist/*\""
cmd="$cmd ! -path \"./.output/*\""
fi
# Output-Files excludieren
cmd="$cmd ! -name \"$output_basename\""
cmd="$cmd ! -name \"code2merge.txt\""
cmd="$cmd ! -name \"c2mNUXT.txt\""
cmd="$cmd ! -name \"c2mVUE.txt\""
cmd="$cmd ! -name \"c2mREACT.txt\""
cmd="$cmd ! -name \"c2mFASTAPI.txt\""
# Dateityp und Extension
cmd="$cmd -type f \\( -name \"*.$ext\" -o -name \"*.*.$ext\" \\)"
echo "$cmd"
}
for ext in "${ext_array[@]}"; do
find_command=$(build_find_command "$ext")
echo "🔍 Find-Befehl: $find_command"
while IFS= read -r file; do
[ -z "$file" ] && continue
skip=0
# === Doppelte Sicherheit: Nochmal checken ob es das Output-File ist ===
file_basename=$(basename "$file")
[[ "$file_basename" == "$output_basename" ]] && skip=1
if [ -n "$exclude_files" ]; then
IFS=',' read -ra exclude_array <<< "$exclude_files"
for ex in "${exclude_array[@]}"; do
[[ "$file" == *"$ex"* ]] && skip=1 && break
done
fi
[ "$skip" -eq 1 ] && echo "🔵 Datei wird übersprungen: $file" && continue
echo "📄 Verarbeite: $file"
echo "// ## FILE: ${file#./}" >> "$output_file"
echo "// ## BEGIN ##" >> "$output_file"
cat "$file" >> "$output_file"
echo -e "\n// ## END ##\n" >> "$output_file"
done < <(eval "$find_command")
done
# === Abschluss-Info ===
file_size=$(ls -lh "$output_file" | awk '{print $5}')
echo "✅ Dateien wurden erfolgreich in '$output_file' zusammengeführt."
echo "📊 Dateigröße: $file_size"
# === Warnung bei verdächtiger Größe ===
file_size_bytes=$(stat -c%s "$output_file" 2>/dev/null || stat -f%z "$output_file" 2>/dev/null)
if [ "$file_size_bytes" -gt 10485760 ]; then # > 10MB
echo "⚠️ WARNUNG: Datei ist größer als 10MB - prüfe auf doppelte Includes!"
fi