-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
303 lines (254 loc) · 8.82 KB
/
setup.sh
File metadata and controls
303 lines (254 loc) · 8.82 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
#!/usr/bin/env zsh
set -euo pipefail
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
# 询问是否使用国内镜像加速
USE_PROXY=""
echo "是否使用国内镜像源加速安装?(y/N): \c"
read -r answer
if [[ "$answer" =~ ^[Yy]$ ]]; then
USE_PROXY="1"
echo "将使用国内镜像源加速"
else
echo "使用官方源安装"
fi
export USE_PROXY
# ============================================
# fnm 和 Node.js
# ============================================
# 安装 fnm (Fast Node Manager)
if ! command -v fnm &> /dev/null; then
curl -fsSL --proto '=https' --tlsv1.2 https://fnm.vercel.app/install | bash
else
echo "fnm is already installed, skipping..."
fi
export PATH="$HOME/.local/share/fnm:$HOME/.fnm:$PATH"
eval "$(fnm env --shell zsh)"
fnm install --lts
fnm use lts-latest
fnm default lts-latest
npm install -g pnpm
if [[ -n "$USE_PROXY" ]]; then
pnpm config set registry https://registry.npmmirror.com
echo "pnpm registry set to npmmirror"
fi
# ============================================
# Python 和 uv
# ============================================
if ! command -v uv &> /dev/null; then
curl -LsSf --proto '=https' --tlsv1.2 https://astral.sh/uv/install.sh | sh
else
echo "uv is already installed, skipping..."
fi
export PATH="$HOME/.cargo/bin:$HOME/.local/bin:$PATH"
uv python install
if [[ -n "$USE_PROXY" ]]; then
# uv 使用环境变量配置镜像源
export UV_INDEX_URL="https://mirrors.aliyun.com/pypi/simple/"
# 写入 .zshenv 持久化
if ! grep -qF "UV_INDEX_URL" ~/.zshenv 2>/dev/null; then
echo 'export UV_INDEX_URL="https://mirrors.aliyun.com/pypi/simple/"' >> ~/.zshenv
fi
echo "uv pypi index url set to aliyun"
fi
uv tool install --force ruff
# ============================================
# 安装编译工具链 (Rust 需要 C 编译器)
# ============================================
if [[ "$OSTYPE" != "darwin"* ]]; then
sudo apt update
sudo apt install -y build-essential
fi
# ============================================
# Rust 和 rustup
# ============================================
if [[ ! -d ~/.rustup ]]; then
if [[ -n "$USE_PROXY" ]]; then
export RUSTUP_DIST_SERVER="https://rsproxy.cn"
export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup"
curl --proto '=https' --tlsv1.2 -sSf https://rsproxy.cn/rustup-init.sh | sh -s -- -y
else
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
fi
else
echo "rustup is already installed, skipping..."
fi
export PATH="$HOME/.cargo/bin:$PATH"
# 在 .zshenv 中 source cargo env
if ! grep -qF '. "$HOME/.cargo/env' ~/.zshenv 2>/dev/null; then
echo '' >> ~/.zshenv
echo '# rustup' >> ~/.zshenv
echo '. "$HOME/.cargo/env"' >> ~/.zshenv
echo "Added cargo/env sourcing to .zshenv"
fi
if [[ -n "$USE_PROXY" ]]; then
mkdir -p ~/.cargo
cat > ~/.cargo/config.toml << 'EOF'
[source.crates-io]
replace-with = 'rsproxy-sparse'
[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"
[source.rsproxy-sparse]
registry = "sparse+https://rsproxy.cn/index/"
[registries.rsproxy]
index = "https://rsproxy.cn/crates.io-index"
[net]
git-fetch-with-cli = true
EOF
echo "cargo registry set to rsproxy"
fi
# ============================================
# Go
# ============================================
GO_VERSION_LATEST=$(curl -s --proto '=https' --tlsv1.2 "https://go.dev/VERSION?m=text" | head -n1)
if [[ -z "$GO_VERSION_LATEST" ]]; then
echo "Failed to fetch latest Go version"
exit 1
fi
if [[ ! "$GO_VERSION_LATEST" =~ ^go[0-9]+\.[0-9]+(\.[0-9]+)?$ ]]; then
echo "Invalid Go version format: ${GO_VERSION_LATEST}"
exit 1
fi
GO_INSTALL_DIR="$HOME/sdk/${GO_VERSION_LATEST}"
GO_INSTALL_NEEDED=""
if ! command -v go &> /dev/null; then
GO_INSTALL_NEEDED="1"
else
GO_VERSION_CURRENT=$(go version | awk '{print $3}')
if [[ "$GO_VERSION_CURRENT" != "$GO_VERSION_LATEST" ]]; then
echo "Go version mismatch: current=${GO_VERSION_CURRENT}, latest=${GO_VERSION_LATEST}"
GO_INSTALL_NEEDED="1"
else
echo "Go ${GO_VERSION_CURRENT} is already the latest, skipping..."
fi
fi
if [[ -n "$GO_INSTALL_NEEDED" ]]; then
if [[ "$OSTYPE" == "darwin"* ]]; then
brew install go
else
# Linux - 安装到用户目录 ~/sdk/go<version>
GO_TAR="${GO_VERSION_LATEST}.linux-amd64.tar.gz"
mkdir -p "$HOME/sdk"
curl -LO "https://dl.google.com/go/${GO_TAR}"
tar -C "$HOME/sdk" -xzf "${GO_TAR}"
# 如果目标目录已存在,先删除
if [[ -d "${GO_INSTALL_DIR}" ]]; then
rm -rf "${GO_INSTALL_DIR}"
fi
mv "$HOME/sdk/go" "${GO_INSTALL_DIR}"
rm "${GO_TAR}"
# 创建软链接 ~/sdk/go -> ~/sdk/go<version>
ln -snf "${GO_INSTALL_DIR}" "$HOME/sdk/go"
# 创建标准路径软链接 ~/go/bin/go 和 ~/go/bin/gofmt
mkdir -p "$HOME/go/bin"
ln -snf "${GO_INSTALL_DIR}/bin/go" "$HOME/go/bin/go"
ln -snf "${GO_INSTALL_DIR}/bin/gofmt" "$HOME/go/bin/gofmt"
# 添加到 PATH
export PATH="$HOME/go/bin:$PATH"
# 创建 env 文件,cargo 风格
cat > "$HOME/go/env" << 'EOF'
#!/bin/sh
case ":${PATH}:" in
*:"$HOME/go/bin":*)
;;
*)
export PATH="$HOME/go/bin:$PATH"
;;
esac
EOF
chmod +x "$HOME/go/env"
# 在 .zshenv 中 source
if ! grep -qF '. "$HOME/go/bin/env' ~/.zshenv 2>/dev/null; then
echo '' >> ~/.zshenv
echo '# Go' >> ~/.zshenv
echo '. "$HOME/go/env"' >> ~/.zshenv
echo "Added go/env sourcing to .zshenv"
fi
echo "Installed ${GO_VERSION_LATEST} to ${GO_INSTALL_DIR}"
echo "Linked go binary to $HOME/go/bin/go"
fi
fi
if [[ -n "$USE_PROXY" ]]; then
go env -w GOPROXY=https://goproxy.cn,direct
echo "Go proxy set to goproxy.cn"
fi
go install golang.org/x/tools/cmd/goimports@latest
go install mvdan.cc/sh/v3/cmd/shfmt@latest
# ============================================
# Git 配置
# ============================================
git config --global core.excludesfile ~/.gitignore
# 先删除再创建,避免符号链接循环
rm -f ~/.gitignore
ln -s "${SCRIPT_DIR}/.gitignore" ~/.gitignore
# ============================================
# Vim 配置
# ============================================
if [[ ! -d ~/.vim_runtime ]]; then
git clone --depth=1 https://github.com/amix/vimrc.git ~/.vim_runtime
sh ~/.vim_runtime/install_awesome_vimrc.sh
echo "Installed vimrc"
else
echo "vimrc is already installed, skipping clone..."
fi
echo "是否覆盖 Vim 自定义配置 (my_configs.vim)? (y/N): \c"
read -r answer
if [[ "$answer" =~ ^[Yy]$ ]]; then
ln -snf "${SCRIPT_DIR}/config/my_configs.vim" "$HOME/.vim_runtime/my_configs.vim"
echo "Vim config updated"
else
echo "Skipping Vim config"
fi
if [[ ! -f ~/.vim/autoload/plug.vim ]]; then
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
--proto '=https' --tlsv1.2 https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
echo "Installed vim-plug"
else
echo "vim-plug is already installed, skipping download..."
fi
vim +PlugInstall +qall
echo "Vim plugins installed"
# ============================================
# VSCode 配置
# ============================================
echo "是否覆盖 VSCode 配置? (y/N): \c"
read -r answer
if [[ "$answer" =~ ^[Yy]$ ]]; then
unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) machine=Linux ;;
Darwin*) machine=Mac ;;
CYGWIN*) machine=Cygwin ;;
MINGW*) machine=MinGw ;;
MSYS_NT*) machine=MSys ;;
*) machine="UNKNOWN:${unameOut}" ;;
esac
VSCODE_SETTINGS="${SCRIPT_DIR}/editors/vscode/settings.json"
case "${machine}" in
Mac*)
if [[ -d ~/Library/Application\ Support/Code/User ]]; then
ln -snf "${VSCODE_SETTINGS}" ~/Library/Application\ Support/Code/User/settings.json
echo "Linked VSCode settings for Mac"
else
echo "VSCode config directory not found, please install VSCode first"
fi
;;
Linux*)
if [[ -d ~/.config/Code/User ]]; then
ln -snf "${VSCODE_SETTINGS}" ~/.config/Code/User/settings.json
echo "Linked VSCode settings for Linux"
elif [[ -d ~/.config/Code\ -\ Insiders/User ]]; then
ln -snf "${VSCODE_SETTINGS}" ~/.config/Code\ -\ Insiders/User/settings.json
echo "Linked VSCode Insiders settings for Linux"
elif [[ -d ~/.config/VSCodium/User ]]; then
ln -snf "${VSCODE_SETTINGS}" ~/.config/VSCodium/User/settings.json
echo "Linked VSCodium settings for Linux"
else
echo "VSCode config directory not found, please install VSCode first"
fi
;;
*) echo "Unsupported OS for VSCode settings: ${machine}" ;;
esac
else
echo "Skipping VSCode config"
fi
echo "Setup completed!"