Skip to content

Instantly share code, notes, and snippets.

@Techcable
Last active September 1, 2016 21:01
Show Gist options
  • Select an option

  • Save Techcable/8ed2f97ee101a87a34bb766146e5209b to your computer and use it in GitHub Desktop.

Select an option

Save Techcable/8ed2f97ee101a87a34bb766146e5209b to your computer and use it in GitHub Desktop.
A bash wrapper for kotlin script (similar to gradlew), which will locate the kotlin compiler (downloading if needed), and execute your script
#!/bin/sh
KOTLIN_VERSION="1.0.3"
KOTLIN_COMPILER_URL="https://github.com/JetBrains/kotlin/releases/download/v${KOTLIN_VERSION}/kotlin-compiler-$KOTLIN_VERSION.zip"
KOTLIN_COMPILER_HASH="37615f1d63e8500cd33c7f3e60b715263f65189d6d8f25defba78968c896dc97" # The hash of the compiler's zip-file
KOTLIN_INSTALATION_DIR="$HOME/.kotlin/$KOTLIN_VERSION"
if [ $# -lt 1 ]; then
echo "Insufficient arguments: $#";
exit 1;
fi;
SCRIPT_FILE=$1
if [ ! -f "$SCRIPT_FILE" ]; then
echo "Unable to locate file $SCRIPT_FILE"
exit 1;
fi;
debug() {
if [ "$DEBUG" ]; then
echo "$1"
fi;
}
require_command() {
if ! which "$1" >/dev/null 2>&1; then
echo "Couldn't find the '$1' command"
errMsg=$2
if [ -z "$errMsg" ]; then
errMsg="Unable to execute $SCRIPT_FILE"
fi;
echo "$errMsg"
exit 1;
fi;
}
verifyHash() {
if [ ! -f "$1" ]; then
echo "Unable to find file '$1' to verify hash"
exit 1;
fi;
name=$(basename "$1")
require_command sha256sum "Unable to verify hash: $name"
hash=$(sha256sum "$1" | sed -r 's/(\w+).*/\1/')
if [ "$hash" != "$2" ]; then
echo "The actual hash of $name didn't match the expected value!!"
echo "Expected '$2' but got '$hash'"
exit 1;
else
echo "Verified sha256 hash of $name is $2";
fi;
}
require_command "java"
download_kotlin() {
echo "Downloading kotlin $KOTLIN_VERSION to $KOTLIN_INSTALATION_DIR"
require_command "wget" "Unable to download the kotlin compiler"
require_command "unzip" "Unable to download the kotlin compiler"
if ! mkdir -p "$KOTLIN_INSTALATION_DIR"; then
echo "Unable to create directory $KOTLIN_INSTALATION_DIR"
exit 1;
fi;
KOTLIN_ZIP_FILE="$KOTLIN_INSTALATION_DIR/kotlin-compiler-$KOTLIN_VERSION.zip"
if [ -f "$KOTLIN_ZIP_FILE" ]; then
echo "Using existing zip file: $KOTLIN_ZIP_FILE"
echo "Delete manually if this is unintended"
else
echo "Downloading $KOTLIN_COMPILER_URL to $KOTLIN_ZIP_FILE"
wget -O "$KOTLIN_ZIP_FILE" "$KOTLIN_COMPILER_URL"
if [ ! -f "$KOTLIN_ZIP_FILE" ]; then
echo "Unable to download $KOTLIN_COMPILER_URL to $KOTLIN_ZIP_FILE"
exit 1;
fi;
fi;
verifyHash "$KOTLIN_ZIP_FILE" "$KOTLIN_COMPILER_HASH"
echo "Extracting" "$(basename "$KOTLIN_ZIP_FILE")" to "${KOTLIN_INSTALATION_DIR}"/kotlinc
old_dir=$(pwd)
cd "$KOTLIN_INSTALATION_DIR" || exit 1
unzip "$KOTLIN_ZIP_FILE"
cd "$old_dir" || exit 1
if [ ! -d "${KOTLIN_INSTALATION_DIR}/kotlinc/bin" ]; then
echo "Unable to extract $KOTLIN_ZIP_FILE to ${KOTLIN_INSTALATION_DIR}/kotlinc";
exit 1;
fi;
echo "Successfully downloaded, verified, and extracted kotlin compiler to ${KOTLIN_INSTALATION_DIR}/kotlinc"
}
KOTLIN_COMPILER=$(which kotlinc)
if [ -f "$KOTLIN_COMPILER" ]; then
debug "Kotlin compiler successfully located on PATH at $KOTLIN_COMPILER"
else
# Resort to auto-install
KOTLIN_COMPILER="$KOTLIN_INSTALATION_DIR/kotlinc/bin/kotlinc"
if [ ! -f "$KOTLIN_COMPILER" ]; then
download_kotlin || exit 1
if [ ! -f "$KOTLIN_COMPILER" ]; then
echo "Looks like all these redundant checks paid off"
echo "See XKCD panic sort: https://xkcd.com/1185/"
exit 1;
fi;
else
debug "Found existing auto-instalation at $KOTLIN_COMPILER"
fi;
fi;
$KOTLIN_COMPILER "-script" "$SCRIPT_FILE"
if [ $? != 1 ]; then
echo "Unable to execute $SCRIPT_FILE with $KOTLIN_COMPILER"
exit 1;
fi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment