-
Notifications
You must be signed in to change notification settings - Fork 27
/
kotlinw
executable file
·68 lines (59 loc) · 2.66 KB
/
kotlinw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/sh
set -e
ROOT_DIR="$(dirname "$0")"
JDK_VERSION="$(grep -s 'java-gradle' "$ROOT_DIR/gradle/libs.versions.toml" | cut -d'"' -f2)"
if [ -z "$JDK_VERSION" ]; then
JDK_VERSION="22"
fi
KOTLIN_VERSION="$(grep -s -m 1 '^kotlin \?=' "$ROOT_DIR/gradle/libs.versions.toml" | cut -d'"' -f2)"
if [ -f "$ROOT_DIR/gradle/libs.versions.toml" ]; then
KOTLIN_VERSION="2.0.0"
fi
# OS specific support (must be 'true' or 'false').
case "$(uname -s)" in
Linux*) MACHINE=Linux;;
Darwin*) MACHINE=Mac;;
*) MACHINE="UNSUPPORTED"
esac
if [ "$MACHINE" = "Linux" ]; then
export JAVA_HOME="/usr/lib/jvm/zulu-$JDK_VERSION-amd64/"
if [ ! -d "$JAVA_HOME" ]; then
export JAVA_HOME="/usr/lib/jvm/zulu$JDK_VERSION-ca-amd64/"
if [ ! -d "$JAVA_HOME" ]; then
echo "Installing JDK ${JDK_VERSION} (you may be prompted for your password)..."
curl -s https://repos.azul.com/azul-repo.key | sudo gpg --batch --yes --dearmor -o /usr/share/keyrings/azul.gpg
echo "deb [signed-by=/usr/share/keyrings/azul.gpg] https://repos.azul.com/zulu/deb stable main" | sudo tee /etc/apt/sources.list.d/zulu.list
sudo apt update
sudo apt install -y "zulu$JDK_VERSION-jdk"
fi
fi
elif [ "$MACHINE" = "Mac" ]; then
export JAVA_HOME="/Library/Java/JavaVirtualMachines/zulu-$JDK_VERSION.jdk/Contents/Home"
if [ ! -d "$JAVA_HOME" ]; then
echo "Installing JDK ${JDK_VERSION} (you may be prompted for your password)..."
brew tap mdogan/zulu
brew install --cask "zulu-jdk${JDK_VERSION}"
fi
else
echo "Only macOS and Linux are supported."
exit 1
fi
INSTALLATION_DIR="${HOME}/.kotlinw/${KOTLIN_VERSION}"
BINARY_DIR="${INSTALLATION_DIR}/kotlinc/bin"
if [ ! -f "${BINARY_DIR}/kotlin" ]; then
echo "Downloading Kotlin ${KOTLIN_VERSION}"
mkdir -p "${INSTALLATION_DIR}"
temp_file=$(mktemp /tmp/kotlin.zip.XXXXXX)
curl -sLo "${temp_file}" "https://github.com/JetBrains/kotlin/releases/download/v${KOTLIN_VERSION}/kotlin-compiler-${KOTLIN_VERSION}.zip"
unzip -q "${temp_file}" -d "${INSTALLATION_DIR}"
rm -f "${temp_file}"
fi
# this works around an issue where the Kotlin compiler used by ktlint accesses code that JDK 12+ don't allow access to
export JAVA_OPTS="--add-opens java.base/java.lang=ALL-UNNAMED"
SCRIPT_FILE="$1"
# will remove the first element of the $@ arguments array since we read it already above
shift
# uses kotlinc instead of kotlin because the latter doesn't allow specifying a jvm target and defaults to Java 8
# the -- between SCRIPT_FILE and the other arguments is there so that the arguments are treated as arguments to the
# script and not to kotlinc
"${BINARY_DIR}/kotlinc" "-jvm-target" "$JDK_VERSION" "-script" "$SCRIPT_FILE" "--" "${@}"