Dynamically reinitialize sdkman

Check whether a candidate is on the path each time sdkman_init.sh is
sourced. If the candidate is not on the path prefix it, otherwise do
nothing.

This should resolve #251 and resolve #299

Since the path is dynamically checked now the SDKMAN_INIT flag and test
are no longer necessary
This commit is contained in:
Dylan Cali
2015-10-10 11:31:02 -05:00
committed by Marco Vermeulen
parent b83c551995
commit ea2df582d5
2 changed files with 24 additions and 26 deletions

View File

@@ -163,29 +163,29 @@ else
fi
fi
# initialise once only
if [[ "${SDKMAN_INIT}" != "true" ]]; then
# Build _HOME environment variables and prefix them all to PATH
# Build _HOME environment variables and prefix them all to PATH
# The candidates are assigned to an array for zsh compliance, a list of words is not iterable
# Arrays are the only way, but unfortunately zsh arrays are not backward compatible with bash
# In bash arrays are zero index based, in zsh they are 1 based(!)
for (( i=0; i <= ${#SDKMAN_CANDIDATES[*]}; i++ )); do
# Eliminate empty entries due to incompatibility
CANDIDATE_NAME="${SDKMAN_CANDIDATES[${i}]}"
CANDIDATE_DIR="${SDKMAN_DIR}/${CANDIDATE_NAME}/current"
if [[ -n "${CANDIDATE_NAME}" && -h "${CANDIDATE_DIR}" ]]; then
CANDIDATE_HOME_VAR="$(echo ${CANDIDATE_NAME} | tr '[:lower:]' '[:upper:]')_HOME"
export $(echo "${CANDIDATE_HOME_VAR}")="$CANDIDATE_DIR"
PATH="${CANDIDATE_DIR}/bin:${PATH}"
unset CANDIDATE_HOME_VAR
fi
unset CANDIDATE_NAME
unset CANDIDATE_DIR
done
unset i
export PATH
# The candidates are assigned to an array for zsh compliance, a list of words is not iterable
# Arrays are the only way, but unfortunately zsh arrays are not backward compatible with bash
# In bash arrays are zero index based, in zsh they are 1 based(!)
for (( i=0; i <= ${#SDKMAN_CANDIDATES[*]}; i++ )); do
# Eliminate empty entries due to incompatibility
CANDIDATE_NAME="${SDKMAN_CANDIDATES[${i}]}"
CANDIDATE_DIR="${SDKMAN_DIR}/${CANDIDATE_NAME}/current"
if [[ -n "${CANDIDATE_NAME}" && -h "${CANDIDATE_DIR}" ]]; then
CANDIDATE_HOME_VAR="$(echo ${CANDIDATE_NAME} | tr '[:lower:]' '[:upper:]')_HOME"
export $(echo "${CANDIDATE_HOME_VAR}")="$CANDIDATE_DIR"
export SDKMAN_INIT="true"
alias gvm="sdk"
fi
# Prefix candidate bin to PATH if it doesn't exist already
CANDIDATE_BIN="${CANDIDATE_DIR}/bin"
echo "$PATH" | grep -q "${CANDIDATE_BIN}" || PATH="${CANDIDATE_BIN}:${PATH}"
unset CANDIDATE_HOME_VAR
fi
unset CANDIDATE_NAME
unset CANDIDATE_DIR
done
unset i
export PATH
alias gvm="sdk"

View File

@@ -7,8 +7,6 @@ Feature: Path Initialisation
Scenario: sdkman is initialised for the first time
Given the candidate "grails" version "2.1.0" is already installed and default
And the system is bootstrapped
When I enter "echo $SDKMAN_INIT"
Then I see "true"
When I enter "echo $PATH"
Then I see a single occurrence of "grails"