refactor: Quote all variables with potential spaces within

This commit is contained in:
Serge Guzik 2019-02-13 18:10:58 +02:00
parent 436c7cae3d
commit d73e02cd9d

122
warp4j
View File

@ -281,14 +281,14 @@ function print_launcher_bash() {
printf "%s" \ printf "%s" \
'#!/usr/bin/env bash '#!/usr/bin/env bash
JAVA_DIST='$BUNDLED_DISTRO_SUBDIR' JAVA_DIST='"$BUNDLED_DISTRO_SUBDIR"'
JAR='$JAR_NAME'.jar JAR='"$JAR_NAME"'.jar
DIR="$(cd "$(dirname "$0")" ; pwd -P)" DIR="$(cd "$(dirname "$0")" ; pwd -P)"
JAVA=$DIR/$JAVA_DIST/bin/java JAVA=$DIR/$JAVA_DIST/bin/java
JAR_PATH=$DIR/$JAR JAR_PATH=$DIR/$JAR
exec $JAVA '"$JVM_OPTIONS"' -jar $JAR_PATH $@ exec "$JAVA" '"$JVM_OPTIONS"' -jar "$JAR_PATH" "$@"
' '
} }
@ -299,8 +299,8 @@ printf "%s" \
SETLOCAL SETLOCAL
SET "JAVA_DIST='$BUNDLED_DISTRO_SUBDIR'" SET "JAVA_DIST='"$BUNDLED_DISTRO_SUBDIR"'"
SET "JAR='$JAR_NAME'.jar" SET "JAR='"$JAR_NAME"'.jar"
SET "JAVA=%~dp0\%JAVA_DIST%\bin\java.exe" SET "JAVA=%~dp0\%JAVA_DIST%\bin\java.exe"
SET "JAR_PATH=%~dp0\%JAR%" SET "JAR_PATH=%~dp0\%JAR%"
@ -332,13 +332,13 @@ MARKER_UNPACKED="unpacked" # after runtime uncompress
# returns latest cached version that matches version specified by user # returns latest cached version that matches version specified by user
function find_latest_cached() { function find_latest_cached() {
local platform=$1 # in terms of adoptopenjdk.net: windows, linux, mac local platform=$1
local user_version=$2 local user_version=$2
local platform_dir=$JAVA_DOWNLOAD_PATH/$platform/ local platform_dir=$JAVA_DOWNLOAD_PATH/$platform/
# turning something like "11.0.1+13" into regexp like "^11\.0\.1\+13" # turning something like "11.0.1+13" into regexp like "^11\.0\.1\+13"
local pattern="^"$(echo $user_version \ local pattern="^"$(echo $user_version \
| sed -e 's/\./\\\./g' -e 's/\+/\\\+/g') | sed -e 's/\./\\\./g' -e 's/\+/\\\+/g')
local versions=$(ls -1 $platform_dir 2> /dev/null \ local versions=$(ls -1 "$platform_dir" 2> /dev/null \
| sort --reverse --version-sort) | sort --reverse --version-sort)
local version local version
for v in ${versions[@]}; do for v in ${versions[@]}; do
@ -417,10 +417,10 @@ function download_distro() {
local link=$3 local link=$3
local download_dir=$JAVA_DOWNLOAD_PATH/$platform/$version local download_dir=$JAVA_DOWNLOAD_PATH/$platform/$version
echo "Downloading $JVM_IMPL-$JAVA_DISTRO_TYPE-$version-$platform..." echo "Downloading $JVM_IMPL-$JAVA_DISTRO_TYPE-$version-$platform..."
rm -rf $download_dir rm -rf "$download_dir"
mkdir -p $download_dir mkdir -p "$download_dir"
(cd $download_dir (cd "$download_dir"
curl --progress-bar --location --remote-name $link curl --progress-bar --location --remote-name "$link"
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
touch $MARKER_DOWNLOADED touch $MARKER_DOWNLOADED
else else
@ -441,7 +441,7 @@ function ensure_distro_cached() {
distro_info=$(fetch_distro_info $platform $LATEST_LTS) distro_info=$(fetch_distro_info $platform $LATEST_LTS)
CONCRETE_JAVA_VERSION=$(find_latest_version "$distro_info" $LATEST_LTS) CONCRETE_JAVA_VERSION=$(find_latest_version "$distro_info" $LATEST_LTS)
distro_link=$(find_distro_link "$distro_info" $CONCRETE_JAVA_VERSION) distro_link=$(find_distro_link "$distro_info" $CONCRETE_JAVA_VERSION)
download_distro $platform $CONCRETE_JAVA_VERSION $distro_link download_distro $platform $CONCRETE_JAVA_VERSION "$distro_link"
else else
CONCRETE_JAVA_VERSION=$(find_latest_cached $platform $LATEST_LTS) CONCRETE_JAVA_VERSION=$(find_latest_cached $platform $LATEST_LTS)
fi fi
@ -450,7 +450,7 @@ function ensure_distro_cached() {
distro_info=$(fetch_distro_info $platform $JAVA_VERSION_BASE) distro_info=$(fetch_distro_info $platform $JAVA_VERSION_BASE)
CONCRETE_JAVA_VERSION=$(find_latest_version "$distro_info" $JAVA_VERSION) CONCRETE_JAVA_VERSION=$(find_latest_version "$distro_info" $JAVA_VERSION)
distro_link=$(find_distro_link "$distro_info" $CONCRETE_JAVA_VERSION) distro_link=$(find_distro_link "$distro_info" $CONCRETE_JAVA_VERSION)
download_distro $platform $CONCRETE_JAVA_VERSION $distro_link download_distro $platform $CONCRETE_JAVA_VERSION "$distro_link"
else else
CONCRETE_JAVA_VERSION=$(find_latest_cached $platform $JAVA_VERSION) CONCRETE_JAVA_VERSION=$(find_latest_cached $platform $JAVA_VERSION)
fi fi
@ -465,7 +465,7 @@ function ensure_distro_cached() {
fi fi
if [[ ! $(find_latest_cached $platform $CONCRETE_JAVA_VERSION) ]]; then if [[ ! $(find_latest_cached $platform $CONCRETE_JAVA_VERSION) ]]; then
distro_link=$(find_distro_link "$distro_info" $CONCRETE_JAVA_VERSION) distro_link=$(find_distro_link "$distro_info" $CONCRETE_JAVA_VERSION)
download_distro $platform $CONCRETE_JAVA_VERSION $distro_link download_distro $platform $CONCRETE_JAVA_VERSION "$distro_link"
fi fi
fi fi
} }
@ -486,15 +486,15 @@ function ensure_distro_unpacked() {
if [[ ! -e $download_dir/$MARKER_UNPACKED ]]; then if [[ ! -e $download_dir/$MARKER_UNPACKED ]]; then
echo "Uncompressing $JVM_IMPL-$JAVA_DISTRO_TYPE-$version-$platform" echo "Uncompressing $JVM_IMPL-$JAVA_DISTRO_TYPE-$version-$platform"
# removing all leftover directories # removing all leftover directories
for d in $download_dir/* ; do for d in "$download_dir"/* ; do
if [[ -d $d ]]; then if [[ -d $d ]]; then
rm -rf $d rm -rf "$d"
fi fi
done done
case $platform in case $platform in
$LIN) $LIN)
mkdir -p $unpacked_dir mkdir -p "$unpacked_dir"
tar --strip-components=1 -C $unpacked_dir -xzf $download_dir/*.tar.gz tar --strip-components=1 -C "$unpacked_dir" -xzf "$download_dir"/*.tar.gz
;; ;;
$MAC) $MAC)
# to uncompess distro for macOS we need to use wildcards # to uncompess distro for macOS we need to use wildcards
@ -508,18 +508,18 @@ function ensure_distro_unpacked() {
else else
tar_cmd="tar --wildcards" tar_cmd="tar --wildcards"
fi fi
mkdir -p $unpacked_dir mkdir -p "$unpacked_dir"
$tar_cmd --strip-components=3 -C $unpacked_dir -xzf $download_dir/*.tar.gz \ $tar_cmd --strip-components=3 -C "$unpacked_dir" -xzf "$download_dir"/*.tar.gz \
"jdk*/Contents/Home" "jdk*/Contents/Home"
;; ;;
$WIN) $WIN)
(cd $download_dir (cd "$download_dir"
unzip -oq *.zip && mv jdk* $UNPACKED_SUBDIR unzip -oq *.zip && mv jdk* $UNPACKED_SUBDIR
) )
;; ;;
esac esac
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 echo "Error: Failed to unpack $JVM_IMPL-$JAVA_DISTRO_TYPE-$version-$platform" >&2
fail fail
@ -540,7 +540,7 @@ JDEPS=$JDK_PATH/bin/jdeps
if [[ $JAVA_DISTRO_TYPE == $DISTRO_TYPE_JDK ]]; then if [[ $JAVA_DISTRO_TYPE == $DISTRO_TYPE_JDK ]]; then
echo "Analyzing dependencies..." echo "Analyzing dependencies..."
# TODO check for errors # TODO check for errors
MODULES=$($JDEPS --print-module-deps $JAR | grep -v Warning) MODULES=$("$JDEPS" --print-module-deps "$JAR" | grep -v Warning)
fi fi
# creates minimized runtime for the platform # creates minimized runtime for the platform
@ -548,13 +548,13 @@ function create_optimized_runtime() {
local platform=$1 local platform=$1
local jmods=$JAVA_DOWNLOAD_PATH/$platform/$CONCRETE_JAVA_VERSION/$UNPACKED_SUBDIR/jmods local jmods=$JAVA_DOWNLOAD_PATH/$platform/$CONCRETE_JAVA_VERSION/$UNPACKED_SUBDIR/jmods
echo "Creating minimal runtime for $platform..." echo "Creating minimal runtime for $platform..."
$JLINK \ "$JLINK" \
--no-header-files \ --no-header-files \
--no-man-pages \ --no-man-pages \
--strip-debug \ --strip-debug \
--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 if [[ $? != 0 ]]; then
echo "Error: Failed to optimize runtime" >&2 echo "Error: Failed to optimize runtime" >&2
fail fail
@ -569,22 +569,22 @@ function create_bundle() {
create_optimized_runtime $platform create_optimized_runtime $platform
;; ;;
$DISTRO_TYPE_JRE) $DISTRO_TYPE_JRE)
mkdir -p $BUNDLES_PATH/$platform/$BUNDLED_DISTRO_SUBDIR mkdir -p "$BUNDLES_PATH/$platform/$BUNDLED_DISTRO_SUBDIR"
cp -r $JAVA_DOWNLOAD_PATH/$platform/$CONCRETE_JAVA_VERSION/$UNPACKED_SUBDIR/* \ cp -r "$JAVA_DOWNLOAD_PATH/$platform/$CONCRETE_JAVA_VERSION/$UNPACKED_SUBDIR"/* \
$BUNDLES_PATH/$platform/$BUNDLED_DISTRO_SUBDIR "$BUNDLES_PATH/$platform/$BUNDLED_DISTRO_SUBDIR"
;; ;;
esac esac
case $platform in case $platform in
$WIN) print_launcher_cmd > $BUNDLES_PATH/$platform/$LAUNCHER_NAME.cmd ;; $WIN) print_launcher_cmd > "$BUNDLES_PATH/$platform/$LAUNCHER_NAME.cmd" ;;
*) print_launcher_bash > $BUNDLES_PATH/$platform/$LAUNCHER_NAME.sh *) print_launcher_bash > "$BUNDLES_PATH/$platform/$LAUNCHER_NAME.sh"
chmod +x $BUNDLES_PATH/$platform/$LAUNCHER_NAME.sh chmod +x "$BUNDLES_PATH/$platform/$LAUNCHER_NAME.sh"
;; ;;
esac esac
cp $JAR $BUNDLES_PATH/$platform/ cp "$JAR" "$BUNDLES_PATH/$platform/"
} }
# remove old bundles # remove old bundles
rm -rf $BUNDLES_PATH rm -rf "$BUNDLES_PATH"
# actually create bundles for all targets # actually create bundles for all targets
for target in ${TARGETS[@]}; do for target in ${TARGETS[@]}; do
create_bundle $target create_bundle $target
@ -592,56 +592,56 @@ done
# creates binaries and archives for all targets # creates binaries and archives for all targets
function warp_targets() { function warp_targets() {
mkdir -p $WARPED_PATH mkdir -p "$WARPED_PATH"
if [[ ${TARGETS[*]} == *"$LIN"* ]]; then if [[ ${TARGETS[*]} == *"$LIN"* ]]; then
echo "Warping for $LIN..." echo "Warping for $LIN..."
mkdir -p $WARPED_TEMP_PATH/$LIN mkdir -p "$WARPED_TEMP_PATH/$LIN"
warp-packer \ warp-packer \
--arch linux-x64 \ --arch linux-x64 \
--input_dir $BUNDLES_PATH/$LIN \ --input_dir "$BUNDLES_PATH/$LIN" \
--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
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"
mv $WARPED_TEMP_PATH/$LIN/$APP_NAME $WARPED_PATH/$APP_NAME-$LIN mv "$WARPED_TEMP_PATH/$LIN/$APP_NAME" "$WARPED_PATH/$APP_NAME-$LIN"
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"
fi fi
if [[ ${TARGETS[*]} == *"$MAC"* ]]; then if [[ ${TARGETS[*]} == *"$MAC"* ]]; then
echo "Warping for $MAC..." echo "Warping for $MAC..."
mkdir -p $WARPED_TEMP_PATH/$MAC mkdir -p "$WARPED_TEMP_PATH/$MAC"
warp-packer \ warp-packer \
--arch macos-x64 \ --arch macos-x64 \
--input_dir $BUNDLES_PATH/$MAC \ --input_dir "$BUNDLES_PATH/$MAC" \
--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
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"
mv $WARPED_TEMP_PATH/$MAC/$APP_NAME $WARPED_PATH/$APP_NAME-$MAC mv "$WARPED_TEMP_PATH/$MAC/$APP_NAME" "$WARPED_PATH/$APP_NAME-$MAC"
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"
fi fi
if [[ ${TARGETS[*]} == *"$WIN"* ]]; then if [[ ${TARGETS[*]} == *"$WIN"* ]]; then
echo "Warping for $WIN..." echo "Warping for $WIN..."
mkdir -p $WARPED_TEMP_PATH/$WIN mkdir -p "$WARPED_TEMP_PATH/$WIN"
warp-packer \ warp-packer \
--arch windows-x64 \ --arch windows-x64 \
--input_dir $BUNDLES_PATH/$WIN \ --input_dir "$BUNDLES_PATH/$WIN" \
--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
(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
) )
mv $WARPED_TEMP_PATH/$WIN/$APP_NAME.exe $WARPED_PATH/$APP_NAME-windows.exe mv "$WARPED_TEMP_PATH/$WIN/$APP_NAME.exe" "$WARPED_PATH/$APP_NAME-windows.exe"
mv $WARPED_TEMP_PATH/$APP_NAME-$WIN-x64.zip $WARPED_PATH mv "$WARPED_TEMP_PATH/$APP_NAME-$WIN-x64.zip" "$WARPED_PATH"
rmdir $WARPED_TEMP_PATH/$WIN rmdir "$WARPED_TEMP_PATH/$WIN"
fi fi
rmdir $WARPED_TEMP_PATH rmdir "$WARPED_TEMP_PATH"
} }
# actually create binaries and archives for all targets # actually create binaries and archives for all targets