[New] support external install hook “$NVM_INSTALL_THIRD_PARTY_HOOK”.

Fixes #1207.
Jordan Harband 2016-11-03 23:42:55 -07:00
parent cadbbced20
commit 24f8ae5c46
No known key found for this signature in database
GPG Key ID: 64A196AEE0916D55
2 changed files with 86 additions and 24 deletions

70
nvm.sh
View File

@ -1567,8 +1567,6 @@ nvm_install_binary() {
local VERSION
VERSION="$(nvm_strip_iojs_prefix "${PREFIXED_VERSION}")"
local VERSION_PATH
if [ -z "$(nvm_get_os)" ]; then
return 2
fi
@ -2358,33 +2356,57 @@ nvm() {
return $?
fi
if [ "_$NVM_OS" = "_freebsd" ]; then
# node.js and io.js do not have a FreeBSD binary
nobinary=1
nvm_err "Currently, there is no binary for $NVM_OS"
elif [ "_$NVM_OS" = "_sunos" ]; then
# Not all node/io.js versions have a Solaris binary
if ! nvm_has_solaris_binary "$VERSION"; then
nobinary=1
nvm_err "Currently, there is no binary of version $VERSION for $NVM_OS"
fi
fi
local NVM_INSTALL_SUCCESS
# skip binary install if "nobinary" option specified.
if [ $nobinary -ne 1 ] && nvm_binary_available "$VERSION"; then
if nvm_install_binary "${FLAVOR}" std "${VERSION}"; then
NVM_INSTALL_SUCCESS=true
if [ -n "${NVM_INSTALL_THIRD_PARTY_HOOK-}" ]; then
nvm_err '** $NVM_INSTALL_THIRD_PARTY_HOOK env var set; dispatching to third-party installation method **'
local NVM_METHOD_PREFERENCE
NVM_METHOD_PREFERENCE='binary'
if [ $nobinary -eq 1 ]; then
NVM_METHOD_PREFERENCE='source'
fi
fi
if [ "$NVM_INSTALL_SUCCESS" != true ]; then
if [ -z "${NVM_MAKE_JOBS-}" ]; then
nvm_get_make_jobs
local VERSION_PATH
VERSION_PATH="$(nvm_version_path "${VERSION}")"
local EXIT_CODE
"${NVM_INSTALL_THIRD_PARTY_HOOK}" "${VERSION}" "${FLAVOR}" std "${NVM_METHOD_PREFERENCE}" "${VERSION_PATH}" || {
EXIT_CODE=$?
nvm_err '*** Third-party $NVM_INSTALL_THIRD_PARTY_HOOK env var failed to install! ***'
return $EXIT_CODE
}
if ! nvm_is_version_installed "${VERSION}"; then
nvm_err '*** Third-party $NVM_INSTALL_THIRD_PARTY_HOOK env var claimed to succeed, but failed to install! ***'
return 33
fi
NVM_INSTALL_SUCCESS=true
else
if [ "_$NVM_OS" = "_freebsd" ]; then
# node.js and io.js do not have a FreeBSD binary
nobinary=1
nvm_err "Currently, there is no binary for $NVM_OS"
elif [ "_$NVM_OS" = "_sunos" ]; then
# Not all node/io.js versions have a Solaris binary
if ! nvm_has_solaris_binary "$VERSION"; then
nobinary=1
nvm_err "Currently, there is no binary of version $VERSION for $NVM_OS"
fi
fi
if nvm_install_source "${FLAVOR}" std "${VERSION}" "${NVM_MAKE_JOBS}" "${ADDITIONAL_PARAMETERS}"; then
NVM_INSTALL_SUCCESS=true
# skip binary install if "nobinary" option specified.
if [ $nobinary -ne 1 ] && nvm_binary_available "$VERSION"; then
if nvm_install_binary "${FLAVOR}" std "${VERSION}"; then
NVM_INSTALL_SUCCESS=true
fi
fi
if [ "$NVM_INSTALL_SUCCESS" != true ]; then
if [ -z "${NVM_MAKE_JOBS-}" ]; then
nvm_get_make_jobs
fi
if nvm_install_source "${FLAVOR}" std "${VERSION}" "${NVM_MAKE_JOBS}" "${ADDITIONAL_PARAMETERS}"; then
NVM_INSTALL_SUCCESS=true
fi
fi
fi
if [ "$NVM_INSTALL_SUCCESS" = true ] && nvm use "$VERSION"; then

View File

@ -0,0 +1,40 @@
#!/bin/sh
die () { echo $@ ; exit 1; }
. ../../nvm.sh
VERSION='v0.11.0'
VERSION_PATH="$(nvm_version_path "${VERSION}")"
succeed() {
nvm_echo "$@"
NVM_INSTALL_THIRD_PARTY_HOOK= nvm install "${VERSION}"
}
fail() {
succeed "$@"
return 11
}
! nvm_is_version_installed "${VERSION}" || nvm uninstall "${VERSION}" || die 'uninstall failed'
OUTPUT="$(NVM_INSTALL_THIRD_PARTY_HOOK=succeed nvm install "${VERSION}")"
USE_OUTPUT="$(nvm use "${VERSION}")"
EXPECTED_OUTPUT="${VERSION} node std binary ${VERSION_PATH}
${USE_OUTPUT}
${USE_OUTPUT}" # double use output is from the normal install in succeed()
[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<; got >${OUTPUT}<"
! nvm_is_version_installed "${VERSION}" || nvm uninstall "${VERSION}" || die 'uninstall 2 failed'
OUTPUT="$(NVM_INSTALL_THIRD_PARTY_HOOK=fail nvm install "${VERSION}" || echo 'failed')"
USE_OUTPUT="$(nvm use "${VERSION}")"
EXPECTED_OUTPUT="${VERSION} node std binary ${VERSION_PATH}
${USE_OUTPUT}
failed"
[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<; got >${OUTPUT}<"