-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzshrc.sh
391 lines (326 loc) · 10.5 KB
/
zshrc.sh
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
#!/bin/bash
# Set MACHINE_TYPE
case $(uname -s) in
Linux*) MACHINE_TYPE=Linux ;;
Darwin*)
if [ "$(uname -m)" = "arm64" ]; then
MACHINE_TYPE=Mac_aarch
else
MACHINE_TYPE=Mac_x86
fi
;;
*) MACHINE_TYPE=Unknown ;;
esac
# If the MACHINE_TYPE is Unknown exit
if [ $MACHINE_TYPE = "Unknown" ]; then
echo "Machine type '$MACHINE_TYPE' is not supported."
exit 1
fi
# Set HOMEBREW_DIRECTORY based on MACHINE_TYPE
case $MACHINE_TYPE in
Mac_aarch) export HOMEBREW_DIRECTORY=/opt/homebrew ;;
Mac_x86) export HOMEBREW_DIRECTORY=/usr/local ;;
Linux) export HOMEBREW_DIRECTORY=/home/linuxbrew/.linuxbrew ;;
esac
# CONFIGURATIONS
################################################################################
export MYZSH_INSTALLED_DIR=$HOME/.myzsh
export ANTIGEN_DIRECTORY=$HOME/.antigen
export DOCKER_DIRECTORY="$HOME/.docker"
export N_PREFIX="$HOME/.n"
export NPM_DIRECTORY=$HOME/.npm
# SET PARAMETERS
################################################################################
export LC_ALL=en_AU.UTF-8
export LANG=en_AU.UTF-8
# HELPERS
################################################################################
print_line() {
echo
echo "$@"
echo "--------------------------------------------------------------------------------"
}
myzsh_install_linux_build_essentials() {
print_line "Installing Linux build essentials"
if command -v "yum" &>/dev/null; then
# AL2
sudo yum -y groups install "buildsys-build"
sudo yum -y groups install "Development Tools"
# python-build https://github.com/pyenv/pyenv/wiki#suggested-build-environment (Fedora)
sudo yum -y install zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl11-devel xz xz-devel libffi-devel findutils
# Homebrew on Linux https://docs.brew.sh/Homebrew-on-Linux
sudo yum -y install procps-ng curl file git
fi
if command -v "apt" &>/dev/null; then
# Ubuntu
sudo apt update
# Homebrew on Linux https://docs.brew.sh/Homebrew-on-Linux
sudo apt install build-essential procps curl file git python3-pip vim
# python-build https://github.com/pyenv/pyenv/wiki#suggested-build-environment (Ubuntu)
sudo apt-get install make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
fi
}
# Homebrew
################################################################################
function myzsh_activate_homebrew() {
HOMEBREW_EXEC="$HOMEBREW_DIRECTORY/bin/brew"
if [ -f "$HOMEBREW_EXEC" ]; then
eval "$("$HOMEBREW_EXEC" shellenv)"
FPATH="$HOMEBREW_DIRECTORY/share/zsh/site-functions:${FPATH}"
export HOMEBREW_INSTALLED=0
fi
}
myzsh_is_homebrew_installed() {
if [ ! -v HOMEBREW_INSTALLED ]; then
type brew &>/dev/null
export HOMEBREW_INSTALLED="$?"
fi
return "$HOMEBREW_INSTALLED"
}
myzsh_install_homebrew() {
if ! myzsh_is_homebrew_installed; then
if [ "$MACHINE_TYPE" = Linux ]; then
myzsh_install_linux_build_essentials
fi
print_line "Installing homebrew"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
myzsh_activate_homebrew
unset HOMEBREW_INSTALLED
else
print_line "Homebrew is already installed"
fi
}
myzsh_brew_install_or_upgrade() {
myzsh_install_homebrew
if brew ls "$@" &>/dev/null; then
print_line "Brew upgrading" "$@"
HOMEBREW_NO_AUTO_UPDATE=1 brew upgrade "$@"
else
print_line "Brew installing" "$@"
HOMEBREW_NO_AUTO_UPDATE=1 brew install "$@"
fi
}
myzsh_brew_uninstall() {
if myzsh_is_homebrew_installed; then
print_line "Brew uninstalling " "$@"
HOMEBREW_NO_AUTO_UPDATE=1 brew uninstall "$@"
else
echo "Homebrew is not installed"
return 1
fi
}
# CLI goodies
################################################################################
myzsh_install_cli_tools() {
myzsh_brew_install_or_upgrade openssl readline sqlite3 xz zlib tcl-tk # required for Python build
myzsh_brew_install_or_upgrade git wget fd ripgrep fzf htop jq tree tmux lazygit shellcheck shfmt bottom bat
# Install fzf key bindings
"$(brew --prefix fzf)/install" --key-bindings --completion --no-update-rc --no-bash --no-fish
[ -f "$HOME"/.tmux.conf ] || ln -s "$MYZSH_INSTALLED_DIR"/tmux.conf "$HOME"/.tmux.conf
cp "$MYZSH_INSTALLED_DIR"/gitconfig-template "$HOME"/.gitconfig
myzsh_activate_cli_tools
}
myzsh_uninstall_cli_tools() {
myzsh_brew_uninstall git wget fd ripgrep fzf htop jq tree tmux lazygit shellcheck shfmt bottom
myzsh_brew_uninstall openssl readline sqlite3 xz zlib tcl-tk
[ -f "$HOME"/.fzf.zsh ] && rm "$HOME"/.fzf.zsh
true
}
myzsh_activate_cli_tools() {
if [ -f "$HOME"/.fzf.zsh ]; then
export FZF_DEFAULT_COMMAND='fd --type f --hidden --follow --exclude .git'
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
export FZF_ALT_C_COMMAND="fd --type d --hidden --follow --exclude .git"
bindkey "ç" fzf-cd-widget
# shellcheck disable=SC1091
source "$HOME"/.fzf.zsh
fi
}
# ANTIGEN
################################################################################
myzsh_install_antigen() {
myzsh_brew_install_or_upgrade antigen
myzsh_activate_antigen
}
myzsh_uninstall_antigen() {
myzsh_brew_uninstall antigen
rm -rf "$ANTIGEN_DIRECTORY" &>/dev/null
}
myzsh_activate_antigen() {
type antigen &>/dev/null && return
ANTIGEN_EXEC_DIRECTORY="$HOMEBREW_DIRECTORY"/opt/antigen/share/antigen
if [ -f "$ANTIGEN_EXEC_DIRECTORY"/antigen.zsh ]; then
# shellcheck disable=SC1091
source "$ANTIGEN_EXEC_DIRECTORY"/antigen.zsh
antigen use oh-my-zsh
antigen bundle key-bindings
if [ $MACHINE_TYPE = "Mac" ]; then
antigen bundle macos
fi
antigen bundle zsh-users/zsh-autosuggestions
antigen bundle zsh-users/zsh-syntax-highlighting
antigen bundle zsh-users/zsh-history-substring-search
antigen bundle git
antigen bundle tmux
antigen bundle vi-mode
antigen apply
bindkey jk vi-cmd-mode
fi
}
# Starship (shell prompt)
################################################################################
myzsh_install_starship() {
myzsh_brew_install_or_upgrade starship
myzsh_activate_starship
}
myzsh_uninstall_starship() {
myzsh_brew_uninstall starship
}
myzsh_activate_starship() {
export STARSHIP_CONFIG="$MYZSH_INSTALLED_DIR"/starship.toml
starship_activation_file="$HOMEBREW_DIRECTORY"/opt/starship/bin/starship
[ -f "$starship_activation_file" ] && eval "$($starship_activation_file init zsh)"
true
}
# RTX
################################################################################
myzsh_install_rtx() {
myzsh_brew_install_or_upgrade rtx
rtx_activation_file="$(brew --prefix rtx)/bin/rtx"
eval "$($rtx_activation_file install)"
myzsh_activate_rtx
}
myzsh_uninstall_rtx() {
myzsh_brew_uninstall rtx
rm -rf "$RTX_PREFIX"
}
myzsh_activate_rtx() {
rtx_activation_file="$HOMEBREW_DIRECTORY"/opt/rtx/bin/rtx
[ -f "$rtx_activation_file" ] && eval "$($rtx_activation_file activate zsh)"
true
}
# Python
################################################################################
myzsh_install_python() {
# https://github.com/pyenv/pyenv/wiki#suggested-build-environment
myzsh_brew_install_or_upgrade openssl readline sqlite3 xz zlib tcl-tk pyenv
print_line "Installing python $1"
pyenv install "$1"
pyenv global "$1"
myzsh_activate_pyenv
python3 -m pip install --upgrade pip
echo "Python version:"
python --version
}
myzsh_activate_python() {
pyenv_activation_file="$HOMEBREW_DIRECTORY"/opt/pyenv/bin/pyenv
[ -f "$pyenv_activation_file" ] && eval "$($pyenv_activation_file init -)"
true
}
# Node
################################################################################
myzsh_install_node() {
myzsh_brew_install_or_upgrade n
print_line "Installing NodeJS $1"
n "$1"
myzsh_activate_node
echo "NodeJS version:"
node --version
}
myzsh_activate_node() {
[ -d "$N_PREFIX"/bin ] && export PATH="$N_PREFIX/bin:$PATH"
true
}
# Rust
################################################################################
myzsh_activate_rust() {
# shellcheck disable=SC1091
[ -d "$HOME/.cargo" ] && . "$HOME/.cargo/env"
true
}
# Java
################################################################################
myzsh_install_java() {
myzsh_install_homebrew
brew tap homebrew/cask-versions
myzsh_brew_install_or_upgrade --cask temurin17
echo "Java version:"
java -version
}
myzsh_activate_java() {
JAVA_HOME=$(/usr/libexec/java_home 2>/dev/null)
export JAVA_HOME
export JAVA_TOOLS_OPTIONS="-Dlog4j2.formatMsgNoLookups=true"
true
}
# Docker
################################################################################
myzsh_activate_docker() {
# shellcheck disable=SC1091
[ -f "$DOCKER_DIRECTORY/init-zsh.sh" ] && (source "$DOCKER_DIRECTORY/init-zsh.sh" || true)
}
# Nerd Fonts
################################################################################
myzsh_install_nerd_fonts() {
myzsh_brew_install_or_upgrade font-hack-nerd-font
myzsh_brew_install_or_upgrade font-meslo-lg-nerd-font
myzsh_brew_install_or_upgrade font-fira-code-nerd-font
}
# Neovim
################################################################################
myzsh_install_neovim() {
myzsh_install_nerd_fonts
myzsh_brew_install_or_upgrade neovim stylua luarocks
}
myzsh_activate_neovim() {
neovim_binary="$HOMEBREW_DIRECTORY/bin/nvim"
[ -f "$neovim_binary" ] && export EDITOR="$neovim_binary"
true
}
myzsh_cleanup_neovim() {
rm -rf "$MYZSH_INSTALLED_DIR"/nvim/plugin/packer_compiled.lua
rm -rf "$HOME"/.config/nvim
rm -rf "$HOME"/.local/share/nvim
rm -rf "$HOME"/.local/state/nvim
rm -rf "$HOME"/.cache/nvim
}
myzsh_uninstall_neovim() {
myzsh_brew_uninstall neovim stylua luarocks
myzsh_cleanup_neovim
}
# Mac key repeat for vscode
################################################################################
myzsh_fix_key_repeat_for_vscode() {
if [ ! $MACHINE_TYPE = Linux ]; then
defaults write com.microsoft.VSCode ApplePressAndHoldEnabled -bool false
fi
}
# Keybindings
################################################################################
myzsh_keybindings() {
# shellcheck disable=SC2154
bindkey "${terminfo[kcuu1]}" history-substring-search-up
# shellcheck disable=SC2154
bindkey "${terminfo[kcud1]}" history-substring-search-down
}
# Turn off beep
setopt nobeep
myzsh_activate_homebrew
myzsh_activate_antigen
myzsh_activate_cli_tools
myzsh_activate_neovim
myzsh_activate_starship
myzsh_activate_rtx
myzsh_activate_python
myzsh_activate_node
myzsh_activate_rust
myzsh_activate_java
myzsh_activate_docker
myzsh_keybindings
[ -x "$HOME/.vocab" ] && "$HOME"/.vocab
# shellcheck disable=SC1091
[ -f "$HOME/.zshrc.local" ] && source "$HOME/.zshrc.local"
export PATH=$HOME/.local/bin:$PATH
# Aliases
alias c="clear"
date