Skip to content

Commit b7d4aaa

Browse files
committed
feat: add prepare_jdks.sh
1 parent 979775a commit b7d4aaa

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed

lib/prepare_jdks.sh

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
#!/bin/bash
2+
#
3+
# a lib to prepare jdks by sdkman https://sdkman.io/
4+
#
5+
#_ source guard start _#
6+
[ -z "${__source_guard_E2AA8C4F_215B_4CDA_9816_429C7A2CD465:+dummy}" ] || return 0
7+
__source_guard_E2AA8C4F_215B_4CDA_9816_429C7A2CD465="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
8+
readonly __source_guard_E2AA8C4F_215B_4CDA_9816_429C7A2CD465
9+
#_ source guard end _#
10+
11+
set -eEuo pipefail
12+
13+
# shellcheck source=common_utils.sh
14+
source "$__source_guard_E2AA8C4F_215B_4CDA_9816_429C7A2CD465/common_utils.sh"
15+
16+
################################################################################
17+
# api functions:
18+
#
19+
# - prepare_jdks::prepare_jdks
20+
# - prepare_jdks::switch_java_home_to_jdk
21+
#
22+
# - prepare_jdks::install_jdk_by_sdkman
23+
# - prepare_jdks::load_sdkman
24+
# - prepare_jdks::install_sdkman
25+
################################################################################
26+
27+
prepare_jdks::install_sdkman() {
28+
PREPARE_JDKS_IS_THIS_TIME_INSTALLED_SDKMAN="${PREPARE_JDKS_IS_THIS_TIME_INSTALLED_SDKMAN:-false}"
29+
30+
# install sdkman if not installed yet
31+
if [ ! -f "$HOME/.sdkman/bin/sdkman-init.sh" ]; then
32+
[ -d "$HOME/.sdkman" ] && rm -rf "$HOME/.sdkman"
33+
34+
curl -s get.sdkman.io | bash || cu::die "fail to install sdkman"
35+
PREPARE_JDKS_IS_THIS_TIME_INSTALLED_SDKMAN=true
36+
37+
# FIXME: hard-coded config logic
38+
{
39+
echo sdkman_auto_answer=true
40+
echo sdkman_auto_selfupdate=false
41+
echo sdkman_disable_auto_upgrade_check=true
42+
} >>"$HOME/.sdkman/etc/config"
43+
fi
44+
}
45+
46+
# load sdkman.
47+
# install sdkman if not installed yet.
48+
prepare_jdks::load_sdkman() {
49+
prepare_jdks::install_sdkman
50+
51+
# load sdkman if not loaded yet
52+
if ! command -v sdk &>/dev/null; then
53+
# shellcheck disable=SC1090
54+
cu::loose_run source "$HOME/.sdkman/bin/sdkman-init.sh"
55+
fi
56+
}
57+
58+
prepare_jdks::ls_java() {
59+
prepare_jdks::load_sdkman
60+
61+
cu::loose_run cu::log_then_run sdk ls java | sed -n '/^ Vendor/,/^===========/p'
62+
}
63+
64+
prepare_jdks::_get_jdk_path_from_jdk_name_of_sdkman() {
65+
local jdk_name_of_sdkman="$1"
66+
echo "$SDKMAN_CANDIDATES_DIR/java/$jdk_name_of_sdkman"
67+
}
68+
69+
# install the jdk by sdkman if not installed yet.
70+
prepare_jdks::install_jdk_by_sdkman() {
71+
prepare_jdks::load_sdkman
72+
73+
local jdk_name_of_sdkman="$1"
74+
local jdk_home_path
75+
jdk_home_path="$(
76+
prepare_jdks::_get_jdk_path_from_jdk_name_of_sdkman "$jdk_name_of_sdkman"
77+
)"
78+
79+
# install jdk by sdkman
80+
if [ ! -d "$jdk_home_path" ]; then
81+
cu::loose_run cu::log_then_run sdk install java "$jdk_name_of_sdkman" ||
82+
cu::die "fail to install jdk $jdk_name_of_sdkman by sdkman"
83+
fi
84+
}
85+
86+
prepare_jdks::_prepare_one_jdk() {
87+
local jdk_name_of_sdkman="$1"
88+
local jdk_major_version="${jdk_name_of_sdkman//.*/}"
89+
90+
# java_home_var_name like JDK7_HOME, JDK11_HOME
91+
local java_home_var_name="JDK${jdk_major_version}_HOME"
92+
93+
if [ -z "${!java_home_var_name:-}" ]; then
94+
# 1. prepare jdk home global var
95+
#
96+
# Dynamic variable names in Bash
97+
# https://stackoverflow.com/a/55331060/922688
98+
#
99+
# read value of dynamic variable by:
100+
# "${!var_which_value_is_var_name}"
101+
# assign value to of dynamic variable by:
102+
# printf -v "$var_which_value_is_var_name" %s value
103+
local jdk_home_path
104+
jdk_home_path="$(prepare_jdks::_get_jdk_path_from_jdk_name_of_sdkman "$jdk_name_of_sdkman")"
105+
106+
printf -v "$java_home_var_name" %s "${jdk_home_path}"
107+
108+
JDK_HOME_VAR_NAMES=(
109+
${JDK_HOME_VAR_NAMES[@]:+"${JDK_HOME_VAR_NAMES[@]}"}
110+
"$java_home_var_name"
111+
)
112+
113+
# 2. install jdk by sdkman
114+
prepare_jdks::install_jdk_by_sdkman "$jdk_name_of_sdkman"
115+
else
116+
cu::yellow_echo "$java_home_var_name is already prepared: ${!java_home_var_name}"
117+
cu::yellow_echo " so skip install $jdk_name_of_sdkman by sdkman"
118+
fi
119+
}
120+
121+
# prepare jdks:
122+
#
123+
# usage:
124+
# prepare_jdks::prepare_jdks <jdk_name_of_sdkman>...
125+
#
126+
# example:
127+
# prepare_jdks::prepare_jdks 11.0.14-ms
128+
# prepare_jdks::prepare_jdks 11.0.14-ms 17.0.2-zulu
129+
#
130+
# do the below works:
131+
# 1. prepare jdk home global var, like
132+
# JDK7_HOME=/path/to/jdk7/home
133+
# JDK11_HOME=/path/to/jdk11/home
134+
# 2. prepare jdk home global array var `JDK_HOME_VAR_NAMES`, like
135+
# JDK_HOME_VAR_NAMES=($JDK7_HOME $JDK11_HOME)
136+
# 3. install jdk by sdkman
137+
prepare_jdks::prepare_jdks() {
138+
JDK_HOME_VAR_NAMES=()
139+
140+
local jdk_name_of_sdkman
141+
for jdk_name_of_sdkman in "$@"; do
142+
prepare_jdks::_prepare_one_jdk "$jdk_name_of_sdkman"
143+
done
144+
145+
cu::blue_echo "prepared jdks:"
146+
local java_home_var_name
147+
for java_home_var_name in "${JDK_HOME_VAR_NAMES[@]}"; do
148+
cu::blue_echo "$java_home_var_name: ${!java_home_var_name}"
149+
done
150+
}
151+
152+
# usage:
153+
# prepare_jdks::switch_java_home_to_jdk 11
154+
# prepare_jdks::switch_java_home_to_jdk /path/to/java_home
155+
prepare_jdks::switch_java_home_to_jdk() {
156+
[ $# == 1 ] || cu::die "${FUNCNAME[0]} need 1 argument! But provided: $*"
157+
158+
local -r switch_target="$1"
159+
[ -n "$switch_target" ] || cu::die "jdk $switch_target is blank"
160+
161+
if cu::is_number_string "$switch_target"; then
162+
# set by java version, e.g.
163+
# prepare_jdks::switch_java_home_to_jdk 11
164+
local java_home_var_name="JDK${switch_target}_HOME"
165+
export JAVA_HOME="${!java_home_var_name:-}"
166+
167+
[ -n "$JAVA_HOME" ] || cu::die "JAVA_HOME of java version $switch_target($java_home_var_name) is unset or blank: $JAVA_HOME"
168+
else
169+
# set by java home path
170+
export JAVA_HOME="$switch_target"
171+
fi
172+
173+
[ -e "$JAVA_HOME" ] || cu::die "jdk $switch_target NOT existed: $JAVA_HOME"
174+
[ -d "$JAVA_HOME" ] || cu::die "jdk $switch_target is NOT directory: $JAVA_HOME"
175+
176+
local java_cmd="$JAVA_HOME/bin/java"
177+
[ -f "$java_cmd" ] || cu::die "\$JAVA_HOME/bin/java ($java_cmd) is NOT found!"
178+
[ -x "$java_cmd" ] || cu::die "\$JAVA_HOME/bin/java ($java_cmd) is NOT executable!"
179+
}
180+
181+
################################################################################
182+
# auto load sdkman.
183+
# disable by define `PREPARE_JDKS_NO_AUTO_LOAD_SDKMAN` var
184+
#
185+
# auto prepare jdks,
186+
# if `PREPARE_JDKS_INSTALL_BY_SDKMAN` is has values
187+
################################################################################
188+
189+
if [ -z "${PREPARE_JDKS_NO_AUTO_LOAD_SDKMAN+defined}" ]; then
190+
prepare_jdks::load_sdkman
191+
fi
192+
193+
if "$PREPARE_JDKS_IS_THIS_TIME_INSTALLED_SDKMAN"; then
194+
prepare_jdks::ls_java
195+
fi
196+
197+
if [ -n "${PREPARE_JDKS_INSTALL_BY_SDKMAN:+has_values}" ]; then
198+
prepare_jdks::prepare_jdks "${PREPARE_JDKS_INSTALL_BY_SDKMAN[@]}"
199+
fi

0 commit comments

Comments
 (0)