-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·298 lines (255 loc) · 8.32 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·298 lines (255 loc) · 8.32 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
#!/usr/bin/env bash
#
# Dotfiles Install & Doctor Script
#
set -e
DOTFILES_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Ensure Homebrew paths are in PATH for installer & doctor
export PATH="/opt/homebrew/bin:/usr/local/bin:$PATH"
# ANSI Color Codes
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
BOLD='\033[1m'
NC='\033[0m' # No Color
print_header() {
printf "\n%b=== %s ===%b\n" "${BLUE}${BOLD}" "$1" "${NC}"
}
ok() {
printf " [%bOK%b] %s\n" "${GREEN}" "${NC}" "$1"
}
warn() {
printf " [%bWARN%b] %s\n" "${YELLOW}" "${NC}" "$1"
}
fail() {
printf " [%bFAIL%b] %s\n" "${RED}" "${NC}" "$1"
}
symlink_file() {
local src="$1"
local dst="$2"
if [ -L "$dst" ]; then
local current_target
current_target="$(readlink "$dst")"
if [ "$current_target" = "$src" ]; then
ok "Symlink already points to $src: $dst"
return
else
warn "Updating existing symlink $dst (was $current_target)"
rm -f "$dst"
fi
elif [ -e "$dst" ]; then
warn "Backing up existing file $dst to ${dst}.bak"
mv "$dst" "${dst}.bak"
fi
ln -s "$src" "$dst"
ok "Created symlink: $dst -> $src"
}
ensure_line_in_file() {
local line="$1"
local file="$2"
if [ ! -f "$file" ]; then
echo "$line" > "$file"
ok "Created $file with configuration line."
elif grep -Fq "$line" "$file"; then
ok "Line already present in $file"
else
echo "" >> "$file"
echo "$line" >> "$file"
ok "Added configuration line to $file"
fi
}
do_install() {
print_header "Installing Dotfiles"
echo "Dotfiles directory: $DOTFILES_DIR"
# 0. Check Required System Commands
print_header "0. System Requirements Check"
local missing_count=0
for cmd in zsh git vim curl; do
if command -v "$cmd" >/dev/null 2>&1; then
ok "Found required command '$cmd': $(command -v "$cmd")"
else
fail "Required command '$cmd' is missing from PATH!"
missing_count=$((missing_count + 1))
fi
done
if [ $missing_count -gt 0 ]; then
printf "%bError: $missing_count required command(s) missing. Please install missing tools and re-run ./install.sh%b\n\n" "${RED}${BOLD}" "${NC}"
exit 1
fi
# 1. Directory & Environment Setup
print_header "1. Environment Setup"
mkdir -p "$HOME/.vim/undodir" "$HOME/.vim/backup" "$HOME/.vim/autoload"
ok "Ensured ~/.vim/undodir, ~/.vim/backup, and ~/.vim/autoload exist."
if [ ! -f "$HOME/.vim/autoload/plug.vim" ]; then
curl -fLo "$HOME/.vim/autoload/plug.vim" --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
ok "Downloaded vim-plug to ~/.vim/autoload/plug.vim"
fi
# 2. Vim Setup
print_header "2. Vim Configuration"
mkdir -p "$HOME/.vim/undodir" "$HOME/.vim/backup"
ok "Ensured ~/.vim/undodir and ~/.vim/backup exist."
local vimrc_line="set runtimepath^=$DOTFILES_DIR/vim | runtime vimrc"
ensure_line_in_file "$vimrc_line" "$HOME/.vimrc"
# 3. Zsh & Prompt Setup
print_header "3. Zsh Configuration"
if ! command -v starship >/dev/null 2>&1; then
if command -v brew >/dev/null 2>&1; then
ok "Installing Starship prompt via Homebrew..."
brew install starship || warn "Could not install Starship automatically via Homebrew."
else
warn "Starship prompt is not installed. Native Zsh vcs_info prompt fallback will be used."
fi
else
ok "Starship prompt is already installed ($(command -v starship))"
fi
mkdir -p "$HOME/.config"
symlink_file "$DOTFILES_DIR/shell/starship.toml" "$HOME/.config/starship.toml"
symlink_file "$DOTFILES_DIR/shell/starship_citc.toml" "$HOME/.config/starship_citc.toml"
local zshrc_line="source \"$DOTFILES_DIR/shell/zshrc\""
ensure_line_in_file "$zshrc_line" "$HOME/.zshrc"
# 4. Git Configs
print_header "4. Git Configuration"
symlink_file "$DOTFILES_DIR/git/.gitconfig" "$HOME/.gitconfig"
symlink_file "$DOTFILES_DIR/git/.githelpers" "$HOME/.githelpers"
symlink_file "$DOTFILES_DIR/git/.gitignore" "$HOME/.gitignore"
if [ -f "$DOTFILES_DIR/git/.gitk" ]; then
symlink_file "$DOTFILES_DIR/git/.gitk" "$HOME/.gitk"
fi
print_header "Installation Complete!"
printf "%bRun './install.sh --doctor' to verify your setup.%b\n\n" "${GREEN}" "${NC}"
}
do_doctor() {
local errors=0
local warnings=0
print_header "Running Dotfiles Doctor"
echo "Dotfiles directory: $DOTFILES_DIR"
# 1. System Requirements
print_header "System Tools"
for cmd in zsh git vim curl; do
if command -v "$cmd" >/dev/null 2>&1; then
ok "Command '$cmd' found: $(command -v "$cmd")"
else
fail "Command '$cmd' is missing from PATH."
errors=$((errors + 1))
fi
done
if [ "$SHELL" = "$(command -v zsh 2>/dev/null)" ] || [[ "$SHELL" == *"zsh"* ]]; then
ok "Default shell is Zsh ($SHELL)"
else
warn "Default shell is not Zsh (current: $SHELL). Consider running: chsh -s \$(which zsh)"
warnings=$((warnings + 1))
fi
# 2. Oh My Zsh & Theme
print_header "Zsh & Oh My Zsh"
if [ -d "$HOME/.oh-my-zsh" ]; then
ok "Oh My Zsh installed at ~/.oh-my-zsh"
else
fail "Oh My Zsh is not installed at ~/.oh-my-zsh"
errors=$((errors + 1))
fi
if command -v starship >/dev/null 2>&1; then
ok "Starship prompt is installed ($(command -v starship))"
else
warn "Starship prompt is not installed. Using native Zsh vcs_info fallback prompt. (Install via: brew install starship)"
warnings=$((warnings + 1))
fi
if [ -f "$HOME/.zshrc" ]; then
if grep -q "shell/zshrc" "$HOME/.zshrc"; then
ok "~/.zshrc correctly sources shell/zshrc"
else
warn "~/.zshrc exists but does not source shell/zshrc"
warnings=$((warnings + 1))
fi
else
fail "~/.zshrc does not exist"
errors=$((errors + 1))
fi
# 3. Vim
print_header "Vim Setup"
if [ -f "$HOME/.vimrc" ]; then
if grep -q "vimrc" "$HOME/.vimrc"; then
ok "~/.vimrc references dotfiles/vim"
else
warn "~/.vimrc exists but does not reference dotfiles/vim"
warnings=$((warnings + 1))
fi
else
fail "~/.vimrc does not exist"
errors=$((errors + 1))
fi
if [ -d "$HOME/.vim/undodir" ] && [ -d "$HOME/.vim/backup" ]; then
ok "~/.vim/undodir and ~/.vim/backup exist"
else
warn "Vim undo/backup directories (~/.vim/undodir, ~/.vim/backup) missing"
warnings=$((warnings + 1))
fi
# 4. Git Setup
print_header "Git Configuration"
for gitfile in .gitconfig .githelpers .gitignore; do
if [ -e "$HOME/$gitfile" ] || [ -L "$HOME/$gitfile" ]; then
ok "~/$gitfile is present"
else
fail "~/$gitfile is missing"
errors=$((errors + 1))
fi
done
local git_name
local git_email
git_name="$(git config --global user.name || true)"
git_email="$(git config --global user.email || true)"
if [ -n "$git_name" ]; then
ok "Git global user.name: $git_name"
else
warn "Git global user.name is not set"
warnings=$((warnings + 1))
fi
if [ -n "$git_email" ]; then
ok "Git global user.email: $git_email"
else
warn "Git global user.email is not set"
warnings=$((warnings + 1))
fi
# 5. Vim Plugins & LSP
print_header "Vim Plugins & LSP"
local plug_file="$HOME/.vim/autoload/plug.vim"
if [ -f "$plug_file" ] || [ -f "$HOME/.local/share/nvim/site/autoload/plug.vim" ]; then
ok "vim-plug plugin manager is installed"
else
warn "vim-plug is not installed yet. Launching Vim will auto-download it."
warnings=$((warnings + 1))
fi
# Summary
print_header "Doctor Summary"
if [ $errors -eq 0 ] && [ $warnings -eq 0 ]; then
printf "%bAll checks passed! Everything is configured properly.%b\n\n" "${GREEN}${BOLD}" "${NC}"
elif [ $errors -eq 0 ]; then
printf "%bPassed with %d warning(s). Run './install.sh' to fix missing configurations.%b\n\n" "${YELLOW}${BOLD}" "$warnings" "${NC}"
else
printf "%bFound %d error(s) and %d warning(s). Run './install.sh' to set up missing pieces.%b\n\n" "${RED}${BOLD}" "$errors" "$warnings" "${NC}"
fi
}
show_help() {
echo "Usage: ./install.sh [OPTION]"
echo ""
echo "Options:"
echo " --install, -i Install dotfiles and symlink configs (default)"
echo " --doctor, -d Run diagnostic checks on dotfiles setup"
echo " --help, -h Show this help message"
}
case "${1:-}" in
--doctor|-d|doctor)
do_doctor
;;
--install|-i|install|"")
do_install
;;
--help|-h|help)
show_help
;;
*)
echo "Unknown option: $1"
show_help
exit 1
;;
esac