-
Notifications
You must be signed in to change notification settings - Fork 0
/
try
executable file
·306 lines (246 loc) · 8.77 KB
/
try
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
#!/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=SC2164 # Use 'cd ... || exit' or 'cd ... || return' in case cd fails.
#shellcheck disable=SC2120 # references arguments, but none are ever passed.
#shellcheck disable=SC2086 # Double quote to prevent globbing and word splitting.
##################
# util functions #
##################
[ -f "$HOME"/.custom-git.try ] && rm "$HOME"/.custom-git.try
__CG_MOVE_UP=$(tput cuu 1)
__CG_CLEAR_LINE=$(tput el 1)
alias __CG_MUCL='echo ${__CG_MOVE_UP}${__CG_CLEAR_LINE}${__CG_MOVE_UP}'
function __cg_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: __cg_insert_new_lines 3
function __cg_insert_new_lines() {
for (( i = 0; i < ${1}; i++)); do
echo
done
}
# Usage: __cg_banner_color 1 "red" "*" "my title" 2
function __cg_banner_color() {
__cg_insert_new_lines ${1}
__cg_apply_tput "${2}"
__cg_apply_tput bold
local msg="${3} ${4} ${3}"
local edge
edge=${msg//?/$3}
echo "${edge}"
echo "${msg}"
printf "%s" "${edge}"
__cg_apply_tput reset
__cg_insert_new_lines ${5}
}
# Usage: __cg_banner_color_err 1 "error msg" 2
function __cg_banner_color_err() {
__cg_banner_color ${1} "red" "~" "[ERROR] ${2}" ${3}
}
# Usage: __cg_print_as 3 "blue" "msg" 1
function __cg_print_as() {
__cg_insert_new_lines ${1}; shift
while [[ $# -gt 2 ]]; do
__cg_apply_tput "${1}"; shift
done
local msg="${1}"; shift
printf "%s" "${msg}"
__cg_apply_tput reset
__cg_insert_new_lines ${1};
}
# Usage: __cg_print_info 3 "msg" 2
function __cg_print_info() {
__cg_print_as ${1} "blue" "[INFO] ${2}" ${3}
}
# Usage: __cg_print_err 3 "msg" 2
function __cg_print_err() {
__cg_print_as ${1} "red" "[ERROR] ${2}" ${3}
}
function __cg_is_command_installed() {
if command -v "${1}" &>/dev/null; then
echo true
else
echo false
fi
}
###########################
# Enable custom-git trial #
###########################
__cg_insert_new_lines 1
__cg_shells=("bash" "zsh")
__cg_shellsNotFound=0
for shell in "${__cg_shells[@]}"; do
if ! command -v "$shell" > /dev/null; then
__cg_shellsNotFound=$((__cg_shellsNotFound + 1))
fi
done
if [[ ${#__cg_shells[@]} -eq $__cg_shellsNotFound ]]; then
__cg_print_err 0 "custom-git is only supported for bash and zsh shells. Stopping installation." 2
return
fi
__CG_SUCCESS=0
__CG_FAILURE=1
__cg_prefix='~/.custom-git'
__cg_currDir="$(pwd)"
__cg_restart_shell=false
export CUSTOM_GIT_HOME="$HOME"/.custom-git-trial
export CUSTOM_CONSOLE_HOME="$CUSTOM_GIT_HOME"/custom-console-bash
function __cg_install_clipboard() {
__cg_clipboard_installation_status=$__CG_SUCCESS
if ! $(__cg_is_command_installed "npm"); then
__cg_clipboard_installation_status=$__CG_FAILURE
__cg_banner_color_err 1 "npm not installed." 2
return
fi
__cg_print_info 1 "Installing clipboard-cli ..." 1
sudo npm install -g clipboard-cli &> /dev/null
if (( $? != __CG_SUCCESS )); then
__cg_clipboard_installation_status=$__CG_FAILURE
__CG_MUCL
__cg_banner_color_err 0 "clipboard-cli installation failed." 2
return
fi
__CG_MUCL
__cg_banner_color 0 "magenta" "*" "clipboard-cli installed." 2
}
if ! $(__cg_is_command_installed "clipboard"); then
__cg_print_info 0 "Some custom-git commands use " 0
__cg_print_as 0 "magenta" "bold" "clipboard-cli " 1
__cg_print_as 0 "blue" "Allow me to install it on your system? (y/n) : " 0
read -r
if [[ "$REPLY" != "y" ]]; then
__cg_print_info 1 "Please install clipboard-cli from " 0
__cg_print_as 0 "magenta" "bold" "\"https://github.com/sindresorhus/clipboard-cli\"" 1
__cg_print_info 0 "Some custom-git commands may not work correctly." 2
else
__cg_install_clipboard
if [[ $__cg_clipboard_installation_status -eq $__CG_FAILURE ]]; then
__cg_print_info 0 "Please install clipboard-cli from " 0
__cg_print_as 0 "magenta" "bold" "\"https://github.com/sindresorhus/clipboard-cli\"" 1
__cg_print_info 0 "Some custom-git commands may not work correctly." 2
fi
fi
fi
function __cg_install_fzf() {
__cg_print_info 1 "Installing fzf ..." 1
__cg_fzf_installation_status=$__CG_SUCCESS
git clone --depth 1 https://github.com/junegunn/fzf.git "$HOME"/.fzf &> /dev/null
cd "$HOME"/.fzf || {
__cg_fzf_installation_status=$__CG_FAILURE
__CG_MUCL
__cg_banner_color_err 0 "couldn't enter directory: $HOME/.fzf" 2
return
}
./install --all &> /dev/null
if (( $? != __CG_SUCCESS )); then
__cg_fzf_installation_status=$__CG_FAILURE
__CG_MUCL
__cg_banner_color_err 0 "fzf installation failed." 2
return
fi
__CG_MUCL
__cg_banner_color 0 "magenta" "*" "fzf installed (restart your shell)" 2
__cg_restart_shell=true
cd "$__cg_currDir" || {
__cg_banner_color_err 0 "couldn't enter directory: $__cg_currDir" 2
return
}
}
if ! $(__cg_is_command_installed "fzf"); then
__cg_print_info 0 "custom-git requires " 0
__cg_print_as 0 "magenta" "bold" "fzf (fuzzy finder)" 0
__cg_print_as 0 "blue" " to work." 1
__cg_print_as 0 "blue" "Allow me to install it on your system? (y/n) : " 0
read -r
if [[ "$REPLY" != "y" ]]; then
__cg_print_info 1 "Please install fzf from " 0
__cg_print_as 0 "magenta" "bold" "https://github.com/junegunn/fzf#installation" 1
__cg_print_as 0 "blue" "and try again..." 2
return
fi
__cg_install_fzf
if [[ $__cg_fzf_installation_status -eq $__CG_FAILURE ]]; then
__cg_print_info 0 "Please install fzf from " 0
__cg_print_as 0 "magenta" "bold" "https://github.com/junegunn/fzf#installation" 1
__cg_print_as 0 "blue" "and try again..." 2
return
fi
fi
if $__cg_restart_shell; then
__cg_print_info 0 "After shell restart, run the 'try' command again." 1
printf '
if command -v curl &>/dev/null; then
curl -fsSL -o ~/.custom-git.try https://custom-git.io/try
source ~/.custom-git.try
else
wget -q -O ~/.custom-git.try https://custom-git.io/try
source ~/.custom-git.try
fi
'
return
fi
if [[ -d "$CUSTOM_GIT_HOME" ]]; then
cd "${CUSTOM_GIT_HOME}"
git switch integ &> /dev/null
__cg_print_info 0 "Fetching latest integ branch of custom-git" 1
git pull &> /dev/null
if (( $? != __CG_SUCCESS )); then
__CG_MUCL
__cg_banner_color_err 0 "Some error occurred while fetching custom-git. Please check your network." 2
cd "$__cg_currDir" || __cg_banner_color_err 1 "couldn't enter directory: $__cg_currDir" 2
return
fi
cd "$__cg_currDir" || __cg_banner_color_err 1 "couldn't enter directory: $__cg_currDir" 2
else
__cg_print_info 0 "Enabling custom-git commands..." 1
git clone --recurse-submodules --single-branch --branch integ\
https://github.com/custom-git/custom-git-bash.git "$CUSTOM_GIT_HOME" &> /dev/null
if (( $? != __CG_SUCCESS )); then
__CG_MUCL
__cg_banner_color_err 0 "Some error occurred while fetching custom-git. Please check your network." 2
return
fi
if [[ ! -d "$CUSTOM_GIT_HOME" ]]; then
__CG_MUCL
__cg_banner_color_err 0 "$(printf "Couldn't create the directory %s. Exiting." "$__cg_prefix")" 2
return
fi
fi
export __CUSTOM_GIT_UTIL="$CUSTOM_GIT_HOME"/util
export __CUSTOM_CONSOLE_UTIL="$CUSTOM_CONSOLE_HOME"/util
if [[ ! "$PATH" == *$CUSTOM_CONSOLE_HOME/cmd* ]]; then
export PATH="$CUSTOM_CONSOLE_HOME/cmd${PATH:+:${PATH}}"
fi
if [[ ! "$PATH" == *$CUSTOM_GIT_HOME/cmd* ]]; then
export PATH="$CUSTOM_GIT_HOME/cmd${PATH:+:${PATH}}"
fi
export FZF_DEFAULT_OPTS="--reverse --border --ansi --preview-window wrap:down:70% --tabstop=4"
export FZF_DEFAULT_OPTS="${FZF_DEFAULT_OPTS} --color='preview-fg:-1'"
__CG_MUCL
__cg_banner_color 0 "magenta" "*" "All the custom-git commands are enabled for this shell session. Existing installation won't be affected." 2
__cg_print_info 0 "For more details, visit: " 0
__cg_print_as 0 "magenta" "bold" "https://github.com/custom-git/custom-git-bash" 2