Merge pull request #526 from creationix/add_nvm_num_version_groups

Replace `expr` with `nvm_num_version_groups`
master
Jordan Harband 2014-09-16 10:13:57 -07:00
commit edf36a8ea7
7 changed files with 57 additions and 22 deletions

24
nvm.sh
View File

@ -181,6 +181,20 @@ nvm_format_version() {
echo "$1" | sed -e 's/^\([0-9]\)/v\1/g' echo "$1" | sed -e 's/^\([0-9]\)/v\1/g'
} }
nvm_num_version_groups() {
local VERSION
VERSION="$1"
if [ -z "$VERSION" ]; then
echo "0"
return
fi
local NVM_NUM_DOTS
NVM_NUM_DOTS=$(echo "$VERSION" | sed -e 's/^v//' | sed -e 's/\.$//' | sed -e 's/[^\.]//g')
local NVM_NUM_GROUPS
NVM_NUM_GROUPS=".$NVM_NUM_DOTS"
echo "${#NVM_NUM_GROUPS}"
}
nvm_strip_path() { nvm_strip_path() {
echo "$1" | sed -e "s#$NVM_DIR/[^/]*$2[^:]*:##g" -e "s#:$NVM_DIR/[^/]*$2[^:]*##g" -e "s#$NVM_DIR/[^/]*$2[^:]*##g" echo "$1" | sed -e "s#$NVM_DIR/[^/]*$2[^:]*:##g" -e "s#:$NVM_DIR/[^/]*$2[^:]*##g" -e "s#$NVM_DIR/[^/]*$2[^:]*##g"
} }
@ -233,14 +247,18 @@ nvm_ls() {
return return
fi fi
# If it looks like an explicit version, don't do anything funny # If it looks like an explicit version, don't do anything funny
if [ `expr "$PATTERN" : "v[0-9]*\.[0-9]*\.[0-9]*$"` != 0 ]; then if [ "~$(echo "$PATTERN" | cut -c1-1)" = "~v" ] && [ "~$(nvm_num_version_groups "$PATTERN")" = "~3" ]; then
if [ -d "$(nvm_version_path "$PATTERN")" ]; then if [ -d "$(nvm_version_path "$PATTERN")" ]; then
VERSIONS="$PATTERN" VERSIONS="$PATTERN"
fi fi
else else
PATTERN=$(nvm_format_version $PATTERN) PATTERN=$(nvm_format_version $PATTERN)
if [ `expr "$PATTERN" : "v[0-9]*\.[0-9]*$"` != 0 ]; then if [ "~$PATTERN" != "~system" ]; then
PATTERN="$PATTERN." local NUM_VERSION_GROUPS
NUM_VERSION_GROUPS="$(nvm_num_version_groups "$PATTERN")"
if [ "~$NUM_VERSION_GROUPS" = "~2" ] || [ "~$NUM_VERSION_GROUPS" = "~1" ]; then
PATTERN="$(echo "$PATTERN" | sed -e 's/\.*$//g')."
fi
fi fi
if [ -d "$(nvm_version_dir new)" ]; then if [ -d "$(nvm_version_dir new)" ]; then
VERSIONS=`find "$(nvm_version_dir new)/" "$(nvm_version_dir old)/" -maxdepth 1 -type d -name "$PATTERN*" -exec basename '{}' ';' \ VERSIONS=`find "$(nvm_version_dir new)/" "$(nvm_version_dir old)/" -maxdepth 1 -type d -name "$PATTERN*" -exec basename '{}' ';' \

View File

@ -1,7 +1,6 @@
#!/bin/sh #!/bin/sh
cleanup () { unset -f nvm_has_system_node; } die () { echo $@ ; exit 1; }
die () { cleanup ; echo $@ ; exit 1; }
. ../../../nvm.sh . ../../../nvm.sh

View File

@ -2,13 +2,10 @@
mkdir ../../../v0.1.3 mkdir ../../../v0.1.3
mkdir ../../../v0.2.3 mkdir ../../../v0.2.3
mkdir ../../../versions
. ../../../nvm.sh . ../../../nvm.sh
mkdir ../../../versions
[ -z "$(nvm ls | \grep 'versions')" ] [ -z "$(nvm ls | \grep 'versions')" ]
# The result should contain only the appropriate version numbers. # The result should contain only the appropriate version numbers.
rmdir ../../../versions

View File

@ -6,6 +6,6 @@ mkdir ../../../v0.1.2
nvm ls v0.1 | grep v0.1.2 && nvm ls v0.1 | grep v0.1.2 &&
nvm ls v0.1.2 | grep v0.1.2 && nvm ls v0.1.2 | grep v0.1.2 &&
nvm ls v0.1. | grep N/A && nvm ls v0.1. | grep v0.1.2 &&
nvm ls v0.1.1 | grep N/A nvm ls v0.1.1 | grep N/A

View File

@ -1,12 +1,12 @@
rmdir ../../../v0.0.1 rmdir ../../../v0.0.1 >/dev/null 2>&1
rmdir ../../../v0.0.3 rmdir ../../../v0.0.3 >/dev/null 2>&1
rmdir ../../../v0.0.9 rmdir ../../../v0.0.9 >/dev/null 2>&1
rmdir ../../../v0.1.2 rmdir ../../../v0.1.2 >/dev/null 2>&1
rmdir ../../../v0.1.3 rmdir ../../../v0.1.3 >/dev/null 2>&1
rmdir ../../../v0.2.3 rmdir ../../../v0.2.3 >/dev/null 2>&1
rmdir ../../../v0.3.1 rmdir ../../../v0.3.1 >/dev/null 2>&1
rmdir ../../../v0.3.3 rmdir ../../../v0.3.3 >/dev/null 2>&1
rmdir ../../../v0.3.9 rmdir ../../../v0.3.9 >/dev/null 2>&1
rmdir ../../../versions rmdir ../../../versions >/dev/null 2>&1
unalias nvm_has_system_node unalias nvm_has_system_node >/dev/null 2>&1

View File

@ -6,7 +6,7 @@ die () { echo $@ ; exit 1; }
return_zero () { return 0; } return_zero () { return 0; }
[ "$( (nvm deactivate > /dev/null 2>&1) && nvm_ls_current)" = "system" ] || die 'when deactivated, did not return "system"' [ "$(nvm deactivate > /dev/null 2>&1 ; nvm_ls_current)" = "system" ] || die 'when deactivated, did not return "system"'
TEST_PWD=$(pwd) TEST_PWD=$(pwd)
TEST_DIR="$TEST_PWD/nvm_ls_current_tmp" TEST_DIR="$TEST_PWD/nvm_ls_current_tmp"

View File

@ -0,0 +1,21 @@
#!/bin/sh
die () { echo $@ ; exit 1; }
. ../../../nvm.sh
[ "~$(nvm_num_version_groups)" = "~0" ] || die "no args should give 0"
[ "~$(nvm_num_version_groups a)" = "~1" ] || die "one letter should give 1"
[ "~$(nvm_num_version_groups 1)" = "~1" ] || die "1 should give 1"
[ "~$(nvm_num_version_groups v1)" = "~1" ] || die "v1 should give 1"
[ "~$(nvm_num_version_groups v1.)" = "~1" ] || die "v1. should give 1"
[ "~$(nvm_num_version_groups 1.2)" = "~2" ] || die "1.2 should give 2"
[ "~$(nvm_num_version_groups v1.2)" = "~2" ] || die "v1.2 should give 2"
[ "~$(nvm_num_version_groups v1.2.)" = "~2" ] || die "v1.2. should give 2"
[ "~$(nvm_num_version_groups 1.2.3)" = "~3" ] || die "1.2.3 should give 3"
[ "~$(nvm_num_version_groups v1.2.3)" = "~3" ] || die "v1.2.3 should give 3"
[ "~$(nvm_num_version_groups v1.2.3.)" = "~3" ] || die "v1.2.3. should give 3"