-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuninstall
326 lines (274 loc) · 7.66 KB
/
uninstall
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
#!/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.
#shellcheck disable=SC2181 # Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.
#shellcheck disable=SC2155 # Declare and assign separately to avoid masking return values.
##################
# util functions #
##################
SUCCESS=0
FAILURE=1
MOVE_UP=$(tput cuu 1)
CLEAR_LINE=$(tput el 1)
function mucl() {
echo "${MOVE_UP}${CLEAR_LINE}${MOVE_UP}"
}
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 ask() {
while true; do
read -p "$1 ([y]/n) " -r
REPLY=${REPLY:-"y"}
if [[ $REPLY =~ ^[Yy]$ ]]; then
return 0
elif [[ $REPLY =~ ^[Nn]$ ]]; then
return 1
fi
done
}
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
}
function remove_file() {
local file="${1}"
[ -f "${file}" ] && {
print_info 0 "removing ${file}" 1
rm -f "${file}"
}
}
function remove_dir() {
local dir="${1}"
[ -d "${dir}" ] && {
print_info 0 "removing ${dir}" 1
rm -rf "${dir}"
}
}
function remove_line() {
src=$(readlink "$1")
if [ $? -eq 0 ]; then
print_info 0 "removing from ${1} ($src):" 1
else
src=$1
print_info 0 "removing from ${1}:" 1
fi
shift
line_no=1
match=0
while [ -n "$1" ]; do
line=$(sed -n "$line_no,\$p" "$src" | \grep -m1 -nF "$1")
if [ $? -ne 0 ]; then
shift
line_no=1
continue
fi
line_no=$(( $(sed 's/:.*//' <<< "$line") + line_no - 1 ))
content=$(sed 's/^[0-9]*://' <<< "$line")
match=1
echo " - line #$line_no: $content"
[ "$content" = "$1" ] || ask " - remove?"
if [ $? -eq 0 ]; then
awk -v n=$line_no 'NR == n {next} {print}' "$src" > "$src.bak" &&
mv "$src.bak" "$src" || break
echo " - removed"
else
echo " - skipped"
line_no=$(( line_no + 1 ))
fi
done
[ $match -eq 0 ] && echo " - nothing found"
echo
}
# Usage: assert_equal A B msg
function assert_equal() {
local A="${1}" B="${2}" msg="${3}"
[[ "${A}" != "${B}" ]] && {
mucl
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 handle_v0.1.0_install_issue() {
local currVersion="$(get_latest_annotated_tag)"
if [[ "${currVersion}" == "" || "${currVersion}" == "v0.1.0" ]]; then
for shell in "${shells[@]}"; do
local initFile="${CUSTOM_GIT_HOME}.${shell}"
[ -f "${initFile}" ] && {
remove_file "${initFile}"
remove_line ~/.${shell}rc \
"[ -f ${prefix}.${shell} ] && source ${prefix}.${shell}" \
"source ${prefix}.${shell}"
}
done
fi
}
function finish {
cd "${currDir}" || {
banner_color_err 0 "couldn't enter the directory: ${currDir}" 2
exit ${FAILURE}
}
}
trap finish EXIT
########################
# Uninstall custom-git #
########################
insert_new_lines 1
CUSTOM_GIT_HOME="${HOME}"/.custom-git
CUSTOM_GIT_WEBSITE_URL="https://custom-git.io"
if [[ ! -d "${CUSTOM_GIT_HOME}" ]]; then
banner_color_err 0 "custom-git is not installed on your system" 2
print_info 0 "for more details, visit: " 0
print_as 0 "magenta" "bold" "${CUSTOM_GIT_WEBSITE_URL}" 2
exit ${FAILURE}
fi
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
banner_color_err 0 "custom-git is only supported for bash and zsh shells" 2
print_info 0 "for more details, visit: " 0
print_as 0 "magenta" "bold" "${CUSTOM_GIT_WEBSITE_URL}" 2
exit ${FAILURE}
fi
prefix='~/.custom-git'
cd "${CUSTOM_GIT_HOME}" || {
banner_color_err 0 "couldn't enter the directory: ${CUSTOM_GIT_HOME}" 2
exit ${FAILURE}
}
handle_v0.1.0_install_issue
for shell in "${shells[@]}"; do
initFile="${HOME}/.${shell}rc"
[ -f "${initFile}" ] && {
remove_line ~/.${shell}rc \
"[ -f ${prefix}/init.${shell} ] && source ${prefix}/init.${shell}" \
"source ${prefix}/init.${shell}"
}
done
remove_dir "${CUSTOM_GIT_HOME}"
banner_color 1 "red" "#" "custom-git uninstalled." 1
print_info 0 "to re-install, visit: " 0
print_as 0 "magenta" "bold" "${CUSTOM_GIT_WEBSITE_URL}/setup#-install" 1
print_info 1 "to disable 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=$(uname -sm)
[[ "$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