-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom_function.sh
177 lines (164 loc) · 4.67 KB
/
custom_function.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
#!/usr/bin/env bash
#> +-----------------------+
#> | custom_functions.sh |
#> +-----------------------+
#-
#- SYNOPSIS
#-
#- ./custom_functions.sh [-h]
#-
#- OPTIONS
#-
#- -y, all accept.
#- -h, --help print help information.
#- -no_nvm do not build extra function in nvm
#- -no_pipenv_correspond do not build pipenv_correspond()
#-
#-
#- EXAMPLES
#-
#- $ ./custom_functions.sh -y
#====================================================
# Part 1. Option Tool
#====================================================
# Print script help
function show_script_help(){
echo
head -21 $0 | # find this file top 16 lines.
grep "^#[-|>]" | # show the line that include "#-" or "#>".
sed -e "s/^#[-|>]*//1" # use nothing to replace "#-" or "#>" that the first keyword in every line.
echo
}
# Receive arguments in slient mode.
all_accept=0
no_nvm=false
no_pipenv_correspond=false
if [ "$#" -gt 0 ]; then
while [ "$#" -gt 0 ]; do
case "$1" in
# Help
"-h"|"--help")
show_script_help
exit 1
;;
# All Accept
"-y")
all_accept=1
shift 1
;;
"-no_nvm")
no_nvm=true
;;
"-no_pipenv_correspond")
no_pipenv_correspond=true
;;
esac
done
fi
custom_func_keyword="~/.customfunction"
custom_func_root=~/.customfunction
funcs_code_begin=($(grep -xn "^\S*(){" ./config/.customfunction)) # \S: match non-whitespace character
funcs_code_end=($(grep -xn "}" ./config/.customfunction))
declare -A funcs_info=([nvm]=$no_nvm [pipenv_correspond]=$no_pipenv_correspond)
function Echo_Color(){
case $1 in
r* | R* )
COLOR='\033[0;31m'
;;
g* | G* )
COLOR='\033[0;32m'
;;
y* | Y* )
COLOR='\033[0;33m'
;;
b* | B* )
COLOR='\033[0;34m'
;;
*)
echo "$COLOR Wrong COLOR keyword!\033[0m"
;;
esac
echo -e "$COLOR$2\033[0m"
}
function Ask_yn(){
printf "\033[0;33m$1\033[0m\033[0;33m [y/n] \033[0m"
if [ $all_accept = 1 ]; then
echo '-y'
return 1
fi
read respond
if [ "$respond" = "y" -o "$respond" = "Y" -o "$respond" = "" ]; then
return 1
elif [ "$respond" = "n" -o "$respond" = "N" ]; then
return 0
else
Echo_Color r 'wrong command!!'
Ask_yn $1
return $?
fi
unset respond
}
#====================================================
# Part 2. Main
#====================================================
function Get_function_code(){
declare -i key
declare -i begin
declare -i end
for key in ${!funcs_code_end[*]}; do
if echo ${funcs_code_begin[$key]} | grep --silent -i $1; then
begin=$(echo ${funcs_code_begin[$key]} | cut -d ':' -f 1)-1
end=$(echo ${funcs_code_end[$key]} | cut -d ':' -f 1)
function_code=$(sed -n "$begin,$end p" ./config/.customfunction)
fi
done
}
case $SHELL in
*zsh )
shell=zsh
profile=~/.zshrc
;;
*bash )
shell=bash
profile=~/.bashrc
;;
*ksh )
shell=ksh
profile=~/.profile
;;
* )
Echo_Color r "Unknow shell, need to manually add $custom_func_keyword on your shell profile!!"
;;
esac
if [ ! -f "$custom_func_root" ]; then
echo '#!/usr/bin/env bash' > $custom_func_root
else
Echo_Color y "You already have .customfunction"
fi
Echo_Color g "File path: $custom_func_root"
if [ "$(grep -n "source $custom_func_keyword" $profile)" != "" ]; then
Echo_Color y "$custom_func_keyword is already in the $profile"
else
Echo_Color y "Write: source $custom_func_keyword into $profile"
printf "\n# custom func. by self\nsource $custom_func_keyword\n" >> $profile
fi
if ! $no_nvm; then
# delete original nvm setting in the profile
num_original_line=$(grep -xn 'export NVM_DIR="$HOME/.nvm"' $profile | head -n 1 | cut -d ':' -f 1)
if [ "$num_original_line" != "" ]; then
Echo_Color y "remove the official support method..."
sed -i "$num_original_line, $(($num_original_line + 2))d" $profile
fi
fi
for key in ${!funcs_info[*]}; do
if ! ${funcs_info[$key]}; then
if [ "$(grep "$key" $custom_func_root)" != "" ]; then
Echo_Color y "You already have $key() in $custom_func_root."
else
# this may cause some word incurrent, but it still can work.
Get_function_code "$key"; printf "\n$function_code\n" >> $custom_func_root
fi
fi
done
source $profile
Echo_Color g "Done!! $0"