-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcleanup
executable file
·247 lines (200 loc) · 6.06 KB
/
cleanup
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
241
242
243
244
245
246
247
#!/bin/bash
# Cleans system and application caches on macOS.
usage() {
echo "usage: ${0##*/} [options]" 1>&2
echo "" 1>&2
echo "Cleans system and application caches on macOS." 1>&2
echo ""
echo "options:"
echo " -h or --help shows usage help" 1>&2
exit 1
}
if echo "$*" | grep -Eq -- '--help\b|-h\b'; then
usage
fi
array_contains() {
local seeking=$1
shift
local in=1
for element; do
# shellcheck disable=SC2053
if [[ $element == $seeking ]]; then
in=0
break
fi
done
return $in
}
set -e
show() {
local important=0
if [[ $1 == "-i" ]]; then
shift
important=1
echo -n $'\e[38;5;196m'"$(tput bold)! "
elif [[ $1 == "-r" ]]; then
shift
echo -n $'\e[38;5;40m$\e[38;5;63m '
else
echo -n $'\e[1;34m>\e[0m\e[31m '
fi
echo -n "$@"
if [[ $important == "1" ]]; then
echo -n " $(tput sgr0)"$'\e[0m'
else
echo -n $'\e[0m'
fi
echo
}
run() {
show -r "$@"
eval "$@"
return $?
}
# check if gatekeeper is enabled
# GATEKEEPER="$(spctl --status)"
DONE=0
BEFORE="$(df -B1 --output=avail "$HOME" | grep -iv avail)"
show "disk space available before cleanup: $(numfmt --to=iec-i --suffix=B -- "$BEFORE")"
# by default, execute these commands from your home directory
run "cd"
report() {
# if [[ $GATEKEEPER == "assessments enabled" ]]; then
# run "sudo spctl --master-enable"
# fi
if [[ $DONE == 0 ]]; then
show -i "clean up ended prematurely because a command failed!"
fi
AFTER="$(df -B1 --output=avail "$HOME" | grep -iv avail)"
show "disk space available after cleanup: $(numfmt --to=iec-i --suffix=B -- "$AFTER")"
RECLAIMED="$((AFTER - BEFORE))"
if [[ $RECLAIMED -gt "0" ]]; then
show "disk space reclaimed by cleanup: $(numfmt --to=iec-i --suffix=B -- "$RECLAIMED")"
fi
}
trap report EXIT
# grab and hold sudo
sudo -v
while true; do
sudo -n true
sleep 60
kill -0 "$$" || exit
done 2>/dev/null &
# disable gatekeeper
# if [[ $GATEKEEPER == "assessments enabled" ]]; then
# run "sudo spctl --master-disable"
# fi
# trash
run "sudo rm -rf /Volumes/*/.Trashes/*"
run "sudo rm -rf ~/.Trash/*"
# macos
run "sudo rm -rf /private/var/log/asl/*.asl"
run "sudo rm -rf /Library/Logs/DiagnosticReports/*"
run "rm -rf ~/Library/Containers/com.apple.mail/Data/Library/Logs/Mail/*"
run "rm -rf ~/Library/Logs/CoreSimulator/*"
# adobe
run "sudo rm -rf ~/Library/Application\ Support/Adobe/Common/Media\ Cache\ Files/*"
run "sudo rm -rf /Library/Logs/Adobe/*"
# itunes
run "rm -rf ~/Music/iTunes/iTunes\ Media/Mobile\ Applications/*"
run "rm -rf ~/Library/Application\ Support/MobileSync/Backup/*"
# xcode
run "rm -rf ~/Library/Developer/Xcode/DerivedData/*"
run "rm -rf ~/Library/Developer/Xcode/Archives/*"
# if command -v xcrun >/dev/null 2>&1; then
# # shellcheck disable=SC2207
# IFS=$'\n' APPS=($(osascript -e 'tell application "System Events" to return every application process' | tr ',' '\n' | sed 's/^ *application process //g'))
# if array_contains 'com.apple.CoreSimulator.CoreSimulatorService' "${APPS[@]}"; then
# run "osascript -e 'tell application \"com.apple.CoreSimulator.CoreSimulatorService\" to quit'"
# fi
# if array_contains 'iOS Simulator' "${APPS[@]}"; then
# run "osascript -e 'tell application \"iOS Simulator\" to quit'"
# fi
# if array_contains 'Simulator' "${APPS[@]}"; then
# run "osascript -e 'tell application \"Simulator\" to quit'"
# fi
# run "xcrun simctl erase all"
# fi
# cocoapods
test -d "$HOME/Library/Caches/CocoaPods" && run "rm -rf '$_'/*"
# chrome
test -d "$HOME/Library/Caches/Google/Chrome" && run "rm -rf '$_'/*"
# gradle
test -d "$HOME/.gradle/caches/" && run "rm -rf '$_'"
# dropbox
test -d "$HOME/Dropbox/.dropbox.cache" && run "rm -rf '$_'/*"
# composer
command -v composer >/dev/null 2>&1 && run "composer clearcache"
# bundler
test -d "$HOME/.bundle/cache" && run "rm -rf '$_'"
# various user caches
test -d "$HOME/.cache" && run "rm -rf '$_'"
# ccache
test -d "$HOME/.ccache" && run "rm -rf '$_'"
# electron
test -d "$HOME/.electron-gyp" && run "rm -rf '$_'"
test -d "$HOME/.electron" && run "rm -rf '$_'"
# node-gyp
test -d "$HOME/.node-gyp" && run "rm -rf '$_'"
# pylint
test -d "$HOME/.pylint.d" && run "rm -rf '$_'"
# solargraph
test -d "$HOME/.solargraph" && run "rm -rf '$_'"
# babel
test -e "$HOME/.babel.json" && run "rm -f '$_'"
# brew
if command -v brew >/dev/null 2>&1; then
run "brew autoremove"
run "brew cleanup --prune=all"
run "rm -rf $(brew --cache)"
run "brew tap --repair"
fi
# gem
command -v gem >/dev/null 2>&1 && run "yes | gem cleanup"
# cargo
if command -v cargo >/dev/null 2>&1; then
test -e "$HOME/.cargo/registry" && run "rm -rf '$_'"
test -e "$HOME/.cargo/git" && run "rm -rf '$_'"
fi
# go
command -v go >/dev/null 2>&1 && run "go clean -cache -modcache -testcache"
# conda
command -v conda >/dev/null 2>&1 && run "conda clean -a --yes"
# pip
test -d "$HOME/Library/Caches/pip" && run "rm -rf '$_'"
# pyenv
if [[ -n $PYENV_VIRTUALENV_CACHE_PATH ]]; then
test -d "$PYENV_VIRTUALENV_CACHE_PATH" && run "rm -rf '$_'"
fi
# npm
command -v npm >/dev/null 2>&1 && run "npm -g cache clean --force"
# yarn
command -v yarn >/dev/null 2>&1 && run "yarn cache clean --all --force"
# git
if command -v git >/dev/null 2>&1; then
set +e
find "$HOME" -maxdepth 4 \
-not \( -path "$HOME/Library" -prune \) \
-not \( -path "$HOME/Pictures" -prune \) \
-not \( -path "$HOME/.Trash" -prune \) \
-type d -name .git | while read -r GDIR; do
REPO_ROOT="$(dirname "$GDIR")"
run "cd '$REPO_ROOT'"
if [[ $PWD == "$REPO_ROOT" ]]; then
run "git prune"
run "git gc --auto"
fi
done
run "cd"
set -e
fi
# docker
show "docker: checking if up..."
if command -v docker >/dev/null 2>&1 && docker stats --no-stream >/dev/null 2>&1; then
run "docker system prune --volumes -f"
else
show "docker: not up"
fi
# inactive memory
run "sudo purge"
DONE=1