Merge pull request #567 from xcambar/nvm_source

[install script] Refactors NVM_SOURCE
master
Jordan Harband 2014-11-15 10:23:40 -06:00
commit ce82109229
2 changed files with 69 additions and 10 deletions

View File

@ -11,6 +11,33 @@ if [ -z "$NVM_DIR" ]; then
NVM_DIR="$HOME/.nvm" NVM_DIR="$HOME/.nvm"
fi fi
#
# Outputs the location to NVM depending on:
# * The availability of $NVM_SOURCE
# * The method used ("script" or "git" in the script, defaults to "git")
# NVM_SOURCE always takes precedence
#
nvm_source() {
local NVM_METHOD
NVM_METHOD="$1"
if [ -z "$NVM_SOURCE" ]; then
local NVM_SOURCE
else
echo "$NVM_SOURCE"
return 0
fi
if [ "_$NVM_METHOD" = "_script" ]; then
NVM_SOURCE="https://raw.githubusercontent.com/creationix/nvm/v0.18.0/nvm.sh"
elif [ "_$NVM_METHOD" = "_git" ] || [ -z "$NVM_METHOD" ]; then
NVM_SOURCE="https://github.com/creationix/nvm.git"
else
echo >&2 "Unexpected value \"$NVM_METHOD\" for \$NVM_METHOD"
return 1
fi
echo "$NVM_SOURCE"
return 0
}
nvm_download() { nvm_download() {
if nvm_has "curl"; then if nvm_has "curl"; then
curl $* curl $*
@ -27,10 +54,6 @@ nvm_download() {
} }
install_nvm_from_git() { install_nvm_from_git() {
if [ -z "$NVM_SOURCE" ]; then
NVM_SOURCE="https://github.com/creationix/nvm.git"
fi
if [ -d "$NVM_DIR/.git" ]; then if [ -d "$NVM_DIR/.git" ]; then
echo "=> nvm is already installed in $NVM_DIR, trying to update" echo "=> nvm is already installed in $NVM_DIR, trying to update"
printf "\r=> " printf "\r=> "
@ -42,16 +65,15 @@ install_nvm_from_git() {
echo "=> Downloading nvm from git to '$NVM_DIR'" echo "=> Downloading nvm from git to '$NVM_DIR'"
printf "\r=> " printf "\r=> "
mkdir -p "$NVM_DIR" mkdir -p "$NVM_DIR"
git clone "$NVM_SOURCE" "$NVM_DIR" git clone "$(nvm_source "git")" "$NVM_DIR"
fi fi
cd "$NVM_DIR" && git checkout v0.18.0 && git branch -D master >/dev/null 2>&1 cd "$NVM_DIR" && git checkout v0.18.0 && git branch -D master >/dev/null 2>&1
return return
} }
install_nvm_as_script() { install_nvm_as_script() {
if [ -z "$NVM_SOURCE" ]; then local NVM_SOURCE
NVM_SOURCE="https://raw.githubusercontent.com/creationix/nvm/v0.18.0/nvm.sh" NVM_SOURCE=$(nvm_source "script")
fi
# Downloading to $NVM_DIR # Downloading to $NVM_DIR
mkdir -p "$NVM_DIR" mkdir -p "$NVM_DIR"
@ -60,8 +82,8 @@ install_nvm_as_script() {
else else
echo "=> Downloading nvm as script to '$NVM_DIR'" echo "=> Downloading nvm as script to '$NVM_DIR'"
fi fi
nvm_download -s "$NVM_SOURCE" -o "$NVM_DIR/nvm.sh" || { nvm_download -s "$_source" -o "$NVM_DIR/nvm.sh" || {
echo >&2 "Failed to download '$NVM_SOURCE'.." echo >&2 "Failed to download '$_source'.."
return 1 return 1
} }
} }

View File

@ -0,0 +1,37 @@
#!/bin/sh
cleanup () {
unset -f die cleanup
unset NVM_SOURCE out
}
die () { echo $@ ; cleanup ; exit 1; }
NVM_ENV=testing . ../../install.sh
# nvm_source with no parameter returns the git endpoint
echo $(nvm_source) | grep "nvm.git$" > /dev/null || die "nvm_source without arguments should return the location of the git repo"
# nvm_source with git parameter returns the location of the nvm repo
echo $(nvm_source "git") | grep "nvm.git$" > /dev/null || die "nvm_source without arguments should return the location of the git repo"
# nvm_source with script parameter returns the location of nvm.sh
echo $(nvm_source "script") | grep "nvm.sh$" > /dev/null || die "nvm_source \"script\" should return the location of nvm.sh"
# nvm_source with any other parameter errors out and exits
nvm_source "anything" 2> /dev/null && die "nvm_source with invalid parameter should exit"
out=$(nvm_source "anything" 2>&1 >/dev/null) || : #Saving the process here
[ -z "$out" ] && die "nvm_source with invalid parameter should error out"
#nvm_source should always return NVM_SOURCE no matter the parameters
NVM_SOURCE="my_location"
out=$(nvm_source)
[ "_$out" = "_my_location" ] || die "nvm_source without arguments should have returned \$NVM_SOURCE. Got \"$out\""
out=$(nvm_source "git")
[ "_$out" = "_my_location" ] || die "nvm_source git should have returned \$NVM_SOURCE. Got \"$out\""
out=$(nvm_source "script")
[ "_$out" = "_my_location" ] || die "nvm_source script should have returned \$NVM_SOURCE. Got \"$out\""
out=$(nvm_source "anything")
[ "_$out" = "_my_location" ] || die "nvm_source script should have returned \$NVM_SOURCE. Got \"$out\""
cleanup