|
| 1 | +#!/usr/bin/env bash |
| 2 | +#> +-----------------------+ |
| 3 | +#> | custom_functions.sh | |
| 4 | +#> +-----------------------+ |
| 5 | +#- |
| 6 | +#- SYNOPSIS |
| 7 | +#- |
| 8 | +#- ./custom_functions.sh [-h] |
| 9 | +#- |
| 10 | +#- OPTIONS |
| 11 | +#- |
| 12 | +#- -y, all accept. |
| 13 | +#- -h, --help print help information. |
| 14 | +#- -no_nvm do not build extra function in nvm |
| 15 | +#- -no_pipenv_correspond do not build pipenv_correspond() |
| 16 | +#- |
| 17 | +#- |
| 18 | +#- EXAMPLES |
| 19 | +#- |
| 20 | +#- $ ./custom_functions.sh -y |
| 21 | + |
| 22 | +#==================================================== |
| 23 | +# Part 1. Option Tool |
| 24 | +#==================================================== |
| 25 | +# Print script help |
| 26 | +function show_script_help(){ |
| 27 | + echo |
| 28 | + head -17 $0 | # find this file top 16 lines. |
| 29 | + grep "^#[-|>]" | # show the line that include "#-" or "#>". |
| 30 | + sed -e "s/^#[-|>]*//1" # use nothing to replace "#-" or "#>" that the first keyword in every line. |
| 31 | + echo |
| 32 | +} |
| 33 | +# Receive arguments in slient mode. |
| 34 | +all_accept=0 |
| 35 | +no_nvm=false |
| 36 | +no_pipenv_correspond=false |
| 37 | + |
| 38 | +if [ "$#" -gt 0 ]; then |
| 39 | + while [ "$#" -gt 0 ]; do |
| 40 | + case "$1" in |
| 41 | + # Help |
| 42 | + "-h"|"--help") |
| 43 | + show_script_help |
| 44 | + exit 1 |
| 45 | + ;; |
| 46 | + # All Accept |
| 47 | + "-y") |
| 48 | + all_accept=1 |
| 49 | + shift 1 |
| 50 | + ;; |
| 51 | + "-no_nvm") |
| 52 | + no_nvm=true |
| 53 | + ;; |
| 54 | + "-no_pipenv_correspond") |
| 55 | + no_pipenv_correspond=true |
| 56 | + ;; |
| 57 | + esac |
| 58 | + done |
| 59 | +fi |
| 60 | + |
| 61 | +function Echo_Color(){ |
| 62 | + case $1 in |
| 63 | + r* | R* ) |
| 64 | + COLOR='\e[31m' |
| 65 | + ;; |
| 66 | + g* | G* ) |
| 67 | + COLOR='\e[32m' |
| 68 | + ;; |
| 69 | + y* | Y* ) |
| 70 | + COLOR='\e[33m' |
| 71 | + ;; |
| 72 | + b* | B* ) |
| 73 | + COLOR='\e[34m' |
| 74 | + ;; |
| 75 | + *) |
| 76 | + echo "$COLOR Wrong COLOR keyword!\e[0m" |
| 77 | + ;; |
| 78 | + esac |
| 79 | + echo -e "$COLOR$2\e[0m" |
| 80 | +} |
| 81 | + |
| 82 | +function Ask_yn(){ |
| 83 | + printf "\e[33m$1\e[0m\e[33m [y/n] \e[0m" |
| 84 | + if [ $all_accept = 1 ]; then |
| 85 | + printf "-y\n" |
| 86 | + return 1 |
| 87 | + fi |
| 88 | + read respond |
| 89 | + if [ "$respond" = "y" -o "$respond" = "Y" -o "$respond" = "" ]; then |
| 90 | + return 1 |
| 91 | + elif [ "$respond" = "n" -o "$respond" = "N" ]; then |
| 92 | + return 0 |
| 93 | + else |
| 94 | + Echo_Color r 'wrong command!!' |
| 95 | + Ask_yn $1 |
| 96 | + return $? |
| 97 | + fi |
| 98 | + unset respond |
| 99 | +} |
| 100 | + |
| 101 | +#==================================================== |
| 102 | +# Part 2. Main |
| 103 | +#==================================================== |
| 104 | +custom_func_keyword="~/.customfunction" |
| 105 | +custom_func_root=~/.customfunction |
| 106 | + |
| 107 | +func_begin_arr=($(grep -xn "^\S*(){" ./config/.customfunction)) # \S: match non-whitespace character |
| 108 | +func_end_arr=($(grep -xn "}" ./config/.customfunction)) |
| 109 | + |
| 110 | +function Get_function_code(){ |
| 111 | + for key in ${!func_end_arr[*]}; do |
| 112 | + if echo ${func_begin_arr[$key]} | grep --silent -i $1; then |
| 113 | + declare -i begin=$(echo ${func_begin_arr[$key]} | cut -d ':' -f 1)-1 |
| 114 | + declare -i end=$(echo ${func_end_arr[$key]} | cut -d ':' -f 1)+1 |
| 115 | + function_code=$(sed -n "$begin,$end p" ./config/.customfunction) |
| 116 | + fi |
| 117 | + done |
| 118 | +} |
| 119 | + |
| 120 | +case $SHELL in |
| 121 | + *zsh ) |
| 122 | + shell=zsh |
| 123 | + profile=~/.zshrc |
| 124 | + ;; |
| 125 | + *bash ) |
| 126 | + shell=bash |
| 127 | + profile=~/.bashrc |
| 128 | + ;; |
| 129 | + *ksh ) |
| 130 | + shell=ksh |
| 131 | + profile=~/.profile |
| 132 | + ;; |
| 133 | + * ) |
| 134 | + Echo_Color r "Unknow shell, need to manually add $custom_func_keyword on your shell profile!!" |
| 135 | + ;; |
| 136 | +esac |
| 137 | + |
| 138 | +echo '#!/usr/bin/env bash' > $custom_func_root |
| 139 | +if grep -n "source $custom_func_keyword" $profile; then |
| 140 | + echo "$custom_func_keyword is already in the $profile" |
| 141 | +else |
| 142 | + Echo_Color y "Write: source $custom_func_keyword into $profile" |
| 143 | + printf "\n# custom func. by self\nsource $custom_func_keyword\n" >> $profile |
| 144 | +fi |
| 145 | + |
| 146 | +if ! $no_nvm; then |
| 147 | + # delete original nvm setting in the profile |
| 148 | + num_original_line=$(grep -xn 'export NVM_DIR="$HOME/.nvm"' $profile | head -n 1 | cut -d ':' -f 1) |
| 149 | + if [ "$num_original_line" -gt 0 ]; then |
| 150 | + Echo_Color y "remove the office support method..." |
| 151 | + sed -i "$num_original_line, $(($num_original_line + 2))d" $profile |
| 152 | + fi |
| 153 | + |
| 154 | + Get_function_code "nvm"; printf "\n$function_code\n" >> $custom_func_root |
| 155 | +fi |
| 156 | + |
| 157 | +if ! $no_pipenv_correspond; then |
| 158 | + Get_function_code "pipenv_correspond"; printf "\n$function_code\n" >> $custom_func_root |
| 159 | +fi |
| 160 | + |
| 161 | +Echo_Color g "Done config!!" |
0 commit comments