-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·321 lines (262 loc) · 8.65 KB
/
setup.sh
File metadata and controls
executable file
·321 lines (262 loc) · 8.65 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/bin/sh
# Automated Mac Setup Script - Updated Nov 2023
# This script installs essential command-line tools and applications using Homebrew.
# Ensure we run with bash - compatible with sh
if [ -z "${BASH_VERSION:-}" ]; then
if command -v bash >/dev/null 2>&1; then
exec bash "$0" "$@"
else
echo "Error: This script requires bash"
exit 1
fi
fi
# From here on, we're guaranteed to be in bash
# Enable strict mode for safety
set -euo pipefail
# Log the start of the script execution
LOGFILE="$HOME/mac_setup_$(date +'%Y%m%d_%H%M%S').log"
# Simple logging that works everywhere
{
echo "[$(date)] Starting Mac setup..."
# Function to keep sudo active
keep_sudo_active() {
sudo -v
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
}
# Request and keep the administrator password active.
keep_sudo_active
# Function to check the system processor
detect_architecture() {
ARCH=$(uname -m)
if [[ "$ARCH" == "arm64" ]]; then
echo "[$(date)] M1/M2/M3 Processor detected. Proceeding with compatible installations."
else
echo "[$(date)] Intel Processor detected. Proceeding with installations."
fi
}
detect_architecture
# Retry logic for network-related commands
retry() {
local n=0
local try=5
local cmd="$@"
until [ $n -ge $try ]
do
$cmd && break
n=$((n+1))
echo "Retry $n/$try failed for: $cmd"
sleep 5
done
}
# Function to install Homebrew if not already installed
install_homebrew() {
echo "[$(date)] Checking for Homebrew..."
if ! command -v brew >/dev/null 2>&1; then
echo "[$(date)] Installing Homebrew..."
retry /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Add Homebrew to PATH
echo "[$(date)] Adding Homebrew to PATH..."
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
else
echo "[$(date)] Homebrew already installed."
fi
}
install_homebrew
# Update and Upgrade Homebrew: Ensure Homebrew is up-to-date.
update_homebrew() {
echo "[$(date)] Updating and Upgrading Homebrew..."
brew update
brew upgrade
}
update_homebrew
# Install coreutils for gdate and bc for calculations
install_prerequisites() {
echo "[$(date)] Installing prerequisites..."
brew install coreutils bc
}
install_prerequisites
# Install useful command-line tools
install_cli_tools() {
echo "[$(date)] Installing command-line tools..."
# Install z - a smarter cd command
echo "[$(date)] Installing z (smart directory jumping)..."
brew install z
# Install bat - a better cat with syntax highlighting
echo "[$(date)] Installing bat (better cat with syntax highlighting)..."
brew install bat
# Install other useful tools
echo "[$(date)] Installing additional CLI tools..."
brew install tree # Display directory structure
brew install tldr # Simplified man pages
brew install jq # JSON processor
brew install ripgrep # Fast grep alternative
brew install fd # Fast find alternative
brew install htop # Better top
}
install_cli_tools
# Function to install XCode Command Line Tools
install_xcode_tools() {
echo "[$(date)] Checking for Xcode Command Line Tools..."
if ! xcode-select -p >/dev/null 2>&1; then
echo "[$(date)] Installing Xcode Command Line Tools..."
xcode-select --install
else
echo "[$(date)] Xcode Command Line Tools already installed."
fi
}
install_xcode_tools
# Finder Configuration: Set up Finder preferences like showing hidden files.
configure_finder() {
echo "[$(date)] Configuring Finder settings..."
chflags nohidden ~/Library
defaults write com.apple.finder AppleShowAllFiles YES
defaults write com.apple.finder ShowPathbar -bool true
defaults write com.apple.finder ShowStatusBar -bool true
osascript -e 'tell application "Finder" to quit'
osascript -e 'tell application "Finder" to launch'
}
configure_finder
# Terminal and Shell Setup: Install iTerm2 and Oh My Zsh.
install_terminal_tools() {
echo "[$(date)] Installing iTerm2..."
brew install --cask --appdir="/Applications" iterm2
echo "[$(date)] Installing oh-my-zsh..."
# Check if Oh My Zsh is already installed
if [ ! -d "$HOME/.oh-my-zsh" ]; then
RUNZSH=no sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
else
echo "[$(date)] Oh My Zsh already installed."
fi
# Install additional zsh plugins
echo "[$(date)] Installing zsh plugins..."
ZSH_CUSTOM=${ZSH_CUSTOM:-~/.oh-my-zsh/custom}
# Install zsh-autosuggestions
if [ ! -d "$ZSH_CUSTOM/plugins/zsh-autosuggestions" ]; then
git clone https://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions
else
echo "[$(date)] zsh-autosuggestions already installed."
fi
# Install zsh-syntax-highlighting
if [ ! -d "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting" ]; then
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH_CUSTOM/plugins/zsh-syntax-highlighting
else
echo "[$(date)] zsh-syntax-highlighting already installed."
fi
# Configure .zshrc properly
echo "[$(date)] Configuring .zshrc..."
# Backup existing .zshrc if it exists
if [ -f "$HOME/.zshrc" ]; then
backup_file ~/.zshrc
fi
# Create a new .zshrc with proper configuration
cat > ~/.zshrc << 'ZSHRC_CONFIG'
# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"
# Set name of the theme to load --- agnoster looks great with Powerline fonts
# Other good themes: robbyrussell, powerlevel10k/powerlevel10k
ZSH_THEME="agnoster"
# Disable compfix warning for insecure directories
ZSH_DISABLE_COMPFIX="true"
# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
plugins=(
git
brew
macos
z
zsh-autosuggestions
zsh-syntax-highlighting
colored-man-pages
command-not-found
)
# Load Oh My Zsh
source $ZSH/oh-my-zsh.sh
# User configuration
# Custom functions for execution time tracking
preexec() {
timer=$(gdate +%s.%N 2>/dev/null || date +%s)
}
precmd() {
if [ -n "$timer" ]; then
now=$(gdate +%s.%N 2>/dev/null || date +%s)
if command -v bc >/dev/null 2>&1 && command -v gdate >/dev/null 2>&1; then
elapsed=$(echo "$now - $timer" | bc)
timer_show=$(printf "%.2f" $elapsed)
echo "Execution time: ${timer_show}s"
fi
unset timer
fi
}
# Aliases
alias ll="ls -la"
alias la="ls -A"
alias l="ls -CF"
alias cat="bat" # Use bat instead of cat
alias ocat="/bin/cat" # Original cat if needed
# Set PATH for Homebrew
export PATH="/opt/homebrew/bin:$PATH"
# Initialize z for smart directory jumping
if command -v brew >/dev/null 2>&1; then
# Load z installed via Homebrew
if [ -f "$(brew --prefix)/etc/profile.d/z.sh" ]; then
source "$(brew --prefix)/etc/profile.d/z.sh"
fi
fi
# Bat configuration
export BAT_THEME="TwoDark" # Set a nice theme for bat
ZSHRC_CONFIG
echo "[$(date)] .zshrc configuration complete."
}
backup_file() {
local file="$1"
if [[ -f "$file" ]]; then
local backup_suffix="$(date +'%Y%m%d_%H%M%S')"
cp "$file" "${file}.bak_${backup_suffix}"
echo "[$(date)] Backed up $file to ${file}.bak_${backup_suffix}"
fi
}
install_terminal_tools
# Powerline Fonts Installation: Clone and install Powerline fonts.
install_powerline_fonts() {
echo "[$(date)] Installing Powerline fonts..."
if [ ! -d "$HOME/fonts" ]; then
retry git clone https://github.com/powerline/fonts.git "$HOME/fonts"
pushd "$HOME/fonts" && ./install.sh && popd
else
echo "[$(date)] Powerline fonts already installed."
fi
}
install_powerline_fonts
# Python and pip Installation: Install Python and pip (pip is included with Python).
install_python() {
echo "[$(date)] Checking for Python..."
if ! command -v python3 >/dev/null 2>&1; then
echo "[$(date)] Installing Python..."
brew install python
else
echo "[$(date)] Python already installed."
fi
}
install_python
# Core Applications Installation: Install essential applications using Homebrew.
install_core_apps() {
echo "[$(date)] Installing core applications..."
brew install --cask --appdir="/Applications" alfred &
brew install --cask --appdir="/Applications" visual-studio-code &
brew install --cask --appdir="/Applications" slack &
brew install --cask --appdir="/Applications" 1password &
wait
}
install_core_apps
# Clean up: Remove outdated versions from the cellar.
cleanup_homebrew() {
echo "[$(date)] Running brew cleanup..."
brew cleanup
}
cleanup_homebrew
# Ensure successful completion
echo "[$(date)] Mac setup script completed successfully."
} 2>&1 | tee -a "$LOGFILE"
exit 0