-
Notifications
You must be signed in to change notification settings - Fork 0
/
homebrew-update.1h.sh
executable file
·265 lines (246 loc) · 11.7 KB
/
homebrew-update.1h.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env bash
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
SCRIPT_NAME=$(basename "$0")
ASSETS_DIR=${SCRIPT_DIR}/homebrew-update/assets
MAX_LOG_HISTORY=10000
IGNORE_FILE=${ASSETS_DIR}/brew-upgrade-ignore.json
LOG_FILE=${ASSETS_DIR}/brew-upgrade.log
ERR_FILE=${ASSETS_DIR}/brew-upgrade.errors
if [[ -f "${ASSETS_DIR}/.term" ]]; then
TERM=true
else
TERM=false
fi
# Create an empty ignore file if it doesn't exist
if [[ ! -f "${IGNORE_FILE}" ]]; then
echo '{"casks": [], "formulae": []}' > "${IGNORE_FILE}"
fi
export HOMEBREW_CASK_OPTS=--no-quarantine
add_date() {
while IFS= read -r line; do
printf '%s %s\n' "$(date '+%d.%m.%Y-%M:%H:%S')" "$line";
done
}
# Logrotate after MAX_LOG_HISTORY number of lines
if [[ $(wc -l <"${LOG_FILE}") -ge ${MAX_LOG_HISTORY} ]]; then
tail -n${MAX_LOG_HISTORY} "${LOG_FILE}" > "${LOG_FILE}"
fi
if [ -f /opt/homebrew/bin/brew ]; then
HOMEBREW_BIN=/opt/homebrew/bin
else
if [ -f /usr/local/bin/brew ]; then
HOMEBREW_BIN=/usr/local/bin
else
if [ -f $HOME/.local/opt/homebrew/bin/brew ]; then
HOMEBREW_BIN=$HOME/.local/opt/homebrew/bin
else
echo "Failed!"
echo "---"
echo "Homebrew not found in /opt/homebrew/bin, /usr/local/bin or ~/.local/opt/homebrew/bin!"
exit
fi
fi
fi
PATH="${HOMEBREW_BIN}:${PATH}"
brew update >> /dev/null 2>&1
outdated="$(brew outdated --greedy-auto-updates --json)"
ignore_formulae=$(jq -r '.formulae[]' "${IGNORE_FILE}")
ignore_casks=$(jq -r '.casks[]' "${IGNORE_FILE}")
# Filter out ignored formulae
formulae=$(echo "${outdated}" | jq '.formulae.[].name' -r | while read -r formula; do
echo "${ignore_formulae}" | grep -q "${formula}" || echo "${formula}"
done | xargs)
# Filter out ignored casks
casks=$(echo "${outdated}" | jq '.casks.[].name' -r | while read -r cask; do
echo "${ignore_casks}" | grep -q "${cask}" || echo "${cask}"
done | xargs)
count_formulae=$(echo ${formulae} | wc -w | xargs)
count_casks=$(echo ${casks} | wc -w | xargs)
count_all=$((count_formulae + count_casks))
count_ignore_formulae=$(echo ${ignore_formulae} | wc -w | xargs)
count_ignore_casks=$(echo ${ignore_casks} | wc -w | xargs)
count_ignore_all=$((count_ignore_casks + count_ignore_formulae))
icon=$(base64 -i "${ASSETS_DIR}/icon.png")
icon_attention=$(base64 -i "${ASSETS_DIR}/icon_attention.png")
icon_updating=$(base64 -i "${ASSETS_DIR}/icon_updating.png")
if [ $# -eq 0 ]; then
if [[ -f "${ASSETS_DIR}/.updating" ]]; then
echo " | templateImage=${icon_updating}"
else
if [[ "${count_all}" != "0" ]]; then
echo " | templateImage=${icon_attention}"
else
echo " | templateImage=${icon}"
fi
fi
echo "---"
if [[ -f "${ERR_FILE}" ]]; then
if [[ ! -s "${ERR_FILE}" ]]; then
# Delete File if empty
rm -rf "${ERR_FILE}"
else
echo "Errors while upgrading: | color=#9c1e1e"
errors=$(cat "${ERR_FILE}" | sort -u)
for err_pkg in ${errors}; do
echo "--${err_pkg}"
echo "----Show log | bash=/usr/bin/open param1='${LOG_FILE}' terminal=false refresh=true"
echo "----Reinstall | bash='${SCRIPT_DIR}/${SCRIPT_NAME}' param1=reinstall param2=${err_pkg} terminal=true refresh=true"
echo "----Uninstall | bash='${SCRIPT_DIR}/${SCRIPT_NAME}' param1=uninstall param2=${err_pkg} terminal=true refresh=true"
done
echo "Clear Errors | color=#68696C | bash=rm param1=-rf param2='${ERR_FILE}' param3='${LOG_FILE}' terminal=false refresh=true"
echo "---"
fi
fi
if [[ "${count_all}" != "0" ]]; then
if [[ "${count_formulae}" != "0" ]]; then
ident=''
if [[ "${count_formulae}" -gt 5 ]]; then
ident='--'
echo "${count_formulae} Formulae can be update"
fi
for line in ${formulae}; do
echo "${ident}${line}" | grep "[a-z]" | sed "s_${ident}\(.*\)_& | bash='${SCRIPT_DIR}/${SCRIPT_NAME}' param1=upgrade param2=--formulae param3=\1 terminal=${TERM} refresh=true_g"
done
if [[ "${count_formulae}" -gt 1 ]]; then
echo "Brew Upgrade All Formulae | bash='${SCRIPT_DIR}/${SCRIPT_NAME}' param1=upgrade-all-formulae terminal=${TERM} refresh=true"
fi
fi
if [[ "${count_formulae}" == "0" ]]; then
echo "Formulae are up to date!"
fi
echo "---"
if [[ "${count_casks}" != "0" ]]; then
ident=''
if [[ "${count_casks}" -gt 5 ]]; then
ident='--'
echo "${count_casks} Casks can be update"
fi
for line in ${casks}; do
echo "${ident}${line}" | grep "[a-z]" | sed "s_${ident}\(.*\)_& | bash='${SCRIPT_DIR}/${SCRIPT_NAME}' param1=upgrade param2=--cask param3=\1 terminal=${TERM} refresh=true_g"
done
if [[ "${count_casks}" -gt 1 ]]; then
echo "Brew Upgrade All Casks | bash='${SCRIPT_DIR}/${SCRIPT_NAME}' param1=upgrade-all-casks terminal=${TERM} refresh=true"
fi
fi
if [[ "${count_casks}" == "0" ]]; then
echo "Casks are up to date!"
fi
echo "---"
echo "Add to ignore list"
for line in ${formulae}; do
echo "--${line} | bash='${SCRIPT_DIR}/${SCRIPT_NAME}' param1=ignore param2=formulae param3=${line} terminal=false refresh=true"
done
for line in ${casks}; do
echo "--${line} | bash='${SCRIPT_DIR}/${SCRIPT_NAME}' param1=ignore param2=casks param3=${line} terminal=false refresh=true"
done
if [[ "${count_ignore_all}" != "0" ]]; then
echo "Open Ignore List | bash=/usr/bin/open param1='${IGNORE_FILE}' terminal=false refresh=true"
fi
echo "Brew Upgrade All | bash='${SCRIPT_DIR}/${SCRIPT_NAME}' param1=upgrade-all terminal=${TERM} refresh=true"
else
echo "Everthing is up to date!"
echo "---"
fi
echo "Brew Cleanup | bash='${SCRIPT_DIR}/${SCRIPT_NAME}' param1=cleanup terminal=${TERM} refresh=true"
echo "---"
if [[ ${TERM} == 'true' ]]; then
echo "Hide Terminal | bash='rm' param1=${ASSETS_DIR}/.term terminal=false refresh=true"
else
echo "Show Terminal | bash='touch' param1=${ASSETS_DIR}/.term terminal=false refresh=true"
fi
echo "---"
echo "Refresh | refresh=true"
else
if [ "$#" -gt 0 ]; then
rm -rf "${LOG_FILE}"
nohup caffeinate -dism -w $$ >/dev/null 2>&1 &
fi
if [ "$#" -eq 3 ] && [ ${1} == 'upgrade' ]; then
echo "Starting brew upgrade ${2} ${3}" | add_date | tee -a "${LOG_FILE}"
touch "${ASSETS_DIR}/.updating"
/usr/bin/open --background xbar://app.xbarapp.com/refreshPlugin?path=${SCRIPT_NAME}
sleep 1
brew upgrade ${2} ${3} 2>&1 | add_date | tee -a "${LOG_FILE}"
cat "${LOG_FILE}" | grep 'Error:' | awk -F ":" '{print $4}' | sort -u >> "${ERR_FILE}"
sleep 1
rm "${ASSETS_DIR}/.updating"
/usr/bin/open --background xbar://app.xbarapp.com/refreshPlugin?path=${SCRIPT_NAME}
echo "Finished brew upgrade ${2} ${3}" | add_date | tee -a "${LOG_FILE}"
fi
if [ "$#" -eq 3 ] && [ "${1}" == 'ignore' ]; then
ignore_type="${2}"
ignore_item="${3}"
jq --arg item "${ignore_item}" --arg ignore_type "${ignore_type}" '.[$ignore_type] += [$item]' "${IGNORE_FILE}" | jq '.' --indent 4 > "${IGNORE_FILE}.tmp" && mv "${IGNORE_FILE}.tmp" "${IGNORE_FILE}"
echo "Ignored ${ignore_item} in ${ignore_type}" | add_date | tee -a "${LOG_FILE}"
/usr/bin/open --background xbar://app.xbarapp.com/refreshPlugin?path=${SCRIPT_NAME}
sleep 1
fi
if [ "$#" -eq 2 ] && [ ${1} == 'reinstall' ]; then
echo "Starting brew reinstall ${2}" | add_date | tee -a "${LOG_FILE}"
brew reinstall ${2} 2>&1
cat "${ERR_FILE}" | grep -v ${2} > "${ERR_FILE}"
sleep 1
/usr/bin/open --background xbar://app.xbarapp.com/refreshPlugin?path=${SCRIPT_NAME}
echo "Finished brew reinstall ${2}" | add_date | tee -a "${LOG_FILE}"
fi
if [ "$#" -eq 2 ] && [ ${1} == 'uninstall' ]; then
echo "Starting brew uninstall ${2}" | add_date | tee -a "${LOG_FILE}"
brew uninstall ${2} 2>&1
cat "${ERR_FILE}" | grep -v ${2} > "${ERR_FILE}"
sleep 1
/usr/bin/open --background xbar://app.xbarapp.com/refreshPlugin?path=${SCRIPT_NAME}
echo "Finished brew uninstall ${2}" | add_date | tee -a "${LOG_FILE}"
fi
if [ "$#" -eq 1 ]; then
if [[ ${1} == 'upgrade-all' ]]; then
echo "Starting brew upgrade --greedy-auto-updates $(echo ${formulae} | tr '\n' ' ')$(echo ${casks} | tr '\n' ' ')" | add_date | tee -a "${LOG_FILE}"
touch "${ASSETS_DIR}/.updating"
/usr/bin/open --background xbar://app.xbarapp.com/refreshPlugin?path=${SCRIPT_NAME}
sleep 1
brew upgrade --greedy-auto-updates $(echo ${formulae} | tr '\n' ' ')$(echo ${casks} | tr '\n' ' ') 2>&1 | add_date | tee -a "${LOG_FILE}"
errors=$(cat "${LOG_FILE}" | grep 'Error:' | awk -F ":" '{print $4}' | sort -u)
if [[ $errors ]]; then
echo "$errors" >> "${ERR_FILE}"
fi
sleep 1
rm "${ASSETS_DIR}/.updating"
/usr/bin/open --background xbar://app.xbarapp.com/refreshPlugin?path=${SCRIPT_NAME}
echo "Finished brew upgrade --greedy-auto-updates" | add_date | tee -a "${LOG_FILE}"
fi
if [[ ${1} == 'upgrade-all-formulae' ]]; then
echo "Starting brew upgrade --formula $(echo ${formulae} | tr '\n' ' ')" | add_date | tee -a "${ASSETS_DIR}/brew-upgrade.log"
touch "${ASSETS_DIR}/.updating"
/usr/bin/open --background xbar://app.xbarapp.com/refreshPlugin?path=${SCRIPT_NAME}
sleep 1
brew upgrade --formula $(echo ${formulae} | tr '\n' ' ') 2>&1 | add_date | tee -a "${ASSETS_DIR}/brew-upgrade.log"
errors=$(cat "${LOG_FILE}" | grep 'Error:' | awk -F ":" '{print $4}' | sort -u)
if [[ $errors ]]; then
echo "$errors" >> "${ERR_FILE}"
fi
sleep 1
rm "${ASSETS_DIR}/.updating"
/usr/bin/open --background xbar://app.xbarapp.com/refreshPlugin?path=${SCRIPT_NAME}
echo "Finished brew upgrade --formula --greedy-auto-updates" | add_date | tee -a "${ASSETS_DIR}/brew-upgrade.log"
fi
if [[ ${1} == 'upgrade-all-casks' ]]; then
echo "Starting brew upgrade --cask --greedy-auto-updates $(echo ${casks} | tr '\n' ' ')" | add_date | tee -a "${ASSETS_DIR}/brew-upgrade.log"
touch "${ASSETS_DIR}/.updating"
/usr/bin/open --background xbar://app.xbarapp.com/refreshPlugin?path=${SCRIPT_NAME}
sleep 1
brew upgrade --cask --greedy-auto-updates $(echo ${casks} | tr '\n' ' ') 2>&1 | add_date | tee -a "${ASSETS_DIR}/brew-upgrade.log"
errors=$(cat "${LOG_FILE}" | grep 'Error:' | awk -F ":" '{print $4}' | sort -u)
if [[ $errors ]]; then
echo "$errors" >> "${ERR_FILE}"
fi
sleep 1
rm "${ASSETS_DIR}/.updating"
/usr/bin/open --background xbar://app.xbarapp.com/refreshPlugin?path=${SCRIPT_NAME}
echo "Finished brew upgrade --cask --greedy-auto-updates" | add_date | tee -a "${ASSETS_DIR}/brew-upgrade.log"
fi
if [[ ${1} == 'cleanup' ]]; then
echo "Starting brew cleanup" | add_date | tee -a "${LOG_FILE}"
brew cleanup
echo "Finished brew cleanup" | add_date | tee -a "${LOG_FILE}"
fi
fi
fi