-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcleanmymac.sh
161 lines (132 loc) · 3.45 KB
/
cleanmymac.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
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
#!/usr/bin/env bash
# https://brew.sh/
# https://github.com/buo/homebrew-cask-upgrade
set -e
# set -x
displayNotification() {
local description="${1}"
local title="${2}"
echo "$description - $title"
osascript <<EOF
display notification "$description" with title "$title"
EOF
}
bytesToHumanReadable() {
local bytes
bytes=$(numfmt --to=iec <<<"${1}")
local result="$bytes of space was cleaned up :3"
local noti="来自 CleanMyMac"
displayNotification "$result" "$noti"
}
available() {
df / | awk 'NR==2 {print $4}'
}
updateBrew() {
echo "Updating Homebrew..."
arch -arm64 brew update &&
arch -arm64 brew upgrade &&
arch -arm64 brew upgrade --cask --greedy
}
updateMas() {
echo "Updating Mac App Store..."
mas upgrade
}
clearCocoapods() {
echo "Cleaning CocoaPods..."
pod cache clean --all
}
clearXcode() {
echo "Cleaning Xcode..."
xcrun simctl delete unavailable
rm -rf ~/Library/Developer/Xcode/DerivedData/* &>/dev/null
rm -rf ~/Library/Developer/Xcode/Archives/* &>/dev/null
rm -rf ~/Library/Developer/Xcode/Products/* &>/dev/null
cd ~/Library/Developer/Xcode/iOS\ DeviceSupport || return
local ifile
ifile=$(ls | sort -rV | head -n1)
ls | grep -v "${ifile}" | xargs -I {} rm -rf "{}"
}
clearCache() {
echo "Cleaning cache directories..."
rm -rf ~/Library/Caches/*
rm -rf ~/Library/Application\ Support/*/Cache/*
rm -rf ~/Library/Containers/*/Data/Library/Caches/*
rm -rf ~/Library/Developer/Xcode/iOS\ Device\ Logs/*
rm -rf ~/Library/Developer/Xcode/DerivedData/*
echo "Cache directories cleaned."
}
clearLogs() {
echo "Cleaning application logs..."
rm -rf ~/Library/Logs/*
sudo rm -rf /Library/Logs/*
echo "Application logs cleaned."
}
clearTrash() {
echo "Emptying Trash..."
rm -rf ~/.Trash/*
echo "Trash emptied."
}
displayDialog() {
local effect="${1}"
local text="${2}"
local detailDes="Do you wish to ${effect} ${text} software?"
osascript <<EOF
display dialog "$detailDes" buttons {"Yes", "No"} default button "No"
EOF
}
shouldProceed() {
local action="${1}"
local name="${2}"
local res
res=$(displayDialog "$action" "$name")
[[ "$res" == "button returned:Yes" ]]
}
main() {
############################## Update
if shouldProceed "update" "brew"; then
updateBrew
else
echo "Keeping brew"
fi
if shouldProceed "update" "AppStore"; then
updateMas
else
echo "Keeping mas"
fi
############################## Clear
oldAvailable=$(available)
if type "pod" > /dev/null 2>&1; then
if shouldProceed "clear" "cocoapods"; then
clearCocoapods
else
echo "Don't clean up CocoaPods"
fi
fi
# Clean Xcode
if shouldProceed "clear" "xcode"; then
clearXcode
else
echo "Don't clean up Xcode"
fi
# Clean cache directories
if shouldProceed "clean cache of" "xcode"; then
clearCache
else
echo "Don't clean up cache"
fi
clearLogs
clearTrash
# Clean up Homebrew
echo "Cleaning up Homebrew..."
arch -arm64 brew cleanup
# Clean up Ruby
echo "Cleaning up Ruby..."
gem cleanup
clear && echo 'Success!'
newAvailable=$(available)
count=$((newAvailable - oldAvailable))
count=$((count * 512))
bytesToHumanReadable "$count"
}
# Call the main function to execute the script
main