-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall
executable file
·368 lines (301 loc) · 9.42 KB
/
install
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
#!/bin/bash
#shellcheck disable=SC2184 # Quote arguments to unset so they're not glob expanded
#shellcheck disable=SC2088 # Tilde does not expand in quotes. Use $HOME
#shellcheck disable=SC2053 # Quote the right-hand side of != in [[ ]] to prevent glob matching
#shellcheck disable=SC2091 # Remove surrounding $() to avoid executing output (or use eval if intentional)
#shellcheck disable=SC2086 # Double quote to prevent globbing and word splitting
##################
# util functions #
##################
SUCCESS=0
FAILURE=1
MOVE_UP=$(tput cuu 1)
CLEAR_LINE=$(tput el 1)
archi=$(uname -sm)
# Usage: clear_last_n_lines n
# param: n number of lines to clear from the cursor
function clear_last_n_lines() {
local numIterations=${1}
for (( i = 0; i < numIterations; i++ )); do
echo "${MOVE_UP}${CLEAR_LINE}${MOVE_UP}"
done
}
currDir="$(pwd)"
function apply_tput() {
case "${1}" in
black) tput setaf 0
;;
red) tput setaf 1
;;
green) tput setaf 2
;;
yellow) tput setaf 3
;;
blue) tput setaf 4
;;
magenta) tput setaf 5
;;
cyan) tput setaf 6
;;
white) tput setaf 7
;;
reset) tput sgr0
;;
*) tput "${1}"
;;
esac
}
# Usage: insert_new_lines 3
function insert_new_lines() {
for (( i = 0; i < ${1}; i++)); do
echo
done
}
# Usage: banner_color 1 "red" "*" "my title" 2
function banner_color() {
insert_new_lines ${1}
apply_tput "${2}"
apply_tput bold
local msg="${3} ${4} ${3}"
local edge
edge=${msg//?/$3}
echo "${edge}"
echo "${msg}"
printf "%s" "${edge}"
apply_tput reset
insert_new_lines ${5}
}
# Usage: banner_color_err 1 "error message" 2
function banner_color_err() {
banner_color ${1} "red" "~" "[error] ${2}" ${3}
}
# Usage: print_as 3 "blue" "msg" 1
function print_as() {
insert_new_lines ${1}; shift
while [[ $# -gt 2 ]]; do
apply_tput "${1}"; shift
done
local msg="${1}"; shift
printf "%s" "${msg}"
apply_tput reset
insert_new_lines ${1};
}
# Usage: print_info 3 "msg" 2
function print_info() {
print_as ${1} "blue" "${2}" ${3}
}
# Usage: print_err 3 "msg" 2
function print_err() {
print_as ${1} "red" "${2}" ${3}
}
function is_command_installed() {
if command -v "${1}" &>/dev/null; then
echo true
else
echo false
fi
}
function append_line() {
set -e
local line="${1}" file="${2}" pat="${3:-}" lno=""
if [ -f "$file" ]; then
if [ $# -lt 4 ]; then
lno=$(grep -nF "$line" "$file" | sed 's/:.*//' | tr '\n' ' ')
else
lno=$(grep -nF "$pat" "$file" | sed 's/:.*//' | tr '\n' ' ')
fi
fi
if [ -z "$lno" ]; then
[ -f "$file" ] && echo >> "$file"
echo "$line" >> "$file"
fi
set +e
}
# Usage: assert_equal A B msg
function assert_equal() {
local A="${1}" B="${2}" msg="${3}"
[[ "${A}" != "${B}" ]] && {
clear_last_n_lines 1
banner_color_err 0 "${msg}" 2
exit ${FAILURE}
}
}
# Usage: assert_success msg
function assert_success() {
local status="$?" msg="${1}"
assert_equal "${status}" ${SUCCESS} "${msg}"
}
function get_latest_annotated_tag() {
git describe --abbrev=0 &> /dev/null
if [[ $? -ne ${SUCCESS} ]]; then
echo ""
return
fi
git describe --abbrev=0
}
function install_clipboard() {
if ! $(is_command_installed "npm"); then
banner_color_err 1 "npm not installed" 1
return ${FAILURE}
fi
print_info 1 "installing clipboard-cli ..." 1
npm install -g clipboard-cli &> /dev/null
if (( $? != SUCCESS )); then
clear_last_n_lines 1
banner_color 0 "black" "~" "couldn't install clipboard-cli" 1
return ${FAILURE}
fi
clear_last_n_lines 1
banner_color 0 "magenta" "*" "clipboard-cli installed" 1
}
function install_delta() {
if [[ ! "${archi}" =~ Darwin ]]; then
return ${FAILURE}
fi
if ! $(is_command_installed "brew"); then
banner_color 1 "black" "~" "brew not installed" 1
return ${FAILURE}
fi
print_info 1 "installing delta ..." 1
brew install git-delta &> /dev/null
if (( $? != SUCCESS )); then
clear_last_n_lines 1
banner_color 0 "black" "~" "couldn't install delta" 1
return ${FAILURE}
fi
clear_last_n_lines 1
banner_color 0 "magenta" "*" "delta installed" 1
# delta config settings
git config --global core.pager delta
git config --global delta.navigate true
git config --global delta.syntax-theme GitHub
git config --global delta.line-numbers true
git config --global delta.whitespace-error-style "red reverse"
git config --global delta.commit-decoration-style "blue box ul"
git config --global delta.file-style "blue bold ul"
git config --global delta.file-decoration-style none
git config --global color.diff.commit blue
return ${SUCCESS}
}
function install_fzf() {
print_info 1 "installing fzf ..." 1
git clone --depth 1 https://github.com/junegunn/fzf.git "${FZF_HOME}" &> /dev/null
if (( $? != SUCCESS )); then
clear_last_n_lines 1
banner_color_err 0 "couldn't fetch fzf! please check your network" 1
return ${FAILURE}
fi
cd "${FZF_HOME}" || {
clear_last_n_lines 1
banner_color_err 0 "couldn't enter directory: ${FZF_HOME}" 1
return ${FAILURE}
}
./install --all &> /dev/null
if (( $? != SUCCESS )); then
clear_last_n_lines 1
banner_color_err 0 "fzf installation failed" 1
return ${FAILURE}
fi
clear_last_n_lines 1
banner_color 0 "magenta" "*" "fzf installed" 1
}
function finish {
cd "${currDir}" || {
print_info 0 "couldn't enter the directory: ${currDir}" 2
exit ${FAILURE}
}
}
trap finish EXIT
#####################
# Enable custom-git #
#####################
shells=("bash" "zsh")
numShells=${#shells[@]}
for ((i = 0 ; i < numShells ; i++)); do
shell="${shells[$i]}"
if ! $(is_command_installed "${shell}"); then
unset shells[$i]
fi
done
if [[ ${#shells[@]} -eq 0 ]]; then
print_err 0 "custom-git is only supported for bash and zsh shells" 2
exit ${FAILURE}
fi
prefix='~/.custom-git'
CUSTOM_GIT_HOME="${HOME}"/.custom-git
FZF_HOME="${HOME}"/.fzf
CUSTOM_GIT_BASH_REPO_URL="https://github.com/custom-git/custom-git-bash"
CUSTOM_GIT_CLONE_URL="${CUSTOM_GIT_BASH_REPO_URL}.git"
CUSTOM_GIT_WEBSITE_URL="https://custom-git.io"
if ! $(is_command_installed "clipboard"); then
install_clipboard
if (( $? != SUCCESS )); then
print_info 0 "please install clipboard-cli from " 0
print_as 0 "magenta" "bold" "https://github.com/sindresorhus/clipboard-cli#install" 1
print_info 0 "some custom-git features will not work without it" 1
print_info 0 "for more details, visit: " 0
print_as 0 "magenta" "bold" "${CUSTOM_GIT_WEBSITE_URL}/explorer/show/commit-logs" 1
fi
fi
if ! $(is_command_installed "delta"); then
install_delta
if (( $? != SUCCESS )); then
print_info 0 "please install delta from " 0
print_as 0 "magenta" "bold" "https://github.com/dandavison/delta#installation" 1
print_info 0 "delta is a syntax-highlighting pager for git" 1
fi
fi
if ! $(is_command_installed "fzf"); then
install_fzf
if (( $? != SUCCESS )); then
print_info 0 "please install fzf from " 0
print_as 0 "magenta" "bold" "https://github.com/junegunn/fzf#installation" 1
print_as 0 "blue" "and try again..." 2
exit ${FAILURE}
fi
fi
if [[ -d "${CUSTOM_GIT_HOME}" ]]; then
print_info 0 "custom-git is already installed on your system" 2
exit ${SUCCESS}
fi
print_info 1 "installing custom-git..." 1
git clone --recurse-submodules "${CUSTOM_GIT_CLONE_URL}" "${CUSTOM_GIT_HOME}" &> /dev/null
assert_success "couldn't fetch custom-git! please check your network"
if [[ ! -d "${CUSTOM_GIT_HOME}" ]]; then
clear_last_n_lines 1
banner_color_err 0 "installation failure! couldn't create the directory ${prefix}" 2
exit ${FAILURE}
fi
cd "${CUSTOM_GIT_HOME}" || {
banner_color_err 0 "installation failure! couldn't enter the directory: ${CUSTOM_GIT_HOME}" 2
exit ${FAILURE}
}
latestVersion="$(get_latest_annotated_tag)"
git checkout "${latestVersion}" &> /dev/null
git submodule update --init --recursive &> /dev/null
assert_success "couldn't update submodule! please check your network"
for shell in "${shells[@]}"; do
[ $shell = zsh ] && dest=${ZDOTDIR:-~}/.zshrc || dest=~/.bashrc
append_line "[ -f ${prefix}/init.${shell} ] && source ${prefix}/init.${shell}" "$dest" "source ${prefix}/init.${shell}"
done
clear_last_n_lines 1
banner_color 0 "magenta" "*" "installation complete - custom-git ${latestVersion}" 1
print_info 0 "to browse the custom-git features, visit: " 0
print_as 0 "magenta" "bold" "${CUSTOM_GIT_WEBSITE_URL}/explorer" 1
print_info 1 "to enable custom-git, restart your shell session or reload the config file with:" 2
for shell in "${shells[@]}"; do
case "${shell}" in
bash)
printf "$ source ~/.bashrc # bash shell"
[[ "$archi" =~ Darwin ]] && printf " (~/.bashrc should be sourced from ~/.bash_profile)"
echo
;;
zsh)
echo "$ source ${ZDOTDIR:-~}/.zshrc # zsh shell"
;;
*)
printf "\n%s shell is not supported.\n" "${shell}"
;;
esac
done
echo