install.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #!/usr/bin/env bash
  2. # bash-it installer
  3. # Show how to use this installer
  4. function _bash-it_show_usage() {
  5. echo -e "\n$0 : Install bash-it"
  6. echo -e "Usage:\n$0 [arguments] \n"
  7. echo "Arguments:"
  8. echo "--help (-h): Display this help message"
  9. echo "--silent (-s): Install default settings without prompting for input"
  10. echo "--interactive (-i): Interactively choose plugins"
  11. echo "--no-modify-config (-n): Do not modify existing config file"
  12. echo "--append-to-config (-a): Keep existing config file and append bash-it templates at the end"
  13. echo "--overwrite-backup (-f): Overwrite existing backup"
  14. }
  15. # enable a thing
  16. function _bash-it_load_one() {
  17. file_type=$1
  18. file_to_enable=$2
  19. mkdir -p "$BASH_IT/${file_type}/enabled"
  20. dest="${BASH_IT}/${file_type}/enabled/${file_to_enable}"
  21. if [ ! -e "${dest}" ]; then
  22. ln -sf "../available/${file_to_enable}" "${dest}"
  23. else
  24. echo "File ${dest} exists, skipping"
  25. fi
  26. }
  27. # Interactively enable several things
  28. function _bash-it_load_some() {
  29. file_type=$1
  30. single_type=$(echo "$file_type" | sed -e "s/aliases$/alias/g" | sed -e "s/plugins$/plugin/g")
  31. enable_func="_enable-$single_type"
  32. [ -d "$BASH_IT/$file_type/enabled" ] || mkdir "$BASH_IT/$file_type/enabled"
  33. for path in "$BASH_IT/${file_type}/available/"[^_]*; do
  34. file_name=$(basename "$path")
  35. while true; do
  36. just_the_name="${file_name%%.*}"
  37. read -r -e -n 1 -p "Would you like to enable the $just_the_name $file_type? [y/N] " RESP
  38. case $RESP in
  39. [yY])
  40. $enable_func "$just_the_name"
  41. break
  42. ;;
  43. [nN] | "")
  44. break
  45. ;;
  46. *)
  47. echo -e "\033[91mPlease choose y or n.\033[m"
  48. ;;
  49. esac
  50. done
  51. done
  52. }
  53. # Back up existing profile
  54. function _bash-it_backup() {
  55. test -w "$HOME/$CONFIG_FILE" \
  56. && cp -aL "$HOME/$CONFIG_FILE" "$HOME/$CONFIG_FILE.bak" \
  57. && echo -e "\033[0;32mYour original $CONFIG_FILE has been backed up to $CONFIG_FILE.bak\033[0m"
  58. }
  59. # Back up existing profile and create new one for bash-it
  60. function _bash-it_backup_new() {
  61. _bash-it_backup
  62. sed "s|{{BASH_IT}}|$BASH_IT|" "$BASH_IT/template/bash_profile.template.bash" > "$HOME/$CONFIG_FILE"
  63. echo -e "\033[0;32mCopied the template $CONFIG_FILE into ~/$CONFIG_FILE, edit this file to customize bash-it\033[0m"
  64. }
  65. # Back up existing profile and append bash-it templates at the end
  66. function _bash-it_backup_append() {
  67. _bash-it_backup
  68. (sed "s|{{BASH_IT}}|$BASH_IT|" "$BASH_IT/template/bash_profile.template.bash" | tail -n +2) >> "$HOME/$CONFIG_FILE"
  69. echo -e "\033[0;32mBash-it template has been added to your $CONFIG_FILE\033[0m"
  70. }
  71. function _bash-it_check_for_backup() {
  72. if ! [[ -e "$HOME/$BACKUP_FILE" ]]; then
  73. return
  74. fi
  75. echo -e "\033[0;33mBackup file already exists. Make sure to backup your .bashrc before running this installation.\033[0m" >&2
  76. if [[ -z "${overwrite_backup}" ]]; then
  77. while [[ -z "${silent}" ]]; do
  78. read -e -n 1 -r -p "Would you like to overwrite the existing backup? This will delete your existing backup file ($HOME/$BACKUP_FILE) [y/N] " RESP
  79. case $RESP in
  80. [yY])
  81. overwrite_backup=true
  82. break
  83. ;;
  84. [nN] | "")
  85. break
  86. ;;
  87. *)
  88. echo -e "\033[91mPlease choose y or n.\033[m"
  89. ;;
  90. esac
  91. done
  92. fi
  93. if [[ -z "${overwrite_backup}" ]]; then
  94. echo -e "\033[91mInstallation aborted. Please come back soon!\033[m"
  95. if [[ -n "${silent}" ]]; then
  96. echo -e "\033[91mUse \"-f\" flag to force overwrite of backup.\033[m"
  97. fi
  98. exit 1
  99. else
  100. echo -e "\033[0;32mOverwriting backup...\033[m"
  101. fi
  102. }
  103. function _bash-it_modify_config_files() {
  104. _bash-it_check_for_backup
  105. if [[ -z "${silent}" ]]; then
  106. while [[ -z "${append_to_config}" ]]; do
  107. read -e -n 1 -r -p "Would you like to keep your $CONFIG_FILE and append bash-it templates at the end? [y/N] " choice
  108. case $choice in
  109. [yY])
  110. append_to_config=true
  111. break
  112. ;;
  113. [nN] | "")
  114. break
  115. ;;
  116. *)
  117. echo -e "\033[91mPlease choose y or n.\033[m"
  118. ;;
  119. esac
  120. done
  121. fi
  122. if [[ -n "${append_to_config}" ]]; then
  123. # backup/append
  124. _bash-it_backup_append
  125. else
  126. # backup/new by default
  127. _bash-it_backup_new
  128. fi
  129. }
  130. for param in "$@"; do
  131. shift
  132. case "$param" in
  133. "--help") set -- "$@" "-h" ;;
  134. "--silent") set -- "$@" "-s" ;;
  135. "--interactive") set -- "$@" "-i" ;;
  136. "--no-modify-config") set -- "$@" "-n" ;;
  137. "--append-to-config") set -- "$@" "-a" ;;
  138. "--overwrite-backup") set -- "$@" "-f" ;;
  139. *) set -- "$@" "$param" ;;
  140. esac
  141. done
  142. OPTIND=1
  143. while getopts "hsinaf" opt; do
  144. case "$opt" in
  145. "h")
  146. _bash-it_show_usage
  147. exit 0
  148. ;;
  149. "s") silent=true ;;
  150. "i") interactive=true ;;
  151. "n") no_modify_config=true ;;
  152. "a") append_to_config=true ;;
  153. "f") overwrite_backup=true ;;
  154. "?")
  155. _bash-it_show_usage >&2
  156. exit 1
  157. ;;
  158. esac
  159. done
  160. shift $((OPTIND - 1))
  161. if [[ -n "${silent}" && -n "${interactive}" ]]; then
  162. echo -e "\033[91mOptions --silent and --interactive are mutually exclusive. Please choose one or the other.\033[m"
  163. exit 1
  164. fi
  165. if [[ -n "${no_modify_config}" && -n "${append_to_config}" ]]; then
  166. echo -e "\033[91mOptions --no-modify-config and --append-to-config are mutually exclusive. Please choose one or the other.\033[m"
  167. exit 1
  168. fi
  169. BASH_IT="$(cd "${BASH_SOURCE%/*}" && pwd)"
  170. case $OSTYPE in
  171. darwin*)
  172. CONFIG_FILE=.bash_profile
  173. ;;
  174. *)
  175. CONFIG_FILE=.bashrc
  176. ;;
  177. esac
  178. BACKUP_FILE=$CONFIG_FILE.bak
  179. echo "Installing bash-it"
  180. if [[ -z "${no_modify_config}" ]]; then
  181. _bash-it_modify_config_files
  182. fi
  183. # Disable auto-reload in case its enabled
  184. export BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE=''
  185. # Load dependencies for enabling components
  186. # shellcheck disable=SC1090
  187. source "${BASH_IT}"/vendor/github.com/erichs/composure/composure.sh
  188. # shellcheck source=./lib/utilities.bash
  189. source "$BASH_IT/lib/utilities.bash"
  190. # shellcheck source=./lib/log.bash
  191. source "${BASH_IT}/lib/log.bash"
  192. cite _about _param _example _group _author _version
  193. # shellcheck source=./lib/helpers.bash
  194. source "$BASH_IT/lib/helpers.bash"
  195. if [[ -n $interactive && -z "${silent}" ]]; then
  196. for type in "aliases" "plugins" "completion"; do
  197. echo -e "\033[0;32mEnabling ${type}\033[0m"
  198. _bash-it_load_some "$type"
  199. done
  200. else
  201. echo ""
  202. _bash-it-profile-load "default"
  203. fi
  204. echo ""
  205. echo -e "\033[0;32mInstallation finished successfully! Enjoy bash-it!\033[0m"
  206. # shellcheck disable=SC2086
  207. echo -e "\033[0;32mTo start using it, open a new tab or 'source "~/$CONFIG_FILE"'.\033[0m"
  208. echo ""
  209. echo "To show the available aliases/completions/plugins, type one of the following:"
  210. echo " bash-it show aliases"
  211. echo " bash-it show completions"
  212. echo " bash-it show plugins"
  213. echo ""
  214. echo "To avoid issues and to keep your shell lean, please enable only features you really want to use."
  215. echo "Enabling everything can lead to issues."