|
1 | 1 | #!/usr/bin/env bash |
| 2 | + |
| 3 | +# This is a wrapper script, that automatically download mill via coursier |
| 4 | +# You can give the required mill version with --mill-version parameter |
| 5 | +# If no version is given, it falls back to the value of DEFAULT_MILL_VERSION |
| 6 | +# |
| 7 | +# Project page: https://github.com/coursier/millw |
| 8 | +# Original project page: https://github.com/lefou/millw |
| 9 | +# Script Version: 0.4.0-cs |
| 10 | +# |
| 11 | +# If you want to improve this script, please also contribute your changes back! |
| 12 | +# |
| 13 | +# Licensed under the Apache License, Version 2.0 |
| 14 | + |
| 15 | + |
| 16 | +DEFAULT_MILL_VERSION=0.9.10 |
| 17 | + |
2 | 18 | set -e |
3 | 19 |
|
4 | | -# This script ensures: |
5 | | -# - that we're using the right JVM (currently, temurin:17) |
6 | | -# - that we run the mill launcher script with bash rather than sh, |
7 | | -# as the latter has issues with '+' characters, that can appear |
8 | | -# in the JVM entry put in PATH by 'cs'. |
| 20 | +MILL_REPO_URL="https://github.com/com-lihaoyi/mill" |
| 21 | + |
| 22 | +if [ "x${CURL_CMD}" = "x" ] ; then |
| 23 | + CURL_CMD=curl |
| 24 | +fi |
| 25 | + |
| 26 | +# Explicit commandline argument takes precedence over all other methods |
| 27 | +if [ "x$1" = "x--mill-version" ] ; then |
| 28 | + shift |
| 29 | + if [ "x$1" != "x" ] ; then |
| 30 | + MILL_VERSION="$1" |
| 31 | + shift |
| 32 | + else |
| 33 | + echo "You specified --mill-version without a version." 1>&2 |
| 34 | + echo "Please provide a version that matches one provided on" 1>&2 |
| 35 | + echo "${MILL_REPO_URL}/releases" 1>&2 |
| 36 | + false |
| 37 | + fi |
| 38 | +fi |
| 39 | + |
| 40 | +# Please note, that if a MILL_VERSION is already set in the environment, |
| 41 | +# We reuse it's value and skip searching for a value. |
| 42 | + |
| 43 | +# If not already set, read .mill-version file |
| 44 | +if [ "x${MILL_VERSION}" = "x" ] ; then |
| 45 | + if [ -f ".mill-version" ] ; then |
| 46 | + MILL_VERSION="$(head -n 1 .mill-version 2> /dev/null)" |
| 47 | + fi |
| 48 | +fi |
| 49 | + |
| 50 | +if [ "x${XDG_CACHE_HOME}" != "x" ] ; then |
| 51 | + MILL_DOWNLOAD_PATH="${XDG_CACHE_HOME}/mill/download" |
| 52 | +else |
| 53 | + MILL_DOWNLOAD_PATH="${HOME}/.cache/mill/download" |
| 54 | +fi |
| 55 | + |
| 56 | +# If not already set, try to fetch newest from Github |
| 57 | +if [ "x${MILL_VERSION}" = "x" ] ; then |
| 58 | + # TODO: try to load latest version from release page |
| 59 | + echo "No mill version specified." 1>&2 |
| 60 | + echo "You should provide a version via '.mill-version' file or --mill-version option." 1>&2 |
| 61 | + |
| 62 | + mkdir -p "${MILL_DOWNLOAD_PATH}" |
| 63 | + LANG=C touch -d '1 hour ago' "${MILL_DOWNLOAD_PATH}/.expire_latest" 2>/dev/null || ( |
| 64 | + # we might be on OSX or BSD which don't have -d option for touch |
| 65 | + # but probably a -A [-][[hh]mm]SS |
| 66 | + touch "${MILL_DOWNLOAD_PATH}/.expire_latest"; touch -A -010000 "${MILL_DOWNLOAD_PATH}/.expire_latest" |
| 67 | + ) || ( |
| 68 | + # in case we still failed, we retry the first touch command with the intention |
| 69 | + # to show the (previously suppressed) error message |
| 70 | + LANG=C touch -d '1 hour ago' "${MILL_DOWNLOAD_PATH}/.expire_latest" |
| 71 | + ) |
| 72 | + |
| 73 | + if [ "${MILL_DOWNLOAD_PATH}/.latest" -nt "${MILL_DOWNLOAD_PATH}/.expire_latest" ] ; then |
| 74 | + # we know a current latest version |
| 75 | + MILL_VERSION="$(head -n 1 ${MILL_DOWNLOAD_PATH}/.latest 2> /dev/null)" |
| 76 | + fi |
| 77 | + |
| 78 | + if [ "x${MILL_VERSION}" = "x" ] ; then |
| 79 | + # we don't know a current latest version |
| 80 | + echo "Retrieving latest mill version ..." 1>&2 |
| 81 | + LANG=C ${CURL_CMD} -s -i -f -I ${MILL_REPO_URL}/releases/latest 2> /dev/null | grep --ignore-case Location: | sed s'/^.*tag\///' | tr -d '\r\n' > "${MILL_DOWNLOAD_PATH}/.latest" |
| 82 | + MILL_VERSION="$(head -n 1 ${MILL_DOWNLOAD_PATH}/.latest 2> /dev/null)" |
| 83 | + fi |
| 84 | + |
| 85 | + if [ "x${MILL_VERSION}" = "x" ] ; then |
| 86 | + # Last resort |
| 87 | + MILL_VERSION="${DEFAULT_MILL_VERSION}" |
| 88 | + echo "Falling back to hardcoded mill version ${MILL_VERSION}" 1>&2 |
| 89 | + else |
| 90 | + echo "Using mill version ${MILL_VERSION}" 1>&2 |
| 91 | + fi |
| 92 | +fi |
| 93 | + |
| 94 | +unset MILL_DOWNLOAD_PATH |
| 95 | +unset MILL_OLD_DOWNLOAD_PATH |
| 96 | +unset OLD_MILL |
| 97 | +unset MILL_VERSION_TAG |
| 98 | +unset MILL_REPO_URL |
| 99 | + |
| 100 | +if [ "x$1" == "x-i" -o "x$1" == "x--interactive" ]; then |
| 101 | + MILL_APP_NAME="mill-interactive" |
| 102 | +else |
| 103 | + MILL_APP_NAME="mill" |
| 104 | +fi |
| 105 | + |
| 106 | +mill_cs_opts=() |
| 107 | + |
| 108 | +# Adapted from Mill 0.10.0-M5 assembly |
| 109 | +init_mill_jvm_opts() { |
| 110 | + if [ -z $MILL_JVM_OPTS_PATH ] ; then |
| 111 | + mill_jvm_opts_file=".mill-jvm-opts" |
| 112 | + else |
| 113 | + mill_jvm_opts_file=$MILL_JVM_OPTS_PATH |
| 114 | + fi |
| 115 | + |
| 116 | + if [ -f "$mill_jvm_opts_file" ] ; then |
| 117 | + while IFS= read line |
| 118 | + do |
| 119 | + case $line in |
| 120 | + "-X"*) mill_cs_opts=("${mill_cs_opts[@]}" "--java-opt" "$line") |
| 121 | + esac |
| 122 | + done <"$mill_jvm_opts_file" |
| 123 | + fi |
| 124 | +} |
| 125 | + |
| 126 | +init_mill_cs_opts() { |
| 127 | + if [ -z $MILL_CS_OPTS_PATH ] ; then |
| 128 | + mill_cs_opts_file=".mill-cs-opts" |
| 129 | + else |
| 130 | + mill_cs_opts_file=$MILL_CS_OPTS_PATH |
| 131 | + fi |
| 132 | + |
| 133 | + if [ -f "$mill_cs_opts_file" ] ; then |
| 134 | + while IFS= read line |
| 135 | + do |
| 136 | + case $line in |
| 137 | + "#"*) ;; |
| 138 | + *) mill_cs_opts=("${mill_cs_opts[@]}" "$line") ;; |
| 139 | + esac |
| 140 | + done <"$mill_cs_opts_file" |
| 141 | + fi |
| 142 | +} |
9 | 143 |
|
10 | | -DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)" |
11 | 144 |
|
12 | | -if ! command -v cs >/dev/null; then |
13 | | - cat 1>&2 << EOF |
14 | | -cs command not found. |
| 145 | +# Adapted from |
15 | 146 |
|
16 | | -Please download a cs launcher from |
17 | | - https://github.com/coursier/coursier/releases/tag/v2.1.0-M2 |
18 | | -and install it as 'cs' in your PATH. |
19 | | -EOF |
20 | | - exit 1 |
| 147 | +coursier_version="2.1.0-M2" |
| 148 | + |
| 149 | +# https://stackoverflow.com/questions/3466166/how-to-check-if-running-in-cygwin-mac-or-linux/17072017#17072017 |
| 150 | +if [ "$(expr substr $(uname -s) 1 5 2>/dev/null)" == "Linux" ]; then |
| 151 | + cs_url="https://github.com/coursier/coursier/releases/download/v$coursier_version/cs-x86_64-pc-linux.gz" |
| 152 | + cache_base="$HOME/.cache/coursier/v1" |
| 153 | +elif [ "$(uname)" == "Darwin" ]; then |
| 154 | + cs_url="https://github.com/coursier/coursier/releases/download/v$coursier_version/cs-x86_64-apple-darwin.gz" |
| 155 | + cache_base="$HOME/Library/Caches/Coursier/v1" |
| 156 | +else |
| 157 | + # assuming Windows… |
| 158 | + cs_url="https://github.com/coursier/coursier/releases/download/v$coursier_version/cs-x86_64-pc-win32.zip" |
| 159 | + cache_base="$LOCALAPPDATA/Coursier/v1" # TODO Check that |
| 160 | + ext=".exe" |
| 161 | + do_chmod="0" |
| 162 | +fi |
| 163 | + |
| 164 | +cache_dest="$cache_base/$(echo "$cs_url" | sed 's@://@/@')" |
| 165 | + |
| 166 | +if [ ! -f "$cache_dest" ]; then |
| 167 | + mkdir -p "$(dirname "$cache_dest")" |
| 168 | + tmp_dest="$cache_dest.tmp-setup" |
| 169 | + echo "Downloading $cs_url" |
| 170 | + curl -fLo "$tmp_dest" "$cs_url" |
| 171 | + mv "$tmp_dest" "$cache_dest" |
| 172 | +fi |
| 173 | + |
| 174 | +if [[ "$cache_dest" == *.gz ]]; then |
| 175 | + cs="$(dirname "$cache_dest")/$(basename "$cache_dest" .gz)" |
| 176 | + if [ ! -f "$cs" ]; then |
| 177 | + gzip -d < "$cache_dest" > "$cs" |
| 178 | + fi |
| 179 | + if [ ! -x "$cs" ]; then |
| 180 | + chmod +x "$cs" |
| 181 | + fi |
| 182 | +elif [[ "$cache_dest" == *.zip ]]; then |
| 183 | + cs="$(dirname "$cache_dest")/$(basename "$cache_dest" .zip).exe" |
| 184 | + if [ ! -f "$cs" ]; then |
| 185 | + unzip -p "$cache_dest" "$(basename "$cache_dest" .zip).exe" > "$cs" |
| 186 | + fi |
21 | 187 | fi |
22 | 188 |
|
23 | 189 | eval "$(cs java --env --jvm temurin:17 || cs java --env --jvm openjdk:1.17.0)" |
| 190 | +export PATH=".:$PATH" |
| 191 | + |
| 192 | + |
| 193 | + |
| 194 | +init_mill_jvm_opts |
| 195 | +init_mill_cs_opts |
24 | 196 |
|
25 | | -exec /usr/bin/env bash "$DIR/millw" "$@" |
| 197 | +exec "$cs" launch "$MILL_APP_NAME:$MILL_VERSION" "${mill_cs_opts[@]}" -- "$@" |
0 commit comments