-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·247 lines (208 loc) · 4.6 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
#!/bin/bash
DOTFILES_PATH=$(pwd)
INSTALL_PATH=$HOME
# borrowed from oh-my-zsh
setup_color() {
# Only use colors if connected to a terminal
if [ -t 1 ]; then
RED=$(printf '\033[31m')
GREEN=$(printf '\033[32m')
YELLOW=$(printf '\033[33m')
BLUE=$(printf '\033[34m')
BOLD=$(printf '\033[1m')
RESET=$(printf '\033[m')
else
RED=""
GREEN=""
YELLOW=""
BLUE=""
BOLD=""
RESET=""
fi
}
colored_print() {
color=$1
msg=$2
printf "${color}${msg}${RESET}"
}
info() {
colored_print ${BLUE} "$1\n"
}
act() {
colored_print ${BOLD} "$1\n"
}
warn() {
colored_print ${YELLOW} "$1\n"
}
error() {
colored_print ${RED}${BOLD} "$1\n"
}
ask() {
vared -p "${BOLD}$1${RESET} [Y/n]: " -c ans
if [[ ${ans} =~ ^[Yy]$ ]]; then
return 1
elif [[ ${ans} =~ ^[Nn]$ ]]; then
return 0
fi
return 1
}
check_omz() {
# must go first as some ENVs depends on this
if [[ -z "$ZSH" ]]; then
error "install.sh relies on oh-my-zsh\n"
act "Go to ${BOLD}${GREEN}https://ohmyz.sh/#install${RESET} and install oh-my-zsh before continue."
return 1
fi
return 0
}
build_symbol_link() {
local src_file=$1
local dst_file=$2
if test -e "$dst_file"; then
warn "${dst_file} already exists."
warn "[Remove] ${dst_file}."
rm ${dst_file}
fi
# TODO: it behaves a bit differently when symbol link a directory
ln -s "${src_file}" "${dst_file}"
act "[Create] $TO -> $FROM.\n"
}
link_config_files() {
CONFIGS=($(ls "$DOTFILES_PATH/configs"))
local select_files=$1
info "Create symbol links."
for f in "${CONFIGS[@]}"
do
local FROM="$DOTFILES_PATH/configs/$f"
local TO="$INSTALL_PATH/.$f"
if [ ${select_files} = "all" ]; then
build_symbol_link ${FROM} ${TO}
elif [ $select_files = "select" ]; then
ask " - Link ${f}?"
if [ $? -eq 1 ]; then
build_symbol_link ${FROM} ${TO}
fi
fi
done
}
link_omp_theme() {
info "Add a customized omp theme"
mkdir -p "$INSTALL_PATH/.omp-theme/"
local FROM="$DOTFILES_PATH/themes/nahnuj.omp.json"
local TO="$INSTALL_PATH/.omp-theme/nahnuj.omp.json"
build_symbol_link ${FROM} ${TO}
}
install_plugins() {
info "Install a couple oh-my-zsh plugins"
local ZSH_PLUGINS_DIR="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins"
local PLUGINS=("zsh-syntax-highlighting" "zsh-autosuggestions")
for p in ${PLUGINS[@]}; do
info "* $p"
if [[ -d "$ZSH_PLUGINS_DIR/$p" ]]; then
act "[Skip] Installed"
else
act "[Install] $p"
git clone https://github.com/zsh-users/$p.git $ZSH_PLUGINS_DIR/$p
fi
done
echo
}
install_fzf() {
info "* fzf"
if ! [[ -e ~/.fzf ]]; then
echo "${BLUE}[Install]${RESET} fzf"
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
else
act "[Skip] Installed"
fi
}
check_command() {
local command=$1
info "* $command"
if ! command -v $command &> /dev/null
then
warn "Not found $command, attempt to install now..."
return 1
else
act "[Skip] Installed"
fi
}
check_file() {
local file=$1
info "* Checking $file"
if [ -e $file ]
then
act "[Skip] ${file} exists"
else
warn "Not found $file, attempt to install now..."
return 1
fi
}
install_pip3() {
check_command "pip3"
if [ $? -ne 0 ]; then
act "[Install] pip3"
# TODO: add command here
fi
}
install_virtualenv() {
check_command "virtualenv"
if [ $? -ne 0 ]; then
act "[Install] virtualenv"
sudo pip3 install virtualenv
fi
}
install_virtualenvwrapper() {
check_file "/usr/local/bin/virtualenvwrapper.sh"
if [ $? -ne 0 ]; then
act "[Install] virtualenvwrapper"
sudo -H pip3 install virtualenvwrapper
fi
}
install_tpm() {
TPM_DIR="$HOME/.tmux/plugins/tpm"
check_file $TPM_DIR
if [ $? -ne 0 ]; then
act "[Install] TPM (Tmux Pacakge Manager)"
git clone "https://github.com/tmux-plugins/tpm" $TPM_DIR
fi
}
install_tools() {
info "Install a couple frequently used tools"
install_fzf
install_pip3
install_virtualenv
install_virtualenvwrapper
install_tpm
}
main() {
info "run from: $DOTFILES_PATH"
info "install to: $INSTALL_PATH"
setup_color
if ! check_omz; then
return 1
fi
ask "Link all the config files?"
if [ $? -eq 1 ]; then
link_config_files "all"
else
ask " - OK! Only Link selected files then?"
if [ $? -eq 1 ]; then
link_config_files "select"
else
info "Skip linking files..."
fi
fi
# oh-my-zsh only
ask "Install oh-my-zsh plugins?"
if [ $? -eq 1 ]; then
install_plugins
fi
# other stuffs
ask "Install frequently used tools?"
if [ $? -eq 1 ]; then
install_tools
fi
}
main "$@"