-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
348 lines (325 loc) · 9.37 KB
/
install.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
#!/usr/bin/env bash
set -e
set -o pipefail
set +o posix
# Homebrew 需要安装的 formulas 软件包列表
HOMEBREW_FORMULAS_LIST=(
bat
coreutils
ctags
diffutils
findutils
fzf
gawk
gibo
git
git-extras
gnu-getopt
gnu-tar
gnu-sed
go
grep
htop
hugo
iproute2mac
jq
less
lrzsz
lua
luarocks
make
mdbook
moreutils
nmap
oath-toolkit
python
ripgrep
rust
shellcheck
sshpass
telnet
tig
tmux
tokei
tree
unzip
watch
wtf
kubernetes-cli
kubebuilder
kind
istioctl
helm
kustomize
kompose
)
# Homebrew 需要安装的 casks 软件包列表
HOMEBREW_CASKS_LIST=(
adrive
android-platform-tools
citrix-workspace
clash-verge-rev
dash
docker
gas-mask
google-chrome
iterm2
iina
microsoft-openjdk@21
microsoft-remote-desktop
qq
qqmusic
telegram
tencent-lemon
tencent-meeting
the-unarchiver
vagrant
virtualbox
visual-studio-code
wechat
wireshark
)
# 设置备份目录变量
BACKUP_DIR="${HOME}/.backup"
# 初始化全局变量 homebrew 已安装软件包列表
INSTALLED_FORMULAS_LIST=()
INSTALLED_CASKS_LIST=()
while IFS='' read -r line; do INSTALLED_FORMULAS_LIST+=("$line"); done < <(brew list --formula)
while IFS='' read -r line; do INSTALLED_CASKS_LIST+=("$line"); done < <(brew list --cask)
# 设置所有国内加速镜像地址
export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export NVM_NODEJS_ORG_MIRROR="https://registry.npmmirror.com/node"
PIP_INDEX_URL="https://pypi.tuna.tsinghua.edu.cn/simple"
NPM_MIRROR_REGISTRY="https://registry.npmmirror.com"
# 创建备份目录
create_backup_dir() {
if ! [ -d "${BACKUP_DIR}" ]; then
mkdir -p "${BACKUP_DIR}"
fi
}
# 安装 homebrew
install_homebrew() {
if ! command -v brew &>/dev/null; then
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
fi
}
# 安装 ohmyzsh 和自定义插件
install_ohmyzsh() {
if ! [ -d "${HOME}/.oh-my-zsh" ]; then
git clone --depth=1 --branch master https://github.com/ohmyzsh/ohmyzsh.git "${HOME}/.oh-my-zsh"
fi
if ! [ -d "${HOME}/.oh-my-zsh/custom/themes/powerlevel10k" ]; then
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git "${HOME}/.oh-my-zsh/custom/themes/powerlevel10k"
fi
if ! [ -d "${HOME}/.oh-my-zsh/custom/plugins/zsh-autosuggestions" ]; then
git clone --depth=1 https://github.com/zsh-users/zsh-autosuggestions "${HOME}/.oh-my-zsh/custom/plugins/zsh-autosuggestions"
fi
if ! [ -d "${HOME}/.oh-my-zsh/custom/plugins/you-should-use" ]; then
git clone --depth=1 https://github.com/MichaelAquilina/zsh-you-should-use.git "${HOME}/.oh-my-zsh/custom/plugins/you-should-use"
fi
if ! [ -d "${HOME}/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting" ]; then
git clone --depth=1 https://github.com/zsh-users/zsh-syntax-highlighting.git "${HOME}/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting"
fi
if ! [ -d "${HOME}/.oh-my-zsh/custom/plugins/zsh-completions" ]; then
git clone --depth=1 https://github.com/zsh-users/zsh-completions.git "${HOME}/.oh-my-zsh/custom/plugins/zsh-completions"
fi
}
# 安装 homebrew formulas 列表中的软件
install_homebrew_formulas_packages() {
local NEED_INSTALL_FORMULAS_LIST=()
local item
for item in "${HOMEBREW_FORMULAS_LIST[@]}"; do
if ! [[ ${INSTALLED_FORMULAS_LIST[*]} =~ ${item} ]]; then
NEED_INSTALL_FORMULAS_LIST+=("${item}")
fi
done
if [ "${#NEED_INSTALL_FORMULAS_LIST[@]}" -ne 0 ]; then
brew install "${NEED_INSTALL_FORMULAS_LIST[@]}"
fi
}
# 安装 homebrew casks 列表中的软件
install_homebrew_casks_packages() {
local NEED_INSTALL_CASKS_LIST=()
local item
for item in "${HOMEBREW_CASKS_LIST[@]}"; do
if ! [[ ${INSTALLED_CASKS_LIST[*]} =~ ${item} ]]; then
NEED_INSTALL_CASKS_LIST+=("${item}")
fi
done
if [ "${#NEED_INSTALL_CASKS_LIST[@]}" -ne 0 ]; then
brew install --cask "${NEED_INSTALL_CASKS_LIST[@]}"
fi
}
# 安装 nvm 和 nodejs
install_nvm_nodejs() {
if ! brew list nvm &>/dev/null; then
brew install nvm
fi
export NVM_DIR="${HOME}/.nvm"
if brew --prefix nvm &>/dev/null; then
# shellcheck disable=SC1091
source "$(brew --prefix nvm)/nvm.sh"
fi
if ! nvm list lts/* &>/dev/null; then
nvm install --lts
nvm use --lts
fi
}
# 安装 pnpm 和全局软件包
install_pnpm_global_packages() {
if ! command -v pnpm &>/dev/null; then
npm install --global pnpm --registry
export PNPM_HOME="${HOME}/Library/pnpm"
export PATH="$PATH:$PNPM_HOME"
fi
local PNPM_LIST=(
bash-language-server
dockerfile-language-server-nodejs
gitmoji-cli
prettier
)
local INSTALLED_PNPM_LIST=()
local PNPM_GLOBAL_PACKAGE_JSON="${HOME}/Library/pnpm/global/5/package.json"
if [ -f "${PNPM_GLOBAL_PACKAGE_JSON}" ]; then
while IFS='' read -r line; do
INSTALLED_PNPM_LIST+=("$line")
done < <(jq -r '.dependencies|keys[]' <"${PNPM_GLOBAL_PACKAGE_JSON}")
fi
local NEED_INSTALL_PNPM_LIST=()
local item
for item in "${PNPM_LIST[@]}"; do
if ! [[ ${INSTALLED_PNPM_LIST[*]} =~ ${item} ]]; then
NEED_INSTALL_PNPM_LIST+=("${item}")
fi
done
if [ "${#NEED_INSTALL_PNPM_LIST[@]}" -ne 0 ]; then
pnpm --global add "${NEED_INSTALL_PNPM_LIST[@]}" --registry "${NPM_MIRROR_REGISTRY}"
fi
}
# 安装 Python pip 全局软件包
install_pip_packages() {
local PIP_LIST=(
neovim
s3cmd
ansible
ansible-lint
jedi-language-server
ranger-fm
mycli
pgcli
mitmproxy
)
local INSTALLED_PIP_LIST=()
while IFS='' read -r line; do INSTALLED_PIP_LIST+=("$line"); done < <(pip3 freeze | awk -F '=' '{print $1}')
local NEED_INSTALL_PIP_LIST=()
for item in "${PIP_LIST[@]}"; do
if ! [[ ${INSTALLED_PIP_LIST[*]} =~ ${item} ]]; then
NEED_INSTALL_PIP_LIST+=("${item}")
fi
done
if [ "${#NEED_INSTALL_PIP_LIST[@]}" -ne 0 ]; then
pip3 install --index-url "${PIP_INDEX_URL}" --break-system-packages "${NEED_INSTALL_PIP_LIST[@]}"
fi
}
# 安装 neovim 和 LazyVim
install_neovim() {
if ! brew list neovim &>/dev/null; then
brew install neovim
fi
if ! [ -d "${HOME}/.config/nvim" ]; then
git clone https://github.com/LazyVim/starter "${HOME}/.config/nvim"
rm -rf ~/.config/nvim/.git
fi
}
# 设置 tmux
set_tmux() {
if ! [ -d "${HOME}/.tmux" ]; then
mkdir -p "${HOME}/.tmux"
fi
if ! [ -d "${HOME}/.tmux/plugins/tpm" ]; then
git clone --depth=1 https://github.com/tmux-plugins/tpm "${HOME}/.tmux/plugins/tpm"
fi
}
# 更新 dotfiles
update_dotfiles() {
if ! diff -q .zshrc "${HOME}/.zshrc" &>/dev/null; then
[ -f "${HOME}/.zshrc" ] && mv "${HOME}/.zshrc" "${BACKUP_DIR}/.zshrc.$(date +%F-%H%M%S)"
cp .zshrc "${HOME}/.zshrc"
fi
if ! diff -q .ssh/config "${HOME}/.ssh/config" &>/dev/null; then
mkdir -p "${HOME}/.ssh"
[ -f "${HOME}/.ssh/config" ] && mv "${HOME}/.ssh/config" "${BACKUP_DIR}/ssh_config.$(date +%F-%H%M%S)"
cp .ssh/config "${HOME}/.ssh/config"
chmod 600 "${HOME}/.ssh/config"
fi
if ! diff -q .pip/pip.conf "${HOME}/.pip/pip.conf" &>/dev/null; then
mkdir -p "${HOME}/.pip"
[ -f "${HOME}/.pip/pip.conf" ] && mv "${HOME}/.pip/pip.conf" "${BACKUP_DIR}/pip.conf.$(date +%F-%H%M%S)"
cp .pip/pip.conf "${HOME}/.pip/pip.conf"
fi
if ! diff -q .npmrc "${HOME}/.npmrc" &>/dev/null; then
[ -f "${HOME}/.npmrc" ] && mv "${HOME}/.npmrc" "${BACKUP_DIR}/.npmrc.$(date +%F-%H%M%S)"
cp .npmrc "${HOME}/.npmrc"
fi
if ! diff -q .tmux.conf "${HOME}/.tmux.conf" &>/dev/null; then
[ -f "${HOME}/.tmux.conf" ] && mv "${HOME}/.tmux.conf" "${BACKUP_DIR}/.tmux.conf.$(date +%F-%H%M%S)"
cp .tmux.conf "${HOME}/.tmux.conf"
fi
}
# 设置 iterm2
set_iterm2() {
local style
local FONT_BASE_URL="https://github.com/romkatv/powerlevel10k-media/raw/master"
for style in Regular Bold Italic 'Bold Italic'; do
local FONT_FILE="MesloLGS NF ${style}.ttf"
if ! [ -f "${HOME}/Library/Fonts/$FONT_FILE" ]; then
curl -fsSL -o "${HOME}/Library/Fonts/$FONT_FILE" "$FONT_BASE_URL/${FONT_FILE// /%20}"
fi
done
if ! ls /usr/local/bin/iterm2-*.sh &>/dev/null; then
cp iterm2/iterm2-*.sh /usr/local/bin
envsubst <iterm2/com.googlecode.iterm2.plist.tpl >iterm2/com.googlecode.iterm2.plist
plutil -convert binary1 iterm2/com.googlecode.iterm2.plist -o "${HOME}/Library/Preferences/com.googlecode.iterm2.plist"
fi
}
# 设置 kubectl
set_kubectl() {
if ! [[ "$(readlink /usr/local/bin/kubectl)" =~ "kubernetes-cli" ]]; then
brew link --overwrite kubernetes-cli
fi
}
update_gitconfig() {
if ! [ -f "${HOME}/.gitconfig" ]; then
if ! diff -q .gitconfig "${HOME}/.gitconfig" &>/dev/null; then
[ -f "${HOME}/.gitconfig" ] && mv "${HOME}/.gitconfig" "${BACKUP_DIR}/.gitconfig.$(date +%F-%H%M%S)"
cp .gitconfig "${HOME}/.gitconfig"
fi
fi
}
# Change shell to zsh
chsh_zsh() {
if ! [[ "${SHELL}" =~ "zsh" ]]; then
chsh -s /bin/zsh
fi
}
main() {
create_backup_dir
install_homebrew
install_ohmyzsh
install_homebrew_formulas_packages
install_homebrew_casks_packages
install_nvm_nodejs
install_pnpm_global_packages
install_pip_packages
install_neovim
set_kubectl
update_dotfiles
set_tmux
set_iterm2
update_gitconfig
chsh_zsh
}
main