-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup
More file actions
executable file
·428 lines (355 loc) · 11.6 KB
/
setup
File metadata and controls
executable file
·428 lines (355 loc) · 11.6 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/env zsh
set -euo pipefail
export XDG_CONFIG_HOME="$HOME/.config"
export XDG_CACHE_HOME="$HOME/.cache"
export XDG_DATA_HOME="$HOME/.local/share"
export ZDOTDIR="$XDG_CONFIG_HOME/zsh"
declare -r githubUrl='https://github.com/5ouma/dotfiles'
declare -r dotfiles="$HOME/.dotfiles"
declare -r config="$dotfiles/config"
declare -r data="$dotfiles/data"
declare -r TMPDIR="${TMPDIR:-"$(mktemp -d)"}"
declare -r GIT_REF="${GITHUB_HEAD_REF:-main}"
export GUM_CHOOSE_CURSOR_FOREGROUND='33'
export GUM_CHOOSE_ITEM_FOREGROUND='39'
export GUM_CHOOSE_SELECTED_FOREGROUND='27'
export GUM_CONFIRM_SELECTED_BACKGROUND='27'
export GUM_FORMAT_THEME="$TMPDIR/blue.json"
export GUM_SPIN_SPINNER_FOREGROUND='27'
declare executed=false
declare skipped=false
declare goThrough=false
getGum() {
declare -r gumVir='0.17.0'
declare -r arch="$(uname -m)"
declare -r gumDir="gum_${gumVir}_$(uname)_${arch/aarch64/arm64}"
declare -r gumUrl="https://github.com/charmbracelet/gum/releases/download/v$gumVir/$gumDir.tar.gz"
declare -r gumThemeUrl="https://raw.githubusercontent.com/5ouma/dotfiles/$GIT_REF/data/blue.json"
# shellcheck disable=SC2139
if (! type gum &>/dev/null); then
printf ' 🚚💨 Now delivering...'
curl -sL "$gumUrl" | tar --strip-components=1 -zxC "$TMPDIR" "$gumDir/gum" && alias gumCom="$TMPDIR/gum"
printf '\r '
else
alias gumCom="$(which gum)"
fi
if [[ ! -f "$data/blue.json" ]]; then
curl -s "$gumThemeUrl"
else
cat "$data/${gumThemeUrl/*\//}"
fi >"$GUM_FORMAT_THEME"
} && getGum
gum() {
case $1 in
choose) gumCom choose --no-limit --selected='*' <"$2" ;;
confirm) gumCom confirm "$2" --affirmative="$3" --negative="$4" ;;
format) gumCom format "${@:2}" ;;
style) gumCom style --foreground="$2" "${@:3}" ;;
executed)
echo -en '\n ' && gumCom format '✅ Done!'
executed=true
;;
run)
if (tty &>/dev/null); then
gumCom spin --title="$2" -- "${@:4}"
else
echo && gumCom log --level='info' "$2"
"${@:4}"
fi
;;
skipped) echo -en '\n ' && gumCom format '🟡 Skipped' ;;
esac
}
getDot() {
if [[ ! -d "$dotfiles" ]]; then
# shellcheck disable=SC2016
gum run 'Cloning the repository into `~/.dotfiles`...' -- git clone "$githubUrl" -b "$GIT_REF" "$dotfiles"
else
gum run 'Pulling the dotfiles repository...' -- git -C "$dotfiles" pull
fi
}
checkSym() {
declare -Ag cfgs
while read -r cfg; do
declare file="${cfg#"$config"/*/}"
if (! isDarwin); then
file="${file/'Library\/Application Support'/.config}"
fi
if [[ ! -f "$HOME/$file" ]] || (! diff "$cfg" "$HOME/$file" &>/dev/null); then
cfgs[$file]=$cfg
fi
done < <(find "$config" -type f ! -name '.DS_Store')
((${#cfgs} > 0))
}
mkSym() {
# shellcheck disable=SC2296
while read -r file; do
mkdir -p "$(dirname "$HOME/$file")"
if [[ -f "$HOME/$file" || -L "$HOME/$file" ]]; then
mkdir -p "$HOME/.backup"
mv "$HOME/$file" "$HOME/.backup/$(date +'%H%M%S')_$(basename "$file")"
fi
ln -s "${cfgs[$file]}" "$HOME/$file"
# shellcheck disable=SC2088
printf "- \033[36m%s\033[m\n" "~/$file"
done < <(printf "%s\n" "${(k)cfgs[@]}" | sort) | gum format
unset cfgs
}
has() {
type "$1" &>/dev/null
}
alias skip='skipped=true && return'
shouldProceed() {
($goThrough || gum confirm 'Proceed?' 'Yes' 'No')
}
run() {
declare -r func="${1:u}"
gum format "$2"
(${3:-false} && ! isDarwin) && eval 'declare SETUP_SKIP_'"$func"'=true'
if (eval '${SETUP_SKIP_'"$func"':-false}'); then
gum skipped
return
fi
eval '$1'
if ($skipped); then
skipped=false
gum skipped
else
gum executed
fi
}
isDarwin() {
[[ "$(uname)" == 'Darwin' ]]
}
helpMsg() {
gum format <<EOM
## Usage: setup
> ⚙️ The most minimal and powerful dotfiles ever.
## Commands:
- help Print help information
- set Set files to home directory
## Flags:
- -y, --yes Start without confirmation
EOM
}
errMsg() {
declare -r stat=$1 func="${2/"$HOME"/~}" line=$3
gum format <<EOM
> **$(gum style 1 'Error')**
> Unknown error has occurred!
> - Status: $stat
> - File or Function: $func
> - Line Number: $line
> - Process ID: $$
EOM
} && trap 'errMsg $? "$0" $LINENO' ERR
if [[ ! -x $0 ]]; then
gum format <<EOM
# Welcome to ⚙️ dotfiles setup!
> $githubUrl
---
EOM
fi
if (($# == 0)); then
if (! isDarwin); then
gum format <<EOM
> **$(gum style 3 'Warning')**
> Homebrew requires some dependencies on Linux.
> [](https://docs.brew.sh/Homebrew-on-Linux#requirements)
EOM
gum confirm 'Are you ready to run setup?' 'Yes!' 'No' || exit 1
fi
else
case "$1" in
'help')
helpMsg
exit
;;
'set')
getDot && checkSym && mkSym
exit
;;
'-y' | '--yes')
goThrough=true
export NONINTERACTIVE=1
;;
*)
gum format <<EOM
> **$(gum style 1 'Error')**
> Unknown flag $1
EOM
helpMsg
exit 1
;;
esac
fi
if (! $goThrough); then
gum confirm "Let's start setup!" 'Yes!' 'No' || exit 1
fi
if (isDarwin && ! xcode-select -p &>/dev/null); then
xcode-select --install
gum confirm 'Continue when you done' 'Done' ''
fi
getDot
installHomebrew() {
if (type brew &>/dev/null || ! shouldProceed); then
skip
fi
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
if (isDarwin); then
eval "$(/opt/homebrew/bin/brew shellenv)"
else
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
fi
} && run 'installHomebrew' '## 🍺 Install Homebrew'
setSymlink() {
if ! checkSym || ! shouldProceed; then
skip
fi
mkSym
} && run 'setSymlink' '## 🔗 Symlink files'
# shellcheck disable=SC2016
enableTouchID() {
if [[ -f '/etc/pam.d/sudo_local' ]] || ! shouldProceed; then
skip
fi
sed -e 's/^#auth/auth/' /etc/pam.d/sudo_local.template | sudo tee /etc/pam.d/sudo_local >/dev/null
} && run 'enableTouchID' '## 👑 Enable Touch ID for `sudo`' true
disableRedesignedTextCursor() {
if sudo [ -f '/Library/Preferences/FeatureFlags/Domain/UIKit.plist' ] || ! shouldProceed; then
skip
fi
sudo mkdir -p /Library/Preferences/FeatureFlags/Domain
sudo /usr/libexec/PlistBuddy -c "Add 'redesigned_text_cursor:Enabled' bool false" /Library/Preferences/FeatureFlags/Domain/UIKit.plist
} && run 'disableRedesignedTextCursor' '## 🔤 Disable redesigned text cursor' true
disablePressAndHold() {
declare -r pressAndHold="$(defaults read -g ApplePressAndHoldEnabled)" &>/dev/null
if [[ "$pressAndHold" == 0 ]] || ! shouldProceed; then
skip
fi
defaults write -g ApplePressAndHoldEnabled -bool false
} && run 'disablePressAndHold' '## ⌨️ Disable press-and-hold for keys' true
changeScreenCapturePath() {
declare -r screenCaptureLocation="$(defaults read com.apple.screencapture location)" screenCaptures="$HOME/Pictures/Screen Captures" &>/dev/null
if [[ "$screenCaptureLocation" == "$screenCaptures" ]] || ! shouldProceed; then
skip
fi
mkdir -p "$screenCaptures"
defaults write com.apple.screencapture location "$screenCaptures"
} && run 'changeScreenCapturePath' '## 📷 Change screen capture path' true
setComputerName() {
declare -r userName="$(id -F)" modelName="$(system_profiler SPHardwareDataType | grep 'Model Name' | cut -d ':' -f 2 | xargs)"
declare -r computerName="$userName's $modelName" localHostName="${userName//./}s-${modelName// /-}"
if [[ "$(scutil --get ComputerName)" == "$computerName" && "$(scutil --get HostName)" == "$computerName" && "$(scutil --get LocalHostName)" == "$localHostName" ]] || ! shouldProceed; then
skip
fi
gum run 'Setting ComputerName...' -- sudo scutil --set ComputerName "$computerName"
gum run 'Setting HostName...' -- sudo scutil --set HostName "$computerName"
gum run 'Setting LocalHostName...' -- sudo scutil --set LocalHostName "$localHostName"
gum format <<EOM
- ComputerName: $(gum style 6 "$(sudo scutil --get ComputerName)")
- HostName: $(gum style 6 "$(sudo scutil --get HostName)")
- LocalHostName: $(gum style 6 "$(sudo scutil --get LocalHostName)")
EOM
} && run 'setComputerName' '## 💻 Set the computer name' true
installApps() {
(! isDarwin) && export HOMEBREW_BUNDLE_BREW_SKIP='batt mac-cleanup-go mas trash 5ouma/tap/dorg 5ouma/tap/mksei 5ouma/tap/mli'
if (gum run "Satisfying Brewfile's dependencies..." -- brew bundle check --file="$data/Brewfile" || ! shouldProceed); then
skip
fi
if ($goThrough); then
cat "$data/Brewfile"
else
gum choose "$data/Brewfile"
fi >"$TMPDIR/Brewfile"
brew bundle install --file="$TMPDIR/Brewfile"
[[ -d '/Applications/Xcode.app' ]] && sudo xcodebuild -license accept || :
} && run 'installApps' '## 📲 Install commands and apps'
changeLoginShell() {
if [[ "${SHELL:-''}" == "$(which zsh)" ]] || ! shouldProceed; then
skip
fi
which zsh | sudo tee -a /etc/shells >/dev/null
sudo chsh -s "$(which zsh)" "$USER"
} && run 'changeLoginShell' '## 🐚 Change login shell to Zsh'
restoreDock() {
if (! has dorg || dorg check --file="$data/dorg.yml" &>/dev/null || ! shouldProceed); then
skip
fi
# shellcheck disable=SC2016
gum run 'Running `dorg load`...' -- dorg load --file="$data/dorg.yml"
} && run 'restoreDock' '## 🚥 Restore Dock' true
restoreAppSettings() {
if (! shouldProceed); then
skip
fi
mackup restore
} && run 'restoreAppSettings' '## 💾 Restore application settings' true
restoreAppShortcuts() {
if (defaults read com.apple.universalaccess com.apple.custommenu.apps &>/dev/null || ! shouldProceed); then
skip
fi
# shellcheck disable=SC2016
gum run 'Running `mksei load`...' -- mksei load "$data/keyboard_shortcuts.json"
} && run 'restoreAppShortcuts' '## ⌨️ Restore App Shortcuts' true
restoreLoginItems() {
if (! has mli || mli check --file="$data/login_items.json" &>/dev/null || ! shouldProceed); then
skip
fi
# shellcheck disable=SC2016
gum run 'Running `mli load`...' -- mli load --file="$data/login_items.json"
} && run 'restoreLoginItems' '## 📑 Restore Login Items' true
limitBatteryCharge() {
if (! has batt || batt status &>/dev/null || ! shouldProceed); then
skip
fi
sudo brew services start batt
sleep 1
sudo batt magsafe-led enable || :
} && run 'limitBatteryCharge' '## 🔋 Limit battery charge' true
importGitHubGPGKey() {
if (gpg --list-keys | grep -q 'github' || ! shouldProceed); then
skip
fi
curl -s https://github.com/web-flow.gpg | gpg --import
} && run 'importGitHubGPGKey' '## 🔐 Import GitHub GPG Key'
generateGitHooks() {
declare -r hooks=('pre-commit' 'commit-msg' 'pre-push')
declare allPresent=true
for hook in "${hooks[@]}"; do
if [[ ! -f "$XDG_CONFIG_HOME/git/hooks/$hook" ]]; then
allPresent=false
break
fi
done
if ("$allPresent" || ! shouldProceed); then
skip
fi
prek -c="$data/prek.yml" init-template-dir "$XDG_CONFIG_HOME/git"
} && run 'generateGitHooks' '## 🪝 Generate Git Hooks'
loginGitHubCli() {
if (gh auth status &>/dev/null || ! shouldProceed); then
skip
fi
gh auth login -p='SSH' -h='GitHub.com' -s='codespace,delete_repo,workflow' --skip-ssh-key -w
} && run 'loginGitHubCli' '## 🐙 Log in to GitHub CLI'
installProgrammingLanguages() {
eval "$(mise activate -q zsh)"
if [[ -z "$(mise ls --missing)" ]] || ! shouldProceed; then
skip
fi
mise install
} && run 'installProgrammingLanguages' '## 📼 Install programming languages'
installVimPlugins() {
if [[ -d "$XDG_DATA_HOME/vim/pack/jetpack/opt" ]] || ! shouldProceed; then
skip
fi
vim +Jetpack +qall
gum run 'Building a binary of vim-lumen...' -- vim +qall!
} && run 'installVimPlugins' '## 🖌 Install Vim plugins'
if ($executed); then
(isDarwin) && killall Dock SystemUIServer
gum format '# 🎉 Setup completed!'
else
gum format '# 🧊 Nothing has changed'
fi