-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrew_check.sh
More file actions
executable file
·104 lines (93 loc) · 2.48 KB
/
Copy pathbrew_check.sh
File metadata and controls
executable file
·104 lines (93 loc) · 2.48 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
#!/bin/sh
scriptname=$0
# Verify Homebrew is available
if ! command -v brew >/dev/null 2>&1; then
echo "Error: Homebrew is required. Install it from https://brew.sh/" >&2
exit 1
fi
display_usage() {
printf '\nUsage: %s [OPTION]\n' "$scriptname"
echo "Check and manage Homebrew updates."
echo ""
echo "Options:"
echo " [none] Check for available updates"
echo " -i, --install Install available updates"
echo " -clean Clean Homebrew cache"
echo " -h, --help Show this help message"
printf '\n'
}
# Parameter validation
if [ $# -gt 1 ]; then
echo "Error: Too many arguments." >&2
display_usage
exit 1
fi
# Handle options that don't require fetching update data
case $1 in
-h | --help)
display_usage
exit 0
;;
-clean)
brew cleanup -s && rm -rf "$(brew --cache)"
exit 0
;;
"" | -i | --install)
# These require fresh package data — continue below
;;
*)
echo "Error: Unknown option '$1'" >&2
display_usage
exit 1
;;
esac
# Fetch latest package data.
# Note: brew update syncs the local formula registry before checking for outdated
# packages. Without it, 'brew outdated' would report stale data. This is intentional
# — even a check-only run needs current data to be accurate.
brew update
brew_formula_outdated=$(brew outdated --formula)
brew_cask_outdated=$(brew outdated --cask --greedy)
if [ -n "$brew_formula_outdated" ]; then
brew_formula_outdated_amount=$(printf '%s\n' "$brew_formula_outdated" | wc -l)
else
brew_formula_outdated_amount=0
fi
if [ -n "$brew_cask_outdated" ]; then
brew_cask_outdated_amount=$(printf '%s\n' "$brew_cask_outdated" | wc -l)
else
brew_cask_outdated_amount=0
fi
list_updates() {
if [ "$brew_formula_outdated_amount" -eq 0 ] && [ "$brew_cask_outdated_amount" -eq 0 ]; then
echo "No updates available."
return
fi
if [ "$brew_formula_outdated_amount" -ne 0 ]; then
printf '\nAvailable formulae:\n'
echo "$brew_formula_outdated"
fi
if [ "$brew_cask_outdated_amount" -ne 0 ]; then
printf '\nAvailable applications:\n'
echo "$brew_cask_outdated"
fi
}
# Main logic
case $1 in
"")
list_updates
;;
-i | --install)
if [ "$brew_formula_outdated_amount" -eq 0 ] && [ "$brew_cask_outdated_amount" -eq 0 ]; then
echo "No updates available."
else
if [ "$brew_formula_outdated_amount" -ne 0 ]; then
brew upgrade --formula
fi
if [ "$brew_cask_outdated_amount" -ne 0 ]; then
printf '%s\n' "$brew_cask_outdated" | awk '{print $1}' | xargs -n1 brew upgrade --cask
fi
fi
brew cleanup -s && rm -rf "$(brew --cache)"
;;
esac