Skip to content

Instantly share code, notes, and snippets.

@loganlinn
Last active December 10, 2021 09:44
Show Gist options
  • Save loganlinn/f966a37df79db52716c6d541c2da7aa4 to your computer and use it in GitHub Desktop.
Save loganlinn/f966a37df79db52716c6d541c2da7aa4 to your computer and use it in GitHub Desktop.
Wrapper script for Polylith
#!/usr/bin/env bash
# Ensure this file is executable via `chmod a+x poly`, then place it
# somewhere on your $PATH, like ~/bin. The rest of Polylith will be
# installed upon first run into the ~/.polylith/self-installs directory.
export POLY_VERSION="0.2.13-alpha"
export POLY_CHECKSUM="88b37ddaabab73cf83d29b7b1f06a0f93146a1e165f43bc4792ebbb0a9a6a122"
export POLY_RELEASE="v$POLY_VERSION"
export POLY_JARNAME="poly-$POLY_VERSION.jar"
if [[ -n $CLASSPATH ]]; then
cat <<-'EOS' 1>&2
WARNING: You have $CLASSPATH set, probably by accident.
It is strongly recommended to unset this before proceeding.
EOS
fi
cygwin=false
cygterm=false
msys=false
delimiter=:
case "$OSTYPE" in
cygwin)
cygwin=true
delimiter=';'
case "$TERM" in
rxvt* | xterm* | vt*) cygterm=true ;;
esac
;;
msys)
msys=true
delimiter=';'
;;
esac
readonly cygwin
readonly cygterm
readonly msys
readonly delimiter
function msg {
echo "$@" 1>&2
}
function make_native_path {
# ensure we have native paths
if $cygwin && [[ $1 = /* ]]; then
echo -n "$(cygpath -wp "$1")"
elif $msys && [[ $1 = /?/* ]]; then
echo -n "$(sh -c "(cd $1 2</dev/null && pwd -W) || echo $1 | sed 's/^\\/\([a-z]\)/\\1:/g'")"
else
echo -n "$1"
fi
}
# usage : add_path PATH_VAR [PATH]...
function add_path {
local path_var="$1"
shift
while [ -n "$1" ]; do
# http://bashify.com/?Useful_Techniques:Indirect_Variables:Indirect_Assignment
if [[ -z ${!path_var} ]]; then
# shellcheck disable=2086
export ${path_var}="$(make_native_path "$1")"
else
# shellcheck disable=2086
export ${path_var}="${!path_var}${delimiter}$(make_native_path "$1")"
fi
shift
done
}
function download_failed_message {
cat <<-EOS 1>&2
Failed to download $1 (exit code $2)
It's possible your HTTP client's certificate store does not have the
correct certificate authority needed. This is often caused by an
out-of-date version of libssl. It's also possible that you're behind a
firewall and haven't set HTTP_PROXY and HTTPS_PROXY.
EOS
}
function checksum_failed_message {
cat <<-EOS 1>&2
Failed to properly download $1
The checksum was mismatched. and we could not verify the downloaded
file. We expected a sha256 of
$2 and actually had
$3.
We used '$SHASUM_CMD' to verify the downloaded file.
EOS
}
function self_install {
if [ -r "$POLY_JAR" ]; then
cat <<-EOS 1>&2
The self-install jar already exists at $POLY_JAR.
If you wish to re-download, delete it and rerun "$0 self-install".
EOS
exit 1
fi
msg "Downloading Polylith to $POLY_JAR now..."
mkdir -p "$(dirname "$POLY_JAR")"
POLY_URL="https://github.com/polyfy/polylith/releases/download/$POLY_RELEASE/$POLY_JARNAME"
if $HTTP_CLIENT "$POLY_JAR.pending" "$POLY_URL"; then
printf "%s %s.pending\n" "$POLY_CHECKSUM" "$POLY_JAR" >"$POLY_JAR.pending.shasum"
if $SHASUM_CMD -c "$POLY_JAR.pending.shasum"; then
mv -f "$POLY_JAR.pending" "$POLY_JAR"
else
got_sum="$($SHASUM_CMD "$POLY_JAR.pending" | cut -f 1 -d ' ')"
checksum_failed_message "$POLY_URL" "$POLY_CHECKSUM" "$got_sum"
rm "$POLY_JAR.pending" 2>/dev/null
exit 1
fi
else
local exit_code=$?
rm "$POLY_JAR.pending" 2>/dev/null
download_failed_message "$POLY_URL" "$exit_code"
exit 1
fi
}
ORIGINAL_PWD="$PWD"
while [[ ! -r workspace.edn ]] && [[ $PWD != / ]]; do
cd ..
if [[ $(dirname "$PWD") = / ]]; then
cd "$ORIGINAL_PWD" || exit 1
if [[ -n $DEBUG ]]; then
msg "$PWD: workspace.edn not found.."
fi
break
fi
done
export POLY_HOME="${POLY_HOME:-"$HOME/.polylith"}"
for f in "/etc/polyrc" "$POLY_HOME/polyrc" ".polyrc"; do
if [ -e "$f" ]; then
# shellcheck disable=1090
source "$f"
fi
done
if $cygwin; then
POLY_HOME=$(cygpath -w "$POLY_HOME")
export POLY_HOME
fi
POLY_JAR="${POLY_JAR:-${POLY_HOME}/self-installs/$POLY_JARNAME}"
# normalize $0 on certain BSDs
if [[ $(dirname "$0") = . ]]; then
SCRIPT="$(which "$(basename "$0")")"
if [[ -z $SCRIPT ]]; then
SCRIPT="$0"
fi
else
SCRIPT="$0"
fi
# resolve symlinks to the script itself portably
while [ -h "$SCRIPT" ]; do
ls=$(ls -ld "$SCRIPT")
link=$(expr "$ls" : '.*-> \(.*\)$')
if expr "$link" : '/.*' >/dev/null; then
SCRIPT="$link"
else
SCRIPT="$(dirname "$SCRIPT"$)/$link"
fi
done
# shellcheck disable=2034
BIN_DIR="$(dirname "$SCRIPT")"
BIN_NAME=$(basename "$SCRIPT")
export POLY_JVM_OPTS="${POLY_JVM_OPTS-"-XX:+TieredCompilation -XX:TieredStopAtLevel=1"}"
# This needs to be defined before we call HTTP_CLIENT below
if [[ -z ${HTTP_CLIENT-} ]]; then
if type -p curl >/dev/null 2>&1; then
if [[ -n ${https_proxy-} ]]; then
CURL_PROXY="-x $https_proxy"
fi
HTTP_CLIENT="curl $CURL_PROXY -f -L -o"
else
HTTP_CLIENT="wget -O"
fi
fi
# This needs to be defined before we call SHASUM_CMD below
if [[ -z ${SHASUM_CMD-} ]]; then
if type -p sha256sum >/dev/null 2>&1; then
export SHASUM_CMD="sha256sum"
elif type -p shasum >/dev/null 2>&1; then
export SHASUM_CMD="shasum --algorithm 256"
elif type -p sha256 >/dev/null 2>&1; then
export SHASUM_CMD="sha256 -q"
else
msg "$BIN_NAME couldn't find sha256sum, shasum, or sha256 in your \$PATH, one of which is required."
exit 1
fi
fi
# TODO support running from source checkout
add_path CLASSPATH "$POLY_JAR"
# if [[ $POLY_USE_BOOTCLASSPATH != no ]]; then
# POLY_JVM_OPTS="-Xbootclasspath/a:$POLY_JAR $POLY_JVM_OPTS"
# fi
if [[ ! -r "$POLY_JAR" ]] && [[ $1 != self-install ]]; then
self_install
fi
if [[ ! -x $JAVA_CMD ]] && ! type -f java >/dev/null; then
msg "$BIN_NAME couldn't find 'java' executable, which is required."
msg "Please either set JAVA_CMD or put java (>=1.6) in your \$PATH."
exit 1
fi
export POLY_JAVA_CMD="${POLY_JAVA_CMD:-${JAVA_CMD:-java}}"
# Support $JAVA_OPTS for backwards-compatibility.
export JVM_OPTS="${JVM_OPTS:-"$JAVA_OPTS"}"
# Handle jline issue with cygwin not propagating OSTYPE through java subprocesses: https://github.com/jline/jline2/issues/62
if $cygterm; then
POLY_JVM_OPTS="$POLY_JVM_OPTS -Djline.terminal=jline.UnixTerminal"
stty -icanon min 1 -echo >/dev/null 2>&1
fi
case "$1" in
self-install)
self_install
;;
upgrade | downgrade)
if [[ -n $POLY_DIR ]]; then
msg "The upgrade task is not meant to be run from a checkout."
exit 1
elif [[ ! -w $SCRIPT ]]; then
msg "You do not have permission to upgrade the installation in $SCRIPT"
exit 1
else
echo "The script at $SCRIPT will be upgraded to the latest version."
echo -n "Do you want to continue [Y/n]? "
read -r RESP
case "$RESP" in
y | Y | "")
msg
msg "Upgrading..."
if $cygwin; then
TARGET=$(cygpath -w "$TARGET")
else
TARGET="/tmp/poly-${$}-upgrade"
fi
POLY_SCRIPT_URL="https://gist.github.com/loganlinn/f966a37df79db52716c6d541c2da7aa4/raw/poly"
if $HTTP_CLIENT "$TARGET" "$POLY_SCRIPT_URL"; then
if cmp -s "$TARGET" "$SCRIPT"; then
msg "$SCRIPT is already up-to-date."
fi
mv "$TARGET" "$SCRIPT" && chmod +x "$SCRIPT"
unset CLASSPATH
exec "$SCRIPT" version
else
download_failed_message "$POLY_SCRIPT_URL"
fi
;;
*)
msg "Aborted."
exit 1
;;
esac
fi
;;
*)
if $cygwin; then
# When running on Cygwin, use Windows-style paths for java
ORIGINAL_PWD=$(cygpath -w "$ORIGINAL_PWD")
fi
# apply context specific CLASSPATH entries
if [ -f .poly-classpath ]; then
add_path CLASSPATH "$(cat .poly-classpath)"
fi
if [[ -n $DEBUG ]]; then
msg "$BIN_NAME: POLY_JAVA_CMD=$POLY_JAVA_CMD"
msg "$BIN_NAME: POLY_JVM_OPTS=$POLY_JVM_OPTS"
msg "$BIN_NAME: CLASSPATH=$CLASSPATH"
fi
# shellcheck disable=2086
exec "$POLY_JAVA_CMD" \
-Dfile.encoding=UTF-8 \
-Dmaven.wagon.http.ssl.easy=false \
-Dmaven.wagon.rto=10000 \
$POLY_JVM_OPTS \
-classpath "$CLASSPATH" \
-jar "$POLY_JAR" "$@"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment