mirror of
				https://github.com/nvm-sh/nvm.git
				synced 2025-10-31 10:15:53 +08:00 
			
		
		
		
	[Refactor] Create nvm_get_make_jobs to abstract out “-j” logic.
				
					
				
			This commit is contained in:
		
							
								
								
									
										77
									
								
								nvm.sh
									
									
									
									
									
								
							
							
						
						
									
										77
									
								
								nvm.sh
									
									
									
									
									
								
							| @@ -1200,11 +1200,48 @@ nvm_install_node_binary() { | |||||||
|   return 2 |   return 2 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | nvm_get_make_jobs() { | ||||||
|  |   if nvm_is_natural_num "$1"; then | ||||||
|  |     NVM_MAKE_JOBS="$1" | ||||||
|  |     echo "number of \`make\` jobs: $NVM_MAKE_JOBS" | ||||||
|  |     return | ||||||
|  |   elif [ -n "$1" ]; then | ||||||
|  |     unset NVM_MAKE_JOBS | ||||||
|  |     echo >&2 "$1 is invalid for number of \`make\` jobs, must be a natural number" | ||||||
|  |   fi | ||||||
|  |   local NVM_OS | ||||||
|  |   NVM_OS="$(nvm_get_os)" | ||||||
|  |   local NVM_CPU_THREADS | ||||||
|  |   if [ "_$NVM_OS" = "_linux" ]; then | ||||||
|  |     NVM_CPU_THREADS="$(grep -c 'core id' /proc/cpuinfo)" | ||||||
|  |   elif [ "_$NVM_OS" = "_freebsd" ] || [ "_$NVM_OS" = "_darwin" ]; then | ||||||
|  |     NVM_CPU_THREADS="$(sysctl -n hw.ncpu)" | ||||||
|  |   elif [ "_$NVM_OS" = "_sunos" ]; then | ||||||
|  |     NVM_CPU_THREADS="$(psrinfo | wc -l)" | ||||||
|  |   fi | ||||||
|  |   if ! nvm_is_natural_num "$NVM_CPU_THREADS" ; then | ||||||
|  |     echo "Can not determine how many thread(s) we can use, set to only 1 now." >&2 | ||||||
|  |     echo "Please report an issue on GitHub to help us make it better and run it faster on your computer!" >&2 | ||||||
|  |     NVM_MAKE_JOBS=1 | ||||||
|  |   else | ||||||
|  |     echo "Detected that you have $NVM_CPU_THREADS CPU thread(s)" | ||||||
|  |     if [ $NVM_CPU_THREADS -gt 2 ]; then | ||||||
|  |       NVM_MAKE_JOBS=$(($NVM_CPU_THREADS - 1)) | ||||||
|  |       echo "Set the number of jobs to $NVM_CPU_THREADS - 1 = $NVM_MAKE_JOBS jobs to speed up the build" | ||||||
|  |     else | ||||||
|  |       NVM_MAKE_JOBS=1 | ||||||
|  |       echo "Number of CPU thread(s) less or equal to 2 will have only one job a time for 'make'" | ||||||
|  |     fi | ||||||
|  |   fi | ||||||
|  | } | ||||||
|  |  | ||||||
| nvm_install_node_source() { | nvm_install_node_source() { | ||||||
|   local VERSION |   local VERSION | ||||||
|   VERSION="$1" |   VERSION="$1" | ||||||
|  |   local NVM_MAKE_JOBS | ||||||
|  |   NVM_MAKE_JOBS="$2" | ||||||
|   local ADDITIONAL_PARAMETERS |   local ADDITIONAL_PARAMETERS | ||||||
|   ADDITIONAL_PARAMETERS="$2" |   ADDITIONAL_PARAMETERS="$3" | ||||||
|  |  | ||||||
|   local NVM_ARCH |   local NVM_ARCH | ||||||
|   NVM_ARCH="$(nvm_get_arch)" |   NVM_ARCH="$(nvm_get_arch)" | ||||||
| @@ -1232,29 +1269,6 @@ nvm_install_node_source() { | |||||||
|     MAKE_CXX="CXX=c++" |     MAKE_CXX="CXX=c++" | ||||||
|   fi |   fi | ||||||
|  |  | ||||||
|   if [ -z "$NVM_MAKE_JOBS" ]; then |  | ||||||
|     if [ "_$NVM_OS" = "_linux" ]; then |  | ||||||
|       NVM_CPU_THREADS="$(grep -c 'core id' /proc/cpuinfo)" |  | ||||||
|     elif [ "_$NVM_OS" = "_freebsd" ] || [ "_$NVM_OS" = "_darwin" ]; then |  | ||||||
|       NVM_CPU_THREADS="$(sysctl -n hw.ncpu)" |  | ||||||
|     elif [ "_$NVM_OS" = "_sunos" ]; then |  | ||||||
|       NVM_CPU_THREADS="$(psrinfo | wc -l)" |  | ||||||
|     fi |  | ||||||
|     if ! nvm_is_natural_num "$NVM_CPU_THREADS" ; then |  | ||||||
|       echo "Can not determine how many thread(s) we can use, set to only 1 now." 1>&2 |  | ||||||
|       echo "Please report an issue on GitHub to help us make it better and run it faster on your computer!" 1>&2 |  | ||||||
|       NVM_MAKE_JOBS="1" |  | ||||||
|     else |  | ||||||
|       echo "Detected that you have $NVM_CPU_THREADS CPU thread(s)" |  | ||||||
|       if [ $NVM_CPU_THREADS -gt 2 ]; then |  | ||||||
|         NVM_MAKE_JOBS=$(($NVM_CPU_THREADS - 1)) |  | ||||||
|         echo "Set the number of jobs to $NVM_CPU_THREADS - 1 = $NVM_MAKE_JOBS jobs to speed up the build" |  | ||||||
|       else |  | ||||||
|         NVM_MAKE_JOBS=1 |  | ||||||
|         echo "Number of CPU thread(s) less or equal to 2 will have only one job a time for 'make'" |  | ||||||
|       fi |  | ||||||
|     fi |  | ||||||
|   fi |  | ||||||
|   local tmpdir |   local tmpdir | ||||||
|   tmpdir="$NVM_DIR/src" |   tmpdir="$NVM_DIR/src" | ||||||
|   local tmptarball |   local tmptarball | ||||||
| @@ -1578,6 +1592,7 @@ nvm() { | |||||||
|  |  | ||||||
|       local nobinary |       local nobinary | ||||||
|       nobinary=0 |       nobinary=0 | ||||||
|  |       local make_jobs | ||||||
|       while [ $# -ne 0 ] |       while [ $# -ne 0 ] | ||||||
|       do |       do | ||||||
|         case "$1" in |         case "$1" in | ||||||
| @@ -1587,13 +1602,7 @@ nvm() { | |||||||
|           ;; |           ;; | ||||||
|           -j) |           -j) | ||||||
|             shift # consume "-j" |             shift # consume "-j" | ||||||
|             if nvm_is_natural_num "$1"; then |             nvm_get_make_jobs "$1" | ||||||
|               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 |             shift # consume job count | ||||||
|           ;; |           ;; | ||||||
|           *) |           *) | ||||||
| @@ -1693,15 +1702,15 @@ nvm() { | |||||||
|       fi |       fi | ||||||
|       if [ "$NVM_INSTALL_SUCCESS" != true ]; then |       if [ "$NVM_INSTALL_SUCCESS" != true ]; then | ||||||
|         if [ "$NVM_IOJS" != true ] &&  [ "$NVM_NODE_MERGED" != true ]; then |         if [ "$NVM_IOJS" != true ] &&  [ "$NVM_NODE_MERGED" != true ]; then | ||||||
|           if nvm_install_node_source "$VERSION" "$ADDITIONAL_PARAMETERS"; then |           if nvm_install_node_source "$VERSION" "$NVM_MAKE_JOBS" "$ADDITIONAL_PARAMETERS"; then | ||||||
|             NVM_INSTALL_SUCCESS=true |             NVM_INSTALL_SUCCESS=true | ||||||
|           fi |           fi | ||||||
|         elif [ "$NVM_IOJS" = true ]; then |         elif [ "$NVM_IOJS" = true ]; then | ||||||
|           # nvm_install_iojs_source "$VERSION" "$ADDITIONAL_PARAMETERS" |           # nvm_install_iojs_source "$VERSION" "$NVM_MAKE_JOBS" "$ADDITIONAL_PARAMETERS" | ||||||
|           echo "Installing iojs from source is not currently supported" >&2 |           echo "Installing iojs from source is not currently supported" >&2 | ||||||
|           return 105 |           return 105 | ||||||
|         elif [ "$NVM_NODE_MERGED" = true ]; then |         elif [ "$NVM_NODE_MERGED" = true ]; then | ||||||
|          # nvm_install_merged_node_source "$VERSION" "$ADDITIONAL_PARAMETERS" |          # nvm_install_merged_node_source "$VERSION" "$NVM_MAKE_JOBS" "$ADDITIONAL_PARAMETERS" | ||||||
|          echo "Installing node v1.0 and greater from source is not currently supported" >&2 |          echo "Installing node v1.0 and greater from source is not currently supported" >&2 | ||||||
|          return 106 |          return 106 | ||||||
|         fi |         fi | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user