Repository navigation
Expand file tree
/
Copy pathmac-setup-base.sh
More file actions
executable file
·299 lines (266 loc) · 9.86 KB
/
Copy pathmac-setup-base.sh
File metadata and controls
executable file
·299 lines (266 loc) · 9.86 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env bash
# Author: Miguel Barra <mbarra.git@gmail.com>
set -euo pipefail
echo "==========================================="
echo " macOS Apple Silicon setup"
echo " - Xcode Command Line Tools + license"
echo " - Homebrew (+ PATH in .zshrc)"
echo " - Oh My Zsh"
echo " - Powerlevel10k"
echo " - Fira Code Nerd Font Mono"
echo " - zsh-autosuggestions + zsh-syntax-highlighting (via brew)"
echo "==========================================="
# --------------------------------------------------
# 0) Basic check
# --------------------------------------------------
if [[ "$(uname -s)" != "Darwin" ]]; then
echo "This script only works on macOS."
exit 1
fi
# --------------------------------------------------
# 1) Xcode Command Line Tools
# --------------------------------------------------
echo "[+] Checking Xcode Command Line Tools..."
if ! xcode-select -p >/dev/null 2>&1; then
echo "Not installed. Launching graphical installer..."
xcode-select --install || true
echo
echo "Complete the installation from the macOS popup and then run this script again."
exit 1
else
echo "Xcode Command Line Tools are already installed."
fi
# --------------------------------------------------
# 1.1) Xcode license
# --------------------------------------------------
echo "[+] Checking Xcode license..."
NEEDS_LICENSE=0
# xcodebuild only works when the active developer dir is a full Xcode.app;
# with just the Command Line Tools it always errors, and no license is needed.
DEVELOPER_DIR_ACTIVE="$(xcode-select -p 2>/dev/null || true)"
if [[ "${DEVELOPER_DIR_ACTIVE}" != *.app/* ]]; then
echo "Active developer dir is the Command Line Tools (${DEVELOPER_DIR_ACTIVE}); no Xcode license needed."
elif xcodebuild -license check >/dev/null 2>&1; then
echo "The Xcode license is already accepted."
else
echo "The Xcode license is NOT accepted."
NEEDS_LICENSE=1
fi
echo "[+] Checking sudo access (may ask for your password)..."
if sudo -v; then
# Keep sudo alive
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
else
echo "Could not obtain sudo permissions. Aborting."
exit 1
fi
if [[ "$NEEDS_LICENSE" -eq 1 ]]; then
echo "[+] Attempting automatic license acceptance..."
if sudo xcodebuild -license accept >/dev/null 2>&1; then
echo "License accepted automatically."
else
echo "Could not accept automatically."
echo "Run manually:"
echo " sudo xcodebuild -license"
echo "and type: agree"
exit 1
fi
fi
# --------------------------------------------------
# 2) Homebrew
# --------------------------------------------------
ZSHRC="${HOME}/.zshrc"
ZSHRC_WAS_MISSING=0
if [[ ! -f "${ZSHRC}" ]]; then
ZSHRC_WAS_MISSING=1
touch "${ZSHRC}"
fi
if ! command -v brew >/dev/null 2>&1; then
echo "[+] Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
else
echo "[+] Homebrew is already installed."
fi
# Detect prefix
if [[ -x "/opt/homebrew/bin/brew" ]]; then
BREW_PREFIX="/opt/homebrew"
else
BREW_PREFIX="$(brew --prefix)"
fi
echo "[+] Loading Homebrew into the current session..."
eval "$("${BREW_PREFIX}/bin/brew" shellenv)"
# Add to .zshrc
BREW_SNIPPET='eval "$('"${BREW_PREFIX}"'/bin/brew shellenv)"'
if ! grep -Fq "${BREW_SNIPPET}" "${ZSHRC}" 2>/dev/null; then
echo "[+] Adding Homebrew to PATH in ~/.zshrc..."
{
echo ""
echo "# >>> Homebrew >>>"
echo "${BREW_SNIPPET}"
echo "# <<< Homebrew <<<"
} >> "${ZSHRC}"
else
echo "[+] Homebrew was already in the PATH in ~/.zshrc."
fi
# --------------------------------------------------
# 3) Oh My Zsh
# --------------------------------------------------
if [[ ! -d "${HOME}/.oh-my-zsh" ]]; then
echo "[+] Installing Oh My Zsh..."
export RUNZSH=no
export CHSH=no
# Don't let the installer move our ~/.zshrc (with the Homebrew block) aside
export KEEP_ZSHRC=yes
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
else
echo "[+] Oh My Zsh is already installed."
fi
# Ensure basic Oh My Zsh lines are in .zshrc if we just created it
if ! grep -q 'oh-my-zsh.sh' "${ZSHRC}" 2>/dev/null; then
echo "[+] Configuring oh-my-zsh in ~/.zshrc..."
{
echo ''
echo 'export ZSH="$HOME/.oh-my-zsh"'
echo 'ZSH_THEME="robbyrussell"'
echo 'plugins=(git)'
echo 'source "$ZSH/oh-my-zsh.sh"'
} >> "${ZSHRC}"
fi
# --------------------------------------------------
# 3.1) zsh-autosuggestions + zsh-syntax-highlighting (via Homebrew)
# (Must go after Oh My Zsh)
# --------------------------------------------------
echo "[+] Installing zsh-autosuggestions and zsh-syntax-highlighting (via brew)..."
# Make sure brew is available
if ! command -v brew >/dev/null 2>&1; then
echo "brew not available — make sure Homebrew is installed and in PATH."
exit 1
fi
# Install the packages if not already installed
if ! brew list zsh-autosuggestions >/dev/null 2>&1; then
echo "[+] Installing zsh-autosuggestions..."
brew install zsh-autosuggestions
else
echo "[+] zsh-autosuggestions already installed."
fi
if ! brew list zsh-syntax-highlighting >/dev/null 2>&1; then
echo "[+] Installing zsh-syntax-highlighting..."
brew install zsh-syntax-highlighting
else
echo "[+] zsh-syntax-highlighting already installed."
fi
# Paths to the scripts installed by Homebrew
ZSH_AUTOSUGGESTIONS_SCRIPT="${BREW_PREFIX}/share/zsh-autosuggestions/zsh-autosuggestions.zsh"
ZSH_SYNTAX_HIGHTLIGHT_SCRIPT="${BREW_PREFIX}/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
# Add to ~/.zshrc (zsh-syntax-highlighting must be last)
if ! grep -Fq "${ZSH_AUTOSUGGESTIONS_SCRIPT}" "${ZSHRC}" 2>/dev/null; then
echo "[+] Adding zsh-autosuggestions source to ~/.zshrc..."
{
echo ''
echo '# >>> zsh-autosuggestions (via Homebrew) >>>'
echo "if [ -f \"${ZSH_AUTOSUGGESTIONS_SCRIPT}\" ]; then"
echo " source \"${ZSH_AUTOSUGGESTIONS_SCRIPT}\""
echo "fi"
echo '# <<< zsh-autosuggestions <<<'
} >> "${ZSHRC}"
else
echo "[+] ~/.zshrc already loads zsh-autosuggestions."
fi
# Make sure zsh-syntax-highlighting is at the end of .zshrc
# (recommended by the project)
if ! grep -Fq "${ZSH_SYNTAX_HIGHTLIGHT_SCRIPT}" "${ZSHRC}" 2>/dev/null; then
echo "[+] Adding zsh-syntax-highlighting source to the end of ~/.zshrc..."
{
echo ''
echo '# >>> zsh-syntax-highlighting (via Homebrew) >>>'
echo "if [ -f \"${ZSH_SYNTAX_HIGHTLIGHT_SCRIPT}\" ]; then"
echo " source \"${ZSH_SYNTAX_HIGHTLIGHT_SCRIPT}\""
echo "fi"
echo '# <<< zsh-syntax-highlighting <<<'
} >> "${ZSHRC}"
else
echo "[+] ~/.zshrc already loads zsh-syntax-highlighting."
fi
# --------------------------------------------------
# 4) Powerlevel10k
# --------------------------------------------------
P10K_DIR="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k"
if [[ ! -d "${P10K_DIR}" ]]; then
echo "[+] Installing Powerlevel10k..."
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git "${P10K_DIR}"
else
echo "[+] Powerlevel10k is already installed."
fi
# ZSH_THEME -> powerlevel10k
if grep -q '^ZSH_THEME=' "${ZSHRC}" 2>/dev/null; then
echo "[+] Setting ZSH_THEME=powerlevel10k in ~/.zshrc..."
sed -i.bak 's/^ZSH_THEME=.*/ZSH_THEME="powerlevel10k\/powerlevel10k"/' "${ZSHRC}"
else
echo '[+] Adding ZSH_THEME="powerlevel10k/powerlevel10k" to ~/.zshrc...'
echo 'ZSH_THEME="powerlevel10k/powerlevel10k"' >> "${ZSHRC}"
fi
# Autoload .p10k.zsh
if ! grep -q '\.p10k.zsh' "${ZSHRC}" 2>/dev/null; then
echo "[+] Adding automatic load of ~/.p10k.zsh..."
{
echo ''
echo '# Load Powerlevel10k configuration if it exists'
echo '[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh'
} >> "${ZSHRC}"
fi
# --------------------------------------------------
# 5) Fira Code Nerd Font (no extra tap)
# --------------------------------------------------
echo "[+] Installing Fira Code Nerd Font Mono..."
brew install --cask font-fira-code-nerd-font
# On a fresh macOS 27 install the font registry often ignores new files in
# ~/Library/Fonts until the next login. Registering each file explicitly
# through CoreText makes them usable right away.
FONT_FILES=("${HOME}/Library/Fonts"/FiraCodeNerdFont*.ttf)
fonts_registered() {
local count
count="$(system_profiler SPFontsDataType 2>/dev/null | grep -c "Location: ${HOME}/Library/Fonts/FiraCodeNerdFont")"
[[ "${count}" -ge "${#FONT_FILES[@]}" ]]
}
if fonts_registered; then
echo "[+] FiraCode Nerd Font is registered with macOS."
else
echo "[+] macOS hasn't picked up the font files yet. Registering them with CoreText..."
REGISTER_JS="$(mktemp -t register-fonts)"
cat > "${REGISTER_JS}" <<'EOF'
ObjC.import("CoreText");
function run(argv) {
// 2 = kCTFontManagerScopeUser. A file may report "already registered"
// even when the registry hasn't loaded it; the call still loads it.
for (const p of argv) {
$.CTFontManagerRegisterFontsForURL($.NSURL.fileURLWithPath(p), 2, null);
}
}
EOF
osascript -l JavaScript "${REGISTER_JS}" "${FONT_FILES[@]}" >/dev/null 2>&1 || true
rm -f "${REGISTER_JS}"
# Registration is asynchronous; give it up to ~30s
FONT_OK=0
for _ in 1 2 3 4 5 6; do
sleep 5
if fonts_registered; then FONT_OK=1; break; fi
done
if [[ "${FONT_OK}" -eq 1 ]]; then
echo "[+] FiraCode Nerd Font is now registered with macOS."
else
echo "[!] The font files are in ~/Library/Fonts but macOS doesn't list them yet."
echo " Log out and back in (or restart) before choosing the font in your terminal."
fi
fi
# --------------------------------------------------
# END
# --------------------------------------------------
echo "==========================================="
echo "Setup completed successfully."
echo
echo "1) Close and reopen the terminal, or run:"
echo " source ~/.zshrc"
echo "2) The first time you open Powerlevel10k, the wizard will launch."
echo "3) In your terminal (iTerm2/Terminal), choose the font:"
echo " FiraCode Nerd Font Mono"
echo "==========================================="