From 31457fec3fab50729adc4a7a4aadd80a61a5007b Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sun, 27 Dec 2015 12:52:01 -0800 Subject: [PATCH] [Refactor] `nvm install`: make `-j` and `-s` order-independent. --- nvm.sh | 48 +++++++++++-------- .../install from source with thread parameter | 6 +-- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/nvm.sh b/nvm.sh index 51f9a64..bbac633 100755 --- a/nvm.sh +++ b/nvm.sh @@ -1554,10 +1554,8 @@ nvm() { ;; "install" | "i" ) - local nobinary local version_not_provided version_not_provided=0 - local provided_version local NVM_OS NVM_OS="$(nvm_get_os)" @@ -1577,27 +1575,35 @@ nvm() { shift + local nobinary nobinary=0 - if [ "_$1" = "_-s" ]; then - nobinary=1 - shift - if [ "_$1" = "_-j" ]; then - shift - local NVM_CPU_THREAD_VALID - NVM_CPU_THREAD_VALID=$(nvm_is_natural_num $1) - if [ "$NVM_CPU_THREAD_VALID" = "true" ]; then - NVM_MAKE_JOBS=$1 - echo "Set number of jobs to $MAKE_JOBS for 'make' utility" - else - unset NVM_MAKE_JOBS - echo >&2 "$1 is invalid for CPU threads, should be a natural number" - fi - shift - else - unset NVM_MAKE_JOBS - fi - fi + while [ $# -ne 0 ] + do + case "$1" in + -s) + shift # consume "-s" + nobinary=1 + ;; + -j) + shift # consume "-j" + local NVM_CPU_THREAD_VALID + NVM_CPU_THREAD_VALID=$(nvm_is_natural_num $1) + if [ "$NVM_CPU_THREAD_VALID" = "true" ]; then + NVM_MAKE_JOBS=$1 + echo "number of \`make\` jobs: $NVM_MAKE_JOBS" + else + unset NVM_MAKE_JOBS + echo >&2 "$1 is invalid for number of \`make\` jobs, must be a natural number" + fi + shift # consume job count + ;; + *) + break # stop parsing args + ;; + esac + done + local provided_version provided_version="$1" if [ -z "$provided_version" ]; then diff --git a/test/installation/node/install from source with thread parameter b/test/installation/node/install from source with thread parameter index 473107e..84af8ad 100755 --- a/test/installation/node/install from source with thread parameter +++ b/test/installation/node/install from source with thread parameter @@ -11,7 +11,7 @@ NVM_TEST_VERSION=v0.10.41 # Remove the stuff we're clobbering. [ -e ../../../$NVM_TEST_VERSION ] && rm -R ../../../$NVM_TEST_VERSION -# Install from source with 1 CPU thread parameter +# Install from source with 1 make job nvm install -s -j 1 $NVM_TEST_VERSION || die "'nvm install -s $NVM_TEST_VERSION' failed" # Check @@ -25,8 +25,8 @@ nvm run $NVM_TEST_VERSION --version | grep $NVM_TEST_VERSION || "'nvm run $NVM_T # Remove the stuff we're clobbering. [ -e ../../../$NVM_TEST_VERSION ] && rm -R ../../../$NVM_TEST_VERSION -# Install from source with 2 CPU threads parameter -nvm install -s -j 2 $NVM_TEST_VERSION || die "'nvm install -s $NVM_TEST_VERSION' failed" +# Install from source with 2 make jobs (and swapped arg order) +nvm install -j 2 -s $NVM_TEST_VERSION || die "'nvm install -s $NVM_TEST_VERSION' failed" # Check [ -d ../../../$NVM_TEST_VERSION ]