-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathbrewup.sh
More file actions
executable file
·147 lines (115 loc) · 4.31 KB
/
brewup.sh
File metadata and controls
executable file
·147 lines (115 loc) · 4.31 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
#!/bin/bash
set -euo pipefail
PATH="/usr/local/bin:/usr/local/sbin:/Users/${USER}/.local/bin:/usr/bin:/usr/sbin:/bin:/sbin"
## M1 Brew PATH Fix
if [ "$(arch)" = "arm64" ]; then
export PATH="/opt/homebrew/bin:/opt/homebrew/sbin:$PATH"
fi
## Suppress Homebrew auto-update and hints (updates handled by brew upgrade)
export HOMEBREW_NO_AUTO_UPDATE=1
export HOMEBREW_NO_ENV_HINTS=1
extract_mas_entries() {
local file="$1"
[ -f "$file" ] || return 0
grep '^mas "' "$file" || true
}
restore_previous_mas_entries() {
local previous_file="$1"
local target_file="$2"
local temp_file
local previous_mas
previous_mas=$(extract_mas_entries "$previous_file")
[ -n "$previous_mas" ] || return 0
temp_file=$(mktemp)
python3 - "$target_file" "$temp_file" "$previous_file" <<'PY'
from pathlib import Path
import sys
target_path = Path(sys.argv[1])
temp_path = Path(sys.argv[2])
previous_path = Path(sys.argv[3])
target_lines = target_path.read_text().splitlines()
previous_mas_lines = [line for line in previous_path.read_text().splitlines() if line.startswith('mas "')]
result = []
inserted = False
for line in target_lines:
if line.startswith('mas "'):
continue
if not inserted and (line.startswith('vscode "') or line.startswith('go "')):
result.extend(previous_mas_lines)
inserted = True
result.append(line)
if not inserted:
result.extend(previous_mas_lines)
temp_path.write_text("\n".join(result) + "\n")
PY
mv "$temp_file" "$target_file"
}
## checks if mas is installed, if not will install it
if ! command -v mas >/dev/null 2>&1; then
brew install mas
fi
DATE=$(date '+%Y%m%d.%H%M')
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
reset=$(tput sgr0)
brewFileName="Brewfile.$(hostname -s)"
previousBrewFile=$(mktemp)
cleanup() {
rm -f "$previousBrewFile"
}
trap cleanup EXIT
## Sets Working Dir to Script Location
if ! command -v realpath >/dev/null 2>&1; then
brew install coreutils
fi
cd "$(dirname "$(realpath "$0")")"
echo "${blue}==>${reset} Pulling latest changes from repo..."
git pull
if [ -f "./${brewFileName}" ]; then
cp "./${brewFileName}" "$previousBrewFile"
fi
## Brew Diagnostic
echo "${yellow}==>${reset} Running Brew Doctor diagnostics..."
brew doctor || true
echo "${green}==>${reset} Brew Doctor diagnostic finished."
## Brew packages update and cleanup
echo "${yellow}==>${reset} Updating Homebrew metadata..."
brew update-if-needed
echo "${green}==>${reset} Homebrew metadata updated"
echo "${yellow}==>${reset} Checking for brew updates..."
brew upgrade
brew cleanup -s || true
echo "${green}==>${reset} Finished brew updates"
## Mac Store Updates
echo "${yellow}==>${reset} Checking macOS App Store updates..."
mas upgrade || echo "${yellow}==>${reset} Warning: Mac App Store update failed (not signed in?)"
echo "${green}==>${reset} Finished macOS App Store updates"
## Creating Dump File with hostname
brew bundle dump --force --file="./${brewFileName}"
previousMasEntries=$(extract_mas_entries "$previousBrewFile")
currentMasEntries=$(extract_mas_entries "./${brewFileName}")
if [ -z "${BREWUP_ALLOW_MAS_REMOVALS:-}" ] && [ -n "$previousMasEntries" ]; then
removedMasEntries=$(comm -23 <(printf '%s\n' "$previousMasEntries" | sort) <(printf '%s\n' "$currentMasEntries" | sort) || true)
if [ -n "$removedMasEntries" ]; then
echo "${yellow}==>${reset} Warning: Detected missing Mac App Store entries in generated ${brewFileName}."
echo "${yellow}==>${reset} Restoring previous mas entries to avoid committing Spotlight indexing fallout."
echo "${yellow}==>${reset} Set BREWUP_ALLOW_MAS_REMOVALS=1 to allow intentional mas removals."
restore_previous_mas_entries "$previousBrewFile" "./${brewFileName}"
fi
fi
## Pushing to Repo
echo "${blue}==>${reset} Pushing changes to repo..."
git add "./${brewFileName}"
if ! git diff --cached --quiet; then
added=$(git diff --cached "./${brewFileName}" | grep '^+[^+]' | sed 's/^+//' | paste -sd ', ' - || true)
removed=$(git diff --cached "./${brewFileName}" | grep '^-[^-]' | sed 's/^-//' | paste -sd ', ' - || true)
msg="${DATE}_update"
[ -n "${added}" ] && msg="${msg} | Added: ${added}"
[ -n "${removed}" ] && msg="${msg} | Removed: ${removed}"
git commit -m "${msg}"
git push
else
echo "${green}==>${reset} No changes to commit"
fi
echo "${green}==>${reset} Finished updating brew and mas packages"