-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanup.sh
More file actions
executable file
Β·190 lines (160 loc) Β· 5.21 KB
/
cleanup.sh
File metadata and controls
executable file
Β·190 lines (160 loc) Β· 5.21 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
#!/usr/bin/env bash
# Zsh Configuration Cleanup Script
# Removes backup files, temporary data, and optimizes configuration
set -euo pipefail
CONFIG_DIR="${ZDOTDIR:-$HOME/.config/zsh}"
REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
echo "π§Ή Zsh Configuration Cleanup"
echo "============================"
# Function to ask for confirmation
confirm() {
local prompt="$1"
local response
read -r -p "$prompt (y/N): " response
[[ "$response" =~ ^[Yy]$ ]]
}
# Clean backup directories
cleanup_backups() {
echo "π Cleaning backup directories..."
if [[ -d "$CONFIG_DIR/backups" ]]; then
local backup_count
backup_count=$(find "$CONFIG_DIR/backups" -maxdepth 1 -type d ! -path "$CONFIG_DIR/backups" | wc -l)
if [[ $backup_count -gt 0 ]]; then
echo "Found $backup_count backup directories"
if confirm "Remove all backups except the 3 most recent?"; then
# Keep only the 3 most recent backups
find "$CONFIG_DIR/backups" -maxdepth 1 -type d ! -path "$CONFIG_DIR/backups" -printf '%T@ %p\n' | sort -rn | tail -n +4 | cut -d' ' -f2- | while read -r old_backup; do
rm -rf "$CONFIG_DIR/backups/$old_backup"
echo "Removed: $old_backup"
done
echo "β
Backup cleanup complete"
fi
else
echo "No backups found"
fi
else
echo "No backup directory found"
fi
}
# Clean shell-plugins.backup if present
cleanup_old_plugins() {
echo "π Cleaning old plugin directories..."
if [[ -d "$REPO_DIR/shell-plugins.backup" ]]; then
if confirm "Remove old shell-plugins.backup directory?"; then
rm -rf "$REPO_DIR/shell-plugins.backup"
echo "β
Removed shell-plugins.backup"
fi
else
echo "No old plugin directories found"
fi
}
# Clean compiled files and regenerate
recompile_configs() {
echo "βοΈ Recompiling configuration files..."
# Remove existing compiled files
find "$CONFIG_DIR" -name "*.zwc" -type f -delete 2>/dev/null || true
find "$REPO_DIR" -name "*.zwc" -type f -delete 2>/dev/null || true
# Recompile main configuration
if command -v zsh &>/dev/null; then
zsh -c "
autoload -U zrecompile
zrecompile -p '$CONFIG_DIR/.zshrc'
zrecompile -p '$CONFIG_DIR/keybindings.zsh'
zrecompile -p '$CONFIG_DIR/recovery.zsh'
" 2>/dev/null || echo "β οΈ Some files couldn't be compiled"
echo "β
Configuration files recompiled"
else
echo "β οΈ Zsh not available for recompilation"
fi
}
# Clean completion cache
clean_completion_cache() {
echo "π Cleaning completion cache..."
local cache_files=(
"$HOME/.zcompdump"
"$HOME/.zcompdump.zwc"
"$CONFIG_DIR/.zcompdump"
"$CONFIG_DIR/.zcompdump.zwc"
)
local cleaned=0
for cache_file in "${cache_files[@]}"; do
if [[ -f "$cache_file" ]]; then
rm -f "$cache_file"
echo "Removed: $(basename "$cache_file")"
((cleaned++))
fi
done
if [[ $cleaned -gt 0 ]]; then
echo "β
Cleaned $cleaned cache files"
else
echo "No cache files found"
fi
}
# Optimize Zinit
optimize_zinit() {
echo "β‘ Optimizing Zinit..."
if [[ -d "$HOME/.local/share/zinit" ]]; then
if command -v zsh &>/dev/null; then
echo "Updating Zinit and plugins..."
zsh -c "
source '$HOME/.local/share/zinit/zinit.git/zinit.zsh'
zinit self-update
zinit update --all
zinit compile --all
" 2>/dev/null || echo "β οΈ Some Zinit operations failed"
echo "β
Zinit optimized"
else
echo "β οΈ Zsh not available for Zinit optimization"
fi
else
echo "Zinit not found"
fi
}
# Validate configuration
validate_config() {
echo "β
Validating configuration..."
if command -v zsh &>/dev/null; then
if zsh -n "$CONFIG_DIR/.zshrc" 2>/dev/null; then
echo "β
Configuration syntax is valid"
else
echo "β Configuration has syntax errors:"
zsh -n "$CONFIG_DIR/.zshrc"
return 1
fi
else
echo "β οΈ Zsh not available for validation"
fi
}
# Main execution
main() {
echo "Configuration directory: $CONFIG_DIR"
echo "Repository directory: $REPO_DIR"
echo ""
# Run cleanup operations
cleanup_backups
echo ""
cleanup_old_plugins
echo ""
clean_completion_cache
echo ""
recompile_configs
echo ""
optimize_zinit
echo ""
validate_config
echo ""
echo "π Cleanup complete!"
echo ""
echo "π‘ Recommendations:"
echo " β’ Restart your terminal to apply changes"
echo " β’ Run 'zsh-benchmark' to check performance"
echo " β’ Run 'config-health' to verify everything works"
}
# Check if running from the correct directory
if [[ ! -f "$REPO_DIR/.zshrc" ]]; then
echo "β Error: This script must be run from the zsh configuration directory"
echo "Expected to find .zshrc in: $REPO_DIR"
exit 1
fi
# Run main function
main "$@"