diff --git a/src/main/bash/sdkman-use.sh b/src/main/bash/sdkman-use.sh index 3bfe1445d..afdeef5dc 100644 --- a/src/main/bash/sdkman-use.sh +++ b/src/main/bash/sdkman-use.sh @@ -24,20 +24,59 @@ function __sdk_use() { __sdkman_check_version_present "$version" || return 1 __sdkman_check_candidate_present "$candidate" || return 1 - if [[ ! -d "${SDKMAN_CANDIDATES_DIR}/${candidate}/${version}" ]]; then - echo "" - __sdkman_echo_red "Stop! Candidate version is not installed." - echo "" - __sdkman_echo_yellow "Tip: Run the following to install this version" + major_version=$(echo $version | cut -d. -f1) + if [[ "$major_version" == "$version" ]]; then + count=0 + + for dir in "${SDKMAN_CANDIDATES_DIR}/${candidate}"/*; do + if [ -d "$dir" ] && [[ "$(basename "$dir")" == "${major_version}"* ]]; + then + ((count++)) + fi + done + if [[ ${count} -eq 0 ]] + then + echo "" + __sdkman_echo_red "Stop! No matching version found." + echo "" + elif [[ ${count} -eq 1 ]] + then + version=$(basename $(ls -d "${SDKMAN_CANDIDATES_DIR}/${candidate}/${major_version}"*)) + __sdkman_change_candidate_in_path "$candidate" + __sdkman_echo_green "Using ${candidate} version ${version} in this shell." + else + echo "" + __sdkman_echo_red "Stop! Cannot decide which version to use." + echo "" + __sdkman_echo_yellow + fi + else + if [[ ! -d "${SDKMAN_CANDIDATES_DIR}/${candidate}/${version}" ]]; then + echo "" + __sdkman_echo_red "Stop! Candidate version is not installed." + echo "" + __sdkman_echo_yellow "Tip: Run the following to install this version" + echo "" + __sdkman_echo_yellow "$ sdk install ${candidate} ${version}" + return 1 + fi + + # Just update the *_HOME and PATH for this shell. + __sdkman_set_candidate_home "$candidate" "$version" + + __sdkman_change_candidate_in_path "$candidate" + if [[ ! (-L "${SDKMAN_CANDIDATES_DIR}/${candidate}/current" || -d "${SDKMAN_CANDIDATES_DIR}/${candidate}/current") ]]; then + __sdkman_echo_green "Setting ${candidate} version ${version} as default." + __sdkman_link_candidate_version "$candidate" "$version" + fi + echo "" - __sdkman_echo_yellow "$ sdk install ${candidate} ${version}" - return 1 + __sdkman_echo_green "Using ${candidate} version ${version} in this shell." fi +} - # Just update the *_HOME and PATH for this shell. - __sdkman_set_candidate_home "$candidate" "$version" - - if [[ $PATH =~ ${SDKMAN_CANDIDATES_DIR}/${candidate}/([^/]+) ]]; then +function __sdkman_change_candidate_in_path() { + if [[ $PATH =~ ${SDKMAN_CANDIDATES_DIR}/${1}/([^/]+) ]]; then local matched_version if [[ "$zsh_shell" == "true" ]]; then @@ -48,12 +87,4 @@ function __sdk_use() { export PATH=${PATH//${SDKMAN_CANDIDATES_DIR}\/${candidate}\/${matched_version}/${SDKMAN_CANDIDATES_DIR}\/${candidate}\/${version}} fi - - if [[ ! (-L "${SDKMAN_CANDIDATES_DIR}/${candidate}/current" || -d "${SDKMAN_CANDIDATES_DIR}/${candidate}/current") ]]; then - __sdkman_echo_green "Setting ${candidate} version ${version} as default." - __sdkman_link_candidate_version "$candidate" "$version" - fi - - echo "" - __sdkman_echo_green "Using ${candidate} version ${version} in this shell." } diff --git a/src/test/resources/features/use_major_version.feature b/src/test/resources/features/use_major_version.feature new file mode 100644 index 000000000..b2c57d722 --- /dev/null +++ b/src/test/resources/features/use_major_version.feature @@ -0,0 +1,28 @@ +Feature: Use Version Giving Only The Major Version Number + + Background: + Given the internet is reachable + And an initialised environment + + Scenario: Use a grails version that is installed giving only major version + Given the candidate "grails" version "1.3.0" is already installed and default + And the candidate "grails" version "2.1.0" is already installed but not default + And the system is bootstrapped + When I enter "sdk use grails 2" + Then I see "Using grails version 2.1.0 in this shell." + Then the candidate "grails" version "2.1.0" should be in use + + Scenario: Fail to use a major version because there are multiple matching versions + Given the candidate "grails" version "1.3.0" is already installed and default + And the candidate "grails" version "2.1.0" is already installed but not default + And the candidate "grails" version "2.3.0" is already installed but not default + And the system is bootstrapped + When I enter "sdk use grails 2" + Then I see "Stop! Cannot decide which version to use." + + Scenario: Fail to use a major version because there is no matching version + Given the candidate "grails" version "1.3.0" is already installed and default + And the system is bootstrapped + When I enter "sdk use grails 2" + Then I see "Stop! No matching version found." + \ No newline at end of file