From 24f8ae5c46d084acf88c2717e8a7cacfca52e9b4 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Thu, 3 Nov 2016 23:42:55 -0700 Subject: [PATCH] =?UTF-8?q?[New]=20support=20external=20install=20hook=20?= =?UTF-8?q?=E2=80=9C$NVM=5FINSTALL=5FTHIRD=5FPARTY=5FHOOK=E2=80=9D.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #1207. --- nvm.sh | 70 +++++++++++++++++++---------- test/installation_node/install hook | 40 +++++++++++++++++ 2 files changed, 86 insertions(+), 24 deletions(-) create mode 100755 test/installation_node/install hook diff --git a/nvm.sh b/nvm.sh index 3cb60ad..17d926e 100644 --- a/nvm.sh +++ b/nvm.sh @@ -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 diff --git a/test/installation_node/install hook b/test/installation_node/install hook new file mode 100755 index 0000000..e0f4f33 --- /dev/null +++ b/test/installation_node/install hook @@ -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}<" + +