refactor: Generalize logging

This commit is contained in:
Serge Guzik 2019-02-20 12:23:39 +02:00
parent a118ec64db
commit c43f699705

100
warp4j
View File

@ -47,7 +47,29 @@ function print_help {
trap "exit 1" TERM trap "exit 1" TERM
export TOP_PID=$$ export TOP_PID=$$
function fail() { function fail() {
kill -s TERM $TOP_PID kill -s TERM $TOP_PID
}
# fail with a message
function fail_with() {
local message=$1
echo "Error: $message" >&2
fail
}
# if error code is not zero fail with a message
function fail_if() {
local error_code=$1
local message=$2
if [[ $error_code != 0 ]]; then
fail_with "$message"
fi
}
# common warning
function warn() {
local message=$1
echo "Warning: $message" >&2
} }
# platform IDs # platform IDs
@ -62,8 +84,7 @@ function get_this_platform() {
Linux*) echo $LIN ;; Linux*) echo $LIN ;;
Darwin*) echo $MAC ;; Darwin*) echo $MAC ;;
*) *)
echo "Error: Unsupported platform $this_platform" >&2 fail_with "Unsupported platform $this_platform"
fail
;; ;;
esac esac
} }
@ -125,8 +146,7 @@ while [[ $# -gt 0 ]]; do
shift 2 shift 2
;; ;;
-*|--*) # unsupported options -*|--*) # unsupported options
echo "Error: Unsupported option $1" >&2 fail_with "Unsupported option $1"
exit 1
;; ;;
*) *)
POSITIONAL+=("$1") # store positional arguments POSITIONAL+=("$1") # store positional arguments
@ -172,8 +192,7 @@ check_deps
# apart from options only one argument is allowed # apart from options only one argument is allowed
if [[ $# -gt 1 ]]; then if [[ $# -gt 1 ]]; then
echo "Error: Too many arguments: $@, expecting only jar name" >&2 fail_with "Too many arguments: $@, expecting only jar name"
exit 1
else else
JAR=$1 JAR=$1
fi fi
@ -191,8 +210,7 @@ function java_version_is_correct() {
# validate java version # validate java version
if [[ $JAVA_VERSION ]] && ! java_version_is_correct $JAVA_VERSION ; then if [[ $JAVA_VERSION ]] && ! java_version_is_correct $JAVA_VERSION ; then
echo "Error: JDK version \"$JAVA_VERSION\" is not correct" >&2 fail_with "JDK version \"$JAVA_VERSION\" is not correct"
exit 1
fi fi
JVM_IMPL_HOTSPOT=hotspot JVM_IMPL_HOTSPOT=hotspot
@ -202,8 +220,7 @@ JVM_IMPL_OPENJ9=openj9
if [[ $JVM_IMPL ]] && if [[ $JVM_IMPL ]] &&
[[ $JVM_IMPL != $JVM_IMPL_HOTSPOT ]] && [[ $JVM_IMPL != $JVM_IMPL_HOTSPOT ]] &&
[[ $JVM_IMPL != $JVM_IMPL_OPENJ9 ]]; then [[ $JVM_IMPL != $JVM_IMPL_OPENJ9 ]]; then
echo "Error: jvm implementation \"$JVM_IMPL\" is not correct" >&2 fail_with "jvm implementation \"$JVM_IMPL\" is not correct"
exit 1
fi fi
LATEST_LTS=11 # latest LTS java branch LATEST_LTS=11 # latest LTS java branch
@ -258,12 +275,8 @@ type=$JAVA_DISTRO_TYPE"
function fetch_distro_info() { function fetch_distro_info() {
local platform=$1 # platform ID local platform=$1 # platform ID
local branch=$2 # 8/9/10/11... local branch=$2 # 8/9/10/11...
echo "Fetching $JVM_IMPL-$JAVA_DISTRO_TYPE-$branch info..." >&2
curl -s $(api_url info $platform) curl -s $(api_url info $platform)
if [[ $? != 0 ]]; then fail_if $? "Failed to fetch java $branch info"
echo "Error: Failed to fetch java $branch info" >&2
fail
fi
} }
# extracts all concrete java versions that match the version specified by user # extracts all concrete java versions that match the version specified by user
@ -323,16 +336,14 @@ LAUNCHER_NAME=$JAR_NAME # launcher name inside bundle
# checking jar file exists # checking jar file exists
if [[ ! -e $JAR ]]; then if [[ ! -e $JAR ]]; then
echo "Error: File \"$JAR\" does not exist" >&2 fail_with "File \"$JAR\" does not exist"
exit 1
fi fi
# checking file is actually java archive # checking file is actually java archive
if ([[ $(file $JAR) != *"Java"* ]] && # it could be "Java archive data" or "Java Jar file data (zip)" if ([[ $(file $JAR) != *"Java"* ]] && # it could be "Java archive data" or "Java Jar file data (zip)"
[[ $(file $JAR) != *"Zip"* ]]) || # or "Zip archive data" [[ $(file $JAR) != *"Zip"* ]]) || # or "Zip archive data"
[[ $JAR_EXTENSION_LOWERCASE != "jar" ]]; then [[ $JAR_EXTENSION_LOWERCASE != "jar" ]]; then
echo "Error: File \"$JAR\" is not a java archive" >&2 fail_with "File \"$JAR\" is not a java archive"
exit 1
fi fi
# even if this platform is not targeted, we still need # even if this platform is not targeted, we still need
@ -454,8 +465,7 @@ function find_latest_version() {
fi fi
done done
if [[ -z $matched_version ]]; then if [[ -z $matched_version ]]; then
echo "Error: Can't find distro that matches $user_version" >&2 fail_with "Can't find distro that matches $user_version"
fail
fi fi
echo $matched_version echo $matched_version
} }
@ -470,8 +480,7 @@ function find_distro_link() {
| awk '{print $2}' \ | awk '{print $2}' \
| sed -e 's/"//g' -e 's/,//') | sed -e 's/"//g' -e 's/,//')
if [[ -z $link ]]; then if [[ -z $link ]]; then
echo "Error: Can't find download link for $version" >&2 fail_with "Can't find download link for $version"
fail
fi fi
echo "$link" echo "$link"
} }
@ -490,8 +499,7 @@ function download_distro() {
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
touch $MARKER_DOWNLOADED touch $MARKER_DOWNLOADED
else else
echo "Error: Failed to download $JVM_IMPL-$JAVA_DISTRO_TYPE-$version-$platform" >&2 fail_with "Failed to download $JVM_IMPL-$JAVA_DISTRO_TYPE-$version-$platform"
fail
fi fi
) )
} }
@ -587,8 +595,7 @@ function ensure_distro_unpacked() {
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
touch "$download_dir/$MARKER_UNPACKED" touch "$download_dir/$MARKER_UNPACKED"
else else
echo "Error: Failed to unpack $JVM_IMPL-$JAVA_DISTRO_TYPE-$version-$platform" >&2 fail_with "Failed to unpack $JVM_IMPL-$JAVA_DISTRO_TYPE-$version-$platform"
fail
fi fi
fi fi
} }
@ -621,10 +628,7 @@ function create_optimized_runtime() {
--module-path "$jmods" \ --module-path "$jmods" \
--add-modules $MODULES \ --add-modules $MODULES \
--output "$BUNDLES_PATH/$platform/$BUNDLED_DISTRO_SUBDIR" --output "$BUNDLES_PATH/$platform/$BUNDLED_DISTRO_SUBDIR"
if [[ $? != 0 ]]; then fail_if $? "Failed to optimize runtime"
echo "Error: Failed to optimize runtime" >&2
fail
fi
} }
# creates warp bundle for the platform # creates warp bundle for the platform
@ -669,16 +673,10 @@ function warp_targets() {
--exec "$LAUNCHER_NAME.sh" \ --exec "$LAUNCHER_NAME.sh" \
--output "$WARPED_TEMP_PATH/$LIN/$APP_NAME" \ --output "$WARPED_TEMP_PATH/$LIN/$APP_NAME" \
&> /dev/null &> /dev/null
if [[ $? != 0 ]]; then fail_if $? "Failed to warp for $LIN"
echo "Error: Failed to warp for $LIN" >&2
fail
fi
echo "Archiving for $LIN..." echo "Archiving for $LIN..."
tar -C "$WARPED_TEMP_PATH/$LIN" -czf "$WARPED_TEMP_PATH/$APP_NAME-$LIN-x64.tar.gz" "$APP_NAME" tar -C "$WARPED_TEMP_PATH/$LIN" -czf "$WARPED_TEMP_PATH/$APP_NAME-$LIN-x64.tar.gz" "$APP_NAME"
if [[ $? != 0 ]]; then fail_if $? "Failed to make archive for $LIN"
echo "Error: Failed to make archive for $LIN" >&2
fail
fi
mv "$WARPED_TEMP_PATH/$LIN/$APP_NAME" "$WARPED_PATH/$APP_NAME-$LIN-x64" mv "$WARPED_TEMP_PATH/$LIN/$APP_NAME" "$WARPED_PATH/$APP_NAME-$LIN-x64"
mv "$WARPED_TEMP_PATH/$APP_NAME-$LIN-x64.tar.gz" "$WARPED_PATH" mv "$WARPED_TEMP_PATH/$APP_NAME-$LIN-x64.tar.gz" "$WARPED_PATH"
rmdir "$WARPED_TEMP_PATH/$LIN" rmdir "$WARPED_TEMP_PATH/$LIN"
@ -693,16 +691,10 @@ function warp_targets() {
--exec "$LAUNCHER_NAME.sh" \ --exec "$LAUNCHER_NAME.sh" \
--output "$WARPED_TEMP_PATH/$MAC/$APP_NAME" \ --output "$WARPED_TEMP_PATH/$MAC/$APP_NAME" \
&> /dev/null &> /dev/null
if [[ $? != 0 ]]; then fail_if $? "Failed to warp for $MAC"
echo "Error: Failed to warp for $MAC" >&2
fail
fi
echo "Archiving for $MAC..." echo "Archiving for $MAC..."
tar -C "$WARPED_TEMP_PATH/$MAC" -czf "$WARPED_TEMP_PATH/$APP_NAME-$MAC-x64.tar.gz" "$APP_NAME" tar -C "$WARPED_TEMP_PATH/$MAC" -czf "$WARPED_TEMP_PATH/$APP_NAME-$MAC-x64.tar.gz" "$APP_NAME"
if [[ $? != 0 ]]; then fail_if $? "Failed to make archive for $MAC"
echo "Error: Failed to make archive for $MAC" >&2
fail
fi
mv "$WARPED_TEMP_PATH/$MAC/$APP_NAME" "$WARPED_PATH/$APP_NAME-$MAC-x64" mv "$WARPED_TEMP_PATH/$MAC/$APP_NAME" "$WARPED_PATH/$APP_NAME-$MAC-x64"
mv "$WARPED_TEMP_PATH/$APP_NAME-$MAC-x64.tar.gz" "$WARPED_PATH" mv "$WARPED_TEMP_PATH/$APP_NAME-$MAC-x64.tar.gz" "$WARPED_PATH"
rmdir "$WARPED_TEMP_PATH/$MAC" rmdir "$WARPED_TEMP_PATH/$MAC"
@ -717,21 +709,15 @@ function warp_targets() {
--exec "$LAUNCHER_NAME.cmd" \ --exec "$LAUNCHER_NAME.cmd" \
--output "$WARPED_TEMP_PATH/$WIN/$APP_NAME.exe" \ --output "$WARPED_TEMP_PATH/$WIN/$APP_NAME.exe" \
&> /dev/null &> /dev/null
if [[ $? != 0 ]]; then fail_if $? "Failed to warp for $WIN"
echo "Error: Failed to warp for $WIN" >&2
fail
fi
if command -v zip &> /dev/null ; then ( if command -v zip &> /dev/null ; then (
echo "Archiving for $WIN..." echo "Archiving for $WIN..."
cd "$WARPED_TEMP_PATH/$WIN" cd "$WARPED_TEMP_PATH/$WIN"
zip -r "$WARPED_TEMP_PATH/$APP_NAME-$WIN-x64.zip" "$APP_NAME.exe" &> /dev/null zip -r "$WARPED_TEMP_PATH/$APP_NAME-$WIN-x64.zip" "$APP_NAME.exe" &> /dev/null
if [[ $? != 0 ]]; then fail_if $? "Failed to make archive for $WIN"
echo "Error: Failed to make archive for $WIN" >&2
fail
fi
mv "$WARPED_TEMP_PATH/$APP_NAME-$WIN-x64.zip" "$WARPED_PATH" mv "$WARPED_TEMP_PATH/$APP_NAME-$WIN-x64.zip" "$WARPED_PATH"
) else ) else
echo "Warning: 'zip' not found, will skip creation of archive for windows" >&2 warn "'zip' not found, will skip creation of archive for windows"
fi fi
mv "$WARPED_TEMP_PATH/$WIN/$APP_NAME.exe" "$WARPED_PATH/$APP_NAME-windows-x64.exe" mv "$WARPED_TEMP_PATH/$WIN/$APP_NAME.exe" "$WARPED_PATH/$APP_NAME-windows-x64.exe"
rmdir "$WARPED_TEMP_PATH/$WIN" rmdir "$WARPED_TEMP_PATH/$WIN"