From 1ffa41886a2311851766f17f2a0e5aa9d460be3d Mon Sep 17 00:00:00 2001 From: Peter Dave Hello Date: Sat, 23 May 2015 14:07:34 +0800 Subject: [PATCH] [New] Install bash programmble completions automatically --- install.sh | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/install.sh b/install.sh index bff211c..ff259aa 100755 --- a/install.sh +++ b/install.sh @@ -294,6 +294,8 @@ nvm_do_install() { INSTALL_DIR="$(nvm_install_dir)" SOURCE_STR="\nexport NVM_DIR=\"$INSTALL_DIR\"\n[ -s \"\$NVM_DIR/nvm.sh\" ] && \\. \"\$NVM_DIR/nvm.sh\" # This loads nvm\n" + COMPLETION_STR="[ -s \"\$NVM_DIR/bash_completion\" ] && \\. \"\$NVM_DIR/bash_completion\" # This loads nvm bash_completion\n" + BASH_OR_ZSH=false if [ -z "${NVM_PROFILE-}" ] ; then echo "=> Profile not found. Tried ${NVM_PROFILE} (as defined in \$PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile." @@ -303,12 +305,28 @@ nvm_do_install() { echo "=> Append the following lines to the correct file yourself:" command printf "${SOURCE_STR}" else + case "${NVM_PROFILE-}" in + ".bashrc" | ".bash_profile" | ".zshrc") + BASH_OR_ZSH=true + ;; + esac if ! command grep -qc '/nvm.sh' "$NVM_PROFILE"; then - echo "=> Appending source string to $NVM_PROFILE" - command printf "$SOURCE_STR" >> "$NVM_PROFILE" + echo "=> Appending nvm source string to $NVM_PROFILE" + command printf "${SOURCE_STR}" >> "$NVM_PROFILE" else - echo "=> Source string already in ${NVM_PROFILE}" + echo "=> nvm source string already in ${NVM_PROFILE}" fi + # shellcheck disable=SC2016 + if ${BASH_OR_ZSH} && ! command grep -qc '$NVM_DIR/bash_completion' "$NVM_PROFILE"; then + echo "=> Appending bash_completion source string to $NVM_PROFILE" + command printf "$COMPLETION_STR" >> "$NVM_PROFILE" + else + echo "=> bash_completion source string already in ${NVM_PROFILE}" + fi + fi + if ${BASH_OR_ZSH} && [ -z "${NVM_PROFILE-}" ] ; then + echo "=> Please also append the following lines to the if you are using bash/zsh shell:" + command printf "${COMPLETION_STR}" fi # Source nvm @@ -322,7 +340,10 @@ nvm_do_install() { nvm_reset echo "=> Close and reopen your terminal to start using nvm or run the following to use it now:" - command printf "$SOURCE_STR" + command printf "${SOURCE_STR}" + if ${BASH_OR_ZSH} ; then + command printf " && ${COMPLETION_STR}" + fi } #