Skip to content

Commit

Permalink
refactor: split java_utils.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Jul 18, 2023
1 parent 63c1e0e commit 563a7ad
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 78 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,22 @@ api functions:
- `cu::print_calling_command_line`
- `cu::die`

## [`java_utils.sh`](lib/javautils.sh)

java util functions.

api functions:

- `jvu::get_java_version`
- `jvu::switch_to_jdk`
- `jvu::java_cmd`

## [`java_build_utils.sh`](lib/java_build_utils.sh)

java and maven util functions for build.
maven util functions for build.

api functions:

- java operation functions:
- `jvb::get_java_version`
- `jvb::java_cmd`
- maven operation functions:
- `jvb::mvn_cmd`

Expand Down
43 changes: 0 additions & 43 deletions lib/java_build_utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,6 @@ set -eEuo pipefail
# shellcheck source=common_utils.sh
source "$__source_guard_364DF1B5_9CA2_44D3_9C62_CDF6C2ECB24F/common_utils.sh"

#################################################################################
# java operation functions
#################################################################################

# output the java version extracted from `java -version`
jvb::get_java_version() {
(($# <= 1)) || cu::die "${FUNCNAME[0]} requires at most 1 argument! But provided $#: $*"

local java_home_path="${1:-$JAVA_HOME}"
"$java_home_path/bin/java" -version 2>&1 | awk -F\" '/ version "/{print $2}'
}

# FIXME hard code memery settings
# shellcheck disable=SC2034
readonly JVB_DEFAULT_JAVA_OPTS=(
-Xmx256m -Xms256m
-server -ea
-Duser.language=en -Duser.country=US
)

readonly JVB_JAVA_OPT_DEFAULT_DEBUG_PORT=5050

# set env variable ENABLE_JAVA_RUN_DEBUG to enable java debug mode
jvb::java_cmd() {
(($# > 0)) || cu::die "${FUNCNAME[0]} requires arguments! But no provided"

local debug_opts="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=${JVB_JAVA_OPT_DEBUG_PORT}"

cu::log_then_run "$JAVA_HOME/bin/java" \
"${JVB_JAVA_OPTS[@]}" \
${JVB_ENABLE_JAVA_RUN_VERBOSE_CLASS+-verbose:class} \
${JVB_ENABLE_JAVA_RUN_DEBUG+$debug_opts} \
"$@"
}

#################################################################################
# maven operation functions
#################################################################################
Expand Down Expand Up @@ -143,14 +108,6 @@ jvb::get_mvn_local_repository_dir() {
jvb::__auto_run_when_source() {
# set VAR if absent

if [ -z "${JVB_JAVA_OPT_DEBUG_PORT:-}" ]; then
JVB_JAVA_OPT_DEBUG_PORT="$JVB_JAVA_OPT_DEFAULT_DEBUG_PORT"
fi

if [ -z "${JVB_JAVA_OPTS[*]:-}" ]; then
JVB_JAVA_OPTS=("${JVB_DEFAULT_JAVA_OPTS[@]}")
fi

if [ -z "${JVB_MVN_OPTS[*]:-}" ]; then
JVB_MVN_OPTS=("${JVB_DEFAULT_MVN_OPTS[@]}")
fi
Expand Down
155 changes: 155 additions & 0 deletions lib/java_utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#!/bin/bash
#
# java util functions.
#
################################################################################
# api functions:
#
# - java operation functions:
# - jvu::get_java_version
# - jvu::java_cmd
################################################################################
#
#_ source guard begin _#
[ -z "${__source_guard_ED79675F_4289_4394_A843_03D06DB48AFA:+has_value}" ] || return 0
__source_guard_ED79675F_4289_4394_A843_03D06DB48AFA="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
readonly __source_guard_ED79675F_4289_4394_A843_03D06DB48AFA
#_ source guard end _#

set -eEuo pipefail

# shellcheck source=common_utils.sh
source "$__source_guard_ED79675F_4289_4394_A843_03D06DB48AFA/common_utils.sh"

#################################################################################
# java info functions
#################################################################################

# output the java version extracted from `java -version`
jvu::get_java_version() {
(($# <= 1)) || cu::die "${FUNCNAME[0]} requires at most 1 argument! But provided $#: $*"

local java_home_path="${1:-$JAVA_HOME}"
"$java_home_path/bin/java" -version 2>&1 | awk -F\" '/ version "/{print $2}'
}

#################################################################################
# java operation functions
#################################################################################

jvu::_validate_java_home() {
_jvu_VALIDATE_JAVA_HOME_ERR_MSG=

local -r java_home="$1"
if cu::is_blank_string "$java_home"; then
_jvu_VALIDATE_JAVA_HOME_ERR_MSG="java home($java_home) is BLANK"
return 1
fi

if [ ! -e "$java_home" ]; then
_jvu_VALIDATE_JAVA_HOME_ERR_MSG="java home($java_home) is NOT existed"
return 1
fi
if [ ! -d "$java_home" ]; then
_jvu_VALIDATE_JAVA_HOME_ERR_MSG="java home($java_home) is NOT directory"
return 1
fi

local java_path="$java_home/bin/java"
if [ ! -f "$java_path" ]; then
_jvu_VALIDATE_JAVA_HOME_ERR_MSG="\$java_home/bin/java($java_path) is NOT existed"
return 1
fi
if [ ! -x "$java_path" ]; then
_jvu_VALIDATE_JAVA_HOME_ERR_MSG="\$java_home/bin/java($java_path) is NOT executable"
return 1
fi
}

# switch JAVA_HOME to target
#
# available switch target:
# - version pattern, e.g. 11, 17
# determinate JAVA_HOME by env var JAVAx_HOME
# - /path/to/java/home
#
# usage:
# jvu::switch_to_jdk 17
# jvu::switch_to_jdk /path/to/java/home
#
jvu::switch_to_jdk() {
[ $# == 1 ] || cu::die "${FUNCNAME[0]} requires exact 1 argument! But provided $#: $*"

local -r switch_target="$1"
cu::is_blank_string "$switch_target" && cu::die "switch target($switch_target) is BLANK!"

# 1. first check env var JAVAx_HOME(e.g. JAVA11_HOME)
#
# aka. set by java version, e.g.
# jvu::switch_to_jdk 11
if cu::is_number_string "$switch_target"; then
local jdk_home_var_name="JAVA${switch_target}_HOME"
# check env var JAVAx_HOME is defined or not
if [ -z "${!jdk_home_var_name+defined}" ]; then
cu::die "use \$$jdk_home_var_name as switch target, but \$$jdk_home_var_name is NOT defined!" >&2
fi
local jdk_home="${!jdk_home_var_name:-}"
if ! jvu::_validate_java_home "$jdk_home"; then
cu::die "found \$$jdk_home_var_name($jdk_home) for switch target $switch_target, but $_jvu_VALIDATE_JAVA_HOME_ERR_MSG!" >&2
fi

export JAVA_HOME="$jdk_home"
return
fi

# 2. then switch as a directory
#
# aka. set by path of java installation, e.g.
# jvu::switch_to_jdk /path/to/java/home
local jdk_home="$switch_target"
if ! jvu::_validate_java_home "$jdk_home"; then
cu:die "use $switch_target directory as switch target, but $_jvu_VALIDATE_JAVA_HOME_ERR_MSG!" >&2
fi
export JAVA_HOME="$jdk_home"
}

# FIXME hard code memery settings
# shellcheck disable=SC2034
readonly JVU_DEFAULT_JAVA_OPTS=(
-Xmx256m -Xms256m
-server -ea
-Duser.language=en -Duser.country=US
)

readonly JVU_JAVA_OPT_DEFAULT_DEBUG_PORT=5050

# set env variable ENABLE_JAVA_RUN_DEBUG to enable java debug mode
jvu::java_cmd() {
(($# > 0)) || cu::die "${FUNCNAME[0]} requires arguments! But no provided"

local debug_opts="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=${JVU_JAVA_OPT_DEBUG_PORT}"

cu::log_then_run "$JAVA_HOME/bin/java" \
"${JVU_JAVA_OPTS[@]}" \
${JVU_ENABLE_JAVA_RUN_VERBOSE_CLASS+-verbose:class} \
${JVU_ENABLE_JAVA_RUN_DEBUG+$debug_opts} \
"$@"
}

################################################################################
# auto run logic when source
################################################################################

jvu::__auto_run_when_source() {
# set VAR if absent

if [ -z "${JVU_JAVA_OPT_DEBUG_PORT:-}" ]; then
JVU_JAVA_OPT_DEBUG_PORT="$JVU_JAVA_OPT_DEFAULT_DEBUG_PORT"
fi

if [ -z "${JVU_JAVA_OPTS[*]:-}" ]; then
JVU_JAVA_OPTS=("${JVU_DEFAULT_JAVA_OPTS[@]}")
fi
}

jvu::__auto_run_when_source
34 changes: 3 additions & 31 deletions lib/prepare_jdks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ set -eEuo pipefail

# shellcheck source=common_utils.sh
source "$__source_guard_E2AA8C4F_215B_4CDA_9816_429C7A2CD465/common_utils.sh"
source "$__source_guard_E2AA8C4F_215B_4CDA_9816_429C7A2CD465/java_utils.sh"

# install sdkman.
#
Expand Down Expand Up @@ -188,35 +189,6 @@ prepare_jdks::_get_latest_java_version() {
echo "$input" | cu::get_latest_version_match "$version_pattern"
}

prepare_jdks::_validate_java_home() {
_PREPARE_JDKS_VALIDATE_JAVA_HOME_ERR_MSG=

local -r java_home="$1"
if cu::is_blank_string "$java_home"; then
_PREPARE_JDKS_VALIDATE_JAVA_HOME_ERR_MSG="java home($java_home) is BLANK"
return 1
fi

if [ ! -e "$java_home" ]; then
_PREPARE_JDKS_VALIDATE_JAVA_HOME_ERR_MSG="java home($java_home) is NOT existed"
return 1
fi
if [ ! -d "$java_home" ]; then
_PREPARE_JDKS_VALIDATE_JAVA_HOME_ERR_MSG="java home($java_home) is NOT directory"
return 1
fi

local java_path="$java_home/bin/java"
if [ ! -f "$java_path" ]; then
_PREPARE_JDKS_VALIDATE_JAVA_HOME_ERR_MSG="\$java_home/bin/java($java_path) is NOT existed"
return 1
fi
if [ ! -x "$java_path" ]; then
_PREPARE_JDKS_VALIDATE_JAVA_HOME_ERR_MSG="\$java_home/bin/java($java_path) is NOT executable"
return 1
fi
}

# switch JAVA_HOME to target
#
# available switch target:
Expand Down Expand Up @@ -266,7 +238,7 @@ prepare_jdks::switch_to_jdk() {
if [ -n "${!jdk_home_var_name+defined}" ]; then
local jdk_home="${!jdk_home_var_name:-}"

if prepare_jdks::_validate_java_home "$jdk_home"; then
if jvu::_validate_java_home "$jdk_home"; then
if ! $prepare_mode; then
export JAVA_HOME="$jdk_home"
fi
Expand Down Expand Up @@ -323,7 +295,7 @@ prepare_jdks::switch_to_jdk() {
local jdk_home
jdk_home="$(prepare_jdks::_get_jdk_path_from_jdk_name_of_sdkman "$version")"

if prepare_jdks::_validate_java_home "$jdk_home"; then
if jvu::_validate_java_home "$jdk_home"; then
if ! $prepare_mode; then
export JAVA_HOME="$jdk_home"
fi
Expand Down

0 comments on commit 563a7ad

Please sign in to comment.