-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup
executable file
·361 lines (306 loc) · 11.7 KB
/
setup
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
#!/usr/bin/env zsh
set -euo pipefail
#==================================================[ Variables ]==================================================#
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}"
declare executed=false
declare skipped=false
declare goThrough=false
#==================================================[ Functions ]==================================================#
# Actions
getGum() {
declare -r gumVir='0.14.5'
declare -r gumDir="gum_${gumVir}_$(uname)_$(uname -m)"
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"
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
gumTheme=$(curl -s "$gumThemeUrl") && declare -gr gumTheme
else
gumTheme=$(cat "$data/${gumThemeUrl/*\//}") && declare -gr gumTheme
fi
} && getGum
gum() {
case $1 in
confirm) gumCom confirm "$2" --selected.background='27' --affirmative="$3" --negative="$4" ;;
format) gumCom format --theme=<(echo -E "$gumTheme") "${@:2}" ;;
run)
if (tty &>/dev/null); then
gumCom spin --spinner.foreground='27' --title="$2" -- "${@:4}"
else
echo && gumCom log --level='info' "$2"
"${@:4}"
fi
;;
style) gumCom style --foreground="$2" "${@:3}" ;;
executed)
echo -en '\n ' && gumCom format '✅ Done!'
executed=true
;;
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 [[ ! -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
}
# Utilities
alias skip='skipped=true && return'
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
#==================================================[ Welcome ]==================================================#
if [[ ! -x $0 ]]; then
gum format <<EOM
# Welcome to ⚙️ dotfiles setup!
> $githubUrl
---
EOM
fi
#==================================================[ Flags ]==================================================#
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
#==================================================[ Preparations ]==================================================#
# Ask to confirm
if (! $goThrough); then
gum confirm "Let's start setup!" 'Yes!' 'No' || exit 1
fi
# Install Command Line Tools
if (isDarwin && ! xcode-select -p &>/dev/null); then
xcode-select --install
gum confirm 'Continue when you done' 'Done' ''
fi
# Clone / Pull the repository
getDot
#==================================================[ Homebrew ]==================================================#
installHomebrew() {
(type brew &>/dev/null) && skip
/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'
#==================================================[ Files ]==================================================#
setSymlink() {
if ! checkSym; then
skip
fi
mkSym
} && run 'setSymlink' '## 🔗 Symlink files'
#==================================================[ Settings ]==================================================#
# shellcheck disable=SC2016
enableTouchID() {
[[ -f '/etc/pam.d/sudo_local' ]] && skip
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
changeLaunchpadSize() {
declare -r springboardColumns="$(defaults read com.apple.dock springboard-columns)" springboardRows="$(defaults read com.apple.dock springboard-rows)" &>/dev/null
[[ $springboardColumns == 9 && $springboardRows == 8 ]] && skip
defaults write com.apple.dock springboard-columns -int 9
defaults write com.apple.dock springboard-rows -int 8
killall Dock
} && run 'changeLaunchpadSize' '## 🚀 Change the Launchpad size' true
makeDockImmutable() {
declare -r sizeImmutable="$(defaults read com.apple.dock size-immutable)" &>/dev/null
[[ "$sizeImmutable" == 1 ]] && skip
defaults write com.apple.dock size-immutable -bool true
} && run 'makeDockImmutable' '## 🚥 Make Dock size immutable' true
changeScreenCapturePath() {
declare -r screenCaptureLocation="$(defaults read com.apple.screencapture location)" screenCaptures="$HOME/Pictures/Screen Captures" &>/dev/null
[[ "$screenCaptureLocation" == "$screenCaptures" ]] && skip
mkdir -p "$screenCaptures"
defaults write com.apple.screencapture location "$screenCaptures"
} && run 'changeScreenCapturePath' '## 📷 Change screen capture path' true
setComputerName() {
declare -r modelName="$(system_profiler SPHardwareDataType | grep 'Model Name' | cut -d ':' -f 2 | xargs)"
declare -r computerName="$(id -F)'s $modelName" localHostName="$(id -F)s-${modelName// /-}"
[[ "$(scutil --get ComputerName)" == "$computerName" && "$(scutil --get HostName)" == "$computerName" && "$(scutil --get LocalHostName)" == "$localHostName" ]] && skip
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
#==================================================[ Apps ]==================================================#
installApps() {
(! isDarwin) && export HOMEBREW_BUNDLE_BREW_SKIP='newrelic-infra-agent 5ouma/formula/mksei 5ouma/formula/mli blacktop/tap/lporg lslqtz/formulae/bclm_loop'
(gum run "Satisfying Brewfile's dependencies..." -- brew bundle check --file="$data/Brewfile") && skip
brew update && brew bundle install --no-lock --file="$data/Brewfile"
if (isDarwin); then
sudo xcodebuild -license accept
fi
} && run 'installApps' '## 📲 Install commands and apps'
changeLoginShell() {
[[ "${SHELL:-''}" == "$(which zsh)" ]] && skip
which zsh | sudo tee -a /etc/shells >/dev/null
sudo chsh -s "$(which zsh)" "$USER"
} && run 'changeLoginShell' '### 🐚 Change login shell to Zsh'
restoreLaunchpad() {
lporg save -c="$TMPDIR/lporg.yml" &>/dev/null
(diff "$data/lporg.yml" "$TMPDIR/lporg.yml" &>/dev/null) && skip
# shellcheck disable=SC2016
gum run 'Running `lporg load`...' -- lporg load -nyc="$data/lporg.yml" >/dev/null || :
} && run 'restoreLaunchpad' '## 🚀 Restore Launchpad and Dock' true
restoreAppSettings() {
[[ -z "$(mackup -n backup 2>/dev/null)" && $? ]] && skip
mackup restore
} && run 'restoreAppSettings' '## 💾 Restore application settings' true
restoreAppShortcuts() {
(defaults read com.apple.universalaccess com.apple.custommenu.apps &>/dev/null) && skip
# shellcheck disable=SC2016
gum run 'Running `mksei load`...' -- mksei load "$data/keyboard_shortcuts.json"
} && run 'restoreAppShortcuts' '## ⌨️ Restore App Shortcuts' true
restoreLoginItems() {
(mli check --file="$data/login_items.json" &>/dev/null) && skip
# shellcheck disable=SC2016
gum run 'Running `mli load`...' -- mli load --file="$data/login_items.json"
} && run 'restoreLoginItems' '## 📑 Restore Login Items' true
limitBatteryCharge() {
sudo bclm_loop persist
} && run 'limitBatteryCharge' '## 🔋 Limit battery charge' true
generateGitHooks() {
[[ -f "$XDG_CONFIG_HOME/git/hooks/pre-commit" && -f "$XDG_CONFIG_HOME/git/hooks/commit-msg" ]] && skip
pre-commit init-templatedir "$XDG_CONFIG_HOME/git" -c="$data/pre-commit.yml" | sed -n 1p
gitlint --target="$dotfiles" install-hook
} && run 'generateGitHooks' '## 🪝 Generate Git Hooks for pre-commit and commit-msg'
loginGitHubCli() {
(gh auth status) && skip
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)"
[ -z "$(mise ls --missing)" ] && skip
mise install
} && run 'installProgrammingLanguages' '## 📼 Install programming languages'
installVimPlugins() {
[ -d "$XDG_DATA_HOME/vim/pack/jetpack/opt" ] && skip
vim +Jetpack +qall
gum run 'Building a binary of vim-lumen...' -- vim +qall!
} && run 'installVimPlugins' '## 🖌 Install Vim plugins'
installYaziPlugins() {
[ -d "$XDG_DATA_HOME/yazi/packages" ] && skip
ya pack -i
} && run 'installYaziPlugins' '## 🐤 Install Yazi plugins'
#==================================================[ Done! ]==================================================#
if ($executed); then
(isDarwin) && killall Dock SystemUIServer
gum format '# 🎉 Setup completed!'
else
gum format '# 🧊 Nothing has changed'
fi