Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/use major versions #1291

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 50 additions & 19 deletions src/main/bash/sdkman-use.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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."
}
28 changes: 28 additions & 0 deletions src/test/resources/features/use_major_version.feature
Original file line number Diff line number Diff line change
@@ -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."

Loading