Skip to content

Commit 9a504dd

Browse files
committed
bump mill launcher
1 parent 2044711 commit 9a504dd

File tree

1 file changed

+296
-34
lines changed

1 file changed

+296
-34
lines changed

mill

Lines changed: 296 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,329 @@
11
#!/usr/bin/env sh
22

3-
# This is a wrapper script, that automatically download mill from GitHub release pages
4-
# You can give the required mill version with MILL_VERSION env variable
5-
# If no version is given, it falls back to the value of DEFAULT_MILL_VERSION
3+
# This is a wrapper script, that automatically selects or downloads Mill from Maven Central or GitHub release pages.
4+
#
5+
# This script determines the Mill version to use by trying these sources
6+
# - env-variable `MILL_VERSION`
7+
# - local file `.mill-version`
8+
# - local file `.config/mill-version`
9+
# - `mill-version` from YAML fronmatter of current buildfile
10+
# - if accessible, find the latest stable version available on Maven Central (https://repo1.maven.org/maven2)
11+
# - env-variable `DEFAULT_MILL_VERSION`
12+
#
13+
# If a version has the suffix '-native' a native binary will be used.
14+
# If a version has the suffix '-jvm' an executable jar file will be used, requiring an already installed Java runtime.
15+
# If no such suffix is found, the script will pick a default based on version and platform.
16+
#
17+
# Once a version was determined, it tries to use either
18+
# - a system-installed mill, if found and it's version matches
19+
# - an already downloaded version under ~/.cache/mill/download
20+
#
21+
# If no working mill version was found on the system,
22+
# this script downloads a binary file from Maven Central or Github Pages (this is version dependent)
23+
# into a cache location (~/.cache/mill/download).
24+
#
25+
# Mill Project URL: https://github.com/com-lihaoyi/mill
26+
# Script Version: 1.0.0-M1-21-7b6fae-DIRTY892b63e8
27+
#
28+
# If you want to improve this script, please also contribute your changes back!
29+
# This script was generated from: dist/scripts/src/mill.sh
30+
#
31+
# Licensed under the Apache License, Version 2.0
632

733
set -e
834

35+
if [ "$1" = "--setup-completions" ] ; then
36+
# Need to preserve the first position of those listed options
37+
MILL_FIRST_ARG=$1
38+
shift
39+
fi
40+
941
if [ -z "${DEFAULT_MILL_VERSION}" ] ; then
10-
DEFAULT_MILL_VERSION=0.11.12
42+
DEFAULT_MILL_VERSION="0.12.10"
43+
fi
44+
45+
46+
if [ -z "${GITHUB_RELEASE_CDN}" ] ; then
47+
GITHUB_RELEASE_CDN=""
48+
fi
49+
50+
51+
MILL_REPO_URL="https://github.com/com-lihaoyi/mill"
52+
53+
if [ -z "${CURL_CMD}" ] ; then
54+
CURL_CMD=curl
55+
fi
56+
57+
# Explicit commandline argument takes precedence over all other methods
58+
if [ "$1" = "--mill-version" ] ; then
59+
echo "The --mill-version option is no longer supported." 1>&2
60+
fi
61+
62+
MILL_BUILD_SCRIPT=""
63+
64+
if [ -f "build.mill" ] ; then
65+
MILL_BUILD_SCRIPT="build.mill"
1166
fi
1267

13-
if [ -z "$MILL_VERSION" ] ; then
68+
# Please note, that if a MILL_VERSION is already set in the environment,
69+
# We reuse it's value and skip searching for a value.
70+
71+
# If not already set, read .mill-version file
72+
if [ -z "${MILL_VERSION}" ] ; then
1473
if [ -f ".mill-version" ] ; then
15-
MILL_VERSION="$(head -n 1 .mill-version 2> /dev/null)"
74+
MILL_VERSION="$(tr '\r' '\n' < .mill-version | head -n 1 2> /dev/null)"
1675
elif [ -f ".config/mill-version" ] ; then
17-
MILL_VERSION="$(head -n 1 .config/mill-version 2> /dev/null)"
18-
elif [ -f "mill" ] && [ "$0" != "mill" ] ; then
19-
MILL_VERSION=$(grep -F "DEFAULT_MILL_VERSION=" "mill" | head -n 1 | cut -d= -f2)
20-
else
21-
MILL_VERSION=$DEFAULT_MILL_VERSION
76+
MILL_VERSION="$(tr '\r' '\n' < .config/mill-version | head -n 1 2> /dev/null)"
77+
elif [ -n "${MILL_BUILD_SCRIPT}" ] ; then
78+
MILL_VERSION="$(cat ${MILL_BUILD_SCRIPT} | grep '//[|] *mill-version: *' | sed 's;//| *mill-version: *;;')"
2279
fi
2380
fi
2481

25-
if [ "x${XDG_CACHE_HOME}" != "x" ] ; then
26-
MILL_DOWNLOAD_PATH="${XDG_CACHE_HOME}/mill/download"
27-
else
28-
MILL_DOWNLOAD_PATH="${HOME}/.cache/mill/download"
82+
MILL_USER_CACHE_DIR="${XDG_CACHE_HOME:-${HOME}/.cache}/mill"
83+
84+
if [ -z "${MILL_DOWNLOAD_PATH}" ] ; then
85+
MILL_DOWNLOAD_PATH="${MILL_USER_CACHE_DIR}/download"
86+
fi
87+
88+
# If not already set, try to fetch newest from Github
89+
if [ -z "${MILL_VERSION}" ] ; then
90+
# TODO: try to load latest version from release page
91+
echo "No mill version specified." 1>&2
92+
echo "You should provide a version via a '//| mill-version: ' comment or a '.mill-version' file." 1>&2
93+
94+
mkdir -p "${MILL_DOWNLOAD_PATH}"
95+
LANG=C touch -d '1 hour ago' "${MILL_DOWNLOAD_PATH}/.expire_latest" 2>/dev/null || (
96+
# we might be on OSX or BSD which don't have -d option for touch
97+
# but probably a -A [-][[hh]mm]SS
98+
touch "${MILL_DOWNLOAD_PATH}/.expire_latest"; touch -A -010000 "${MILL_DOWNLOAD_PATH}/.expire_latest"
99+
) || (
100+
# in case we still failed, we retry the first touch command with the intention
101+
# to show the (previously suppressed) error message
102+
LANG=C touch -d '1 hour ago' "${MILL_DOWNLOAD_PATH}/.expire_latest"
103+
)
104+
105+
# POSIX shell variant of bash's -nt operator, see https://unix.stackexchange.com/a/449744/6993
106+
# if [ "${MILL_DOWNLOAD_PATH}/.latest" -nt "${MILL_DOWNLOAD_PATH}/.expire_latest" ] ; then
107+
if [ -n "$(find -L "${MILL_DOWNLOAD_PATH}/.latest" -prune -newer "${MILL_DOWNLOAD_PATH}/.expire_latest")" ]; then
108+
# we know a current latest version
109+
MILL_VERSION=$(head -n 1 "${MILL_DOWNLOAD_PATH}"/.latest 2> /dev/null)
110+
fi
111+
112+
if [ -z "${MILL_VERSION}" ] ; then
113+
# we don't know a current latest version
114+
echo "Retrieving latest mill version ..." 1>&2
115+
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"
116+
MILL_VERSION=$(head -n 1 "${MILL_DOWNLOAD_PATH}"/.latest 2> /dev/null)
117+
fi
118+
119+
if [ -z "${MILL_VERSION}" ] ; then
120+
# Last resort
121+
MILL_VERSION="${DEFAULT_MILL_VERSION}"
122+
echo "Falling back to hardcoded mill version ${MILL_VERSION}" 1>&2
123+
else
124+
echo "Using mill version ${MILL_VERSION}" 1>&2
125+
fi
29126
fi
30-
MILL_EXEC_PATH="${MILL_DOWNLOAD_PATH}/${MILL_VERSION}"
31127

32-
version_remainder="$MILL_VERSION"
33-
MILL_MAJOR_VERSION="${version_remainder%%.*}"; version_remainder="${version_remainder#*.}"
34-
MILL_MINOR_VERSION="${version_remainder%%.*}"; version_remainder="${version_remainder#*.}"
128+
MILL_NATIVE_SUFFIX="-native"
129+
MILL_JVM_SUFFIX="-jvm"
130+
FULL_MILL_VERSION=$MILL_VERSION
131+
ARTIFACT_SUFFIX=""
132+
set_artifact_suffix(){
133+
if [ "$(expr substr $(uname -s) 1 5 2>/dev/null)" = "Linux" ]; then
134+
if [ "$(uname -m)" = "aarch64" ]; then
135+
ARTIFACT_SUFFIX="-native-linux-aarch64"
136+
else
137+
ARTIFACT_SUFFIX="-native-linux-amd64"
138+
fi
139+
elif [ "$(uname)" = "Darwin" ]; then
140+
if [ "$(uname -m)" = "arm64" ]; then
141+
ARTIFACT_SUFFIX="-native-mac-aarch64"
142+
else
143+
ARTIFACT_SUFFIX="-native-mac-amd64"
144+
fi
145+
else
146+
echo "This native mill launcher supports only Linux and macOS." 1>&2
147+
exit 1
148+
fi
149+
}
150+
151+
case "$MILL_VERSION" in
152+
*"$MILL_NATIVE_SUFFIX")
153+
MILL_VERSION=${MILL_VERSION%"$MILL_NATIVE_SUFFIX"}
154+
set_artifact_suffix
155+
;;
156+
157+
*"$MILL_JVM_SUFFIX")
158+
MILL_VERSION=${MILL_VERSION%"$MILL_JVM_SUFFIX"}
159+
;;
160+
161+
*)
162+
case "$MILL_VERSION" in
163+
0.1.*) ;;
164+
0.2.*) ;;
165+
0.3.*) ;;
166+
0.4.*) ;;
167+
0.5.*) ;;
168+
0.6.*) ;;
169+
0.7.*) ;;
170+
0.8.*) ;;
171+
0.9.*) ;;
172+
0.10.*) ;;
173+
0.11.*) ;;
174+
0.12.*) ;;
175+
*)
176+
set_artifact_suffix
177+
esac
178+
;;
179+
esac
180+
181+
MILL="${MILL_DOWNLOAD_PATH}/$MILL_VERSION$ARTIFACT_SUFFIX"
182+
183+
try_to_use_system_mill() {
184+
if [ "$(uname)" != "Linux" ]; then
185+
return 0
186+
fi
187+
188+
MILL_IN_PATH="$(command -v mill || true)"
189+
190+
if [ -z "${MILL_IN_PATH}" ]; then
191+
return 0
192+
fi
193+
194+
SYSTEM_MILL_FIRST_TWO_BYTES=$(head --bytes=2 "${MILL_IN_PATH}")
195+
if [ "${SYSTEM_MILL_FIRST_TWO_BYTES}" = "#!" ]; then
196+
# MILL_IN_PATH is (very likely) a shell script and not the mill
197+
# executable, ignore it.
198+
return 0
199+
fi
200+
201+
SYSTEM_MILL_PATH=$(readlink -e "${MILL_IN_PATH}")
202+
SYSTEM_MILL_SIZE=$(stat --format=%s "${SYSTEM_MILL_PATH}")
203+
SYSTEM_MILL_MTIME=$(stat --format=%y "${SYSTEM_MILL_PATH}")
204+
205+
if [ ! -d "${MILL_USER_CACHE_DIR}" ]; then
206+
mkdir -p "${MILL_USER_CACHE_DIR}"
207+
fi
208+
209+
SYSTEM_MILL_INFO_FILE="${MILL_USER_CACHE_DIR}/system-mill-info"
210+
if [ -f "${SYSTEM_MILL_INFO_FILE}" ]; then
211+
parseSystemMillInfo() {
212+
LINE_NUMBER="${1}"
213+
# Select the line number of the SYSTEM_MILL_INFO_FILE, cut the
214+
# variable definition in that line in two halves and return
215+
# the value, and finally remove the quotes.
216+
sed -n "${LINE_NUMBER}p" "${SYSTEM_MILL_INFO_FILE}" |\
217+
cut -d= -f2 |\
218+
sed 's/"\(.*\)"/\1/'
219+
}
220+
221+
CACHED_SYSTEM_MILL_PATH=$(parseSystemMillInfo 1)
222+
CACHED_SYSTEM_MILL_VERSION=$(parseSystemMillInfo 2)
223+
CACHED_SYSTEM_MILL_SIZE=$(parseSystemMillInfo 3)
224+
CACHED_SYSTEM_MILL_MTIME=$(parseSystemMillInfo 4)
225+
226+
if [ "${SYSTEM_MILL_PATH}" = "${CACHED_SYSTEM_MILL_PATH}" ] \
227+
&& [ "${SYSTEM_MILL_SIZE}" = "${CACHED_SYSTEM_MILL_SIZE}" ] \
228+
&& [ "${SYSTEM_MILL_MTIME}" = "${CACHED_SYSTEM_MILL_MTIME}" ]; then
229+
if [ "${CACHED_SYSTEM_MILL_VERSION}" = "${MILL_VERSION}" ]; then
230+
MILL="${SYSTEM_MILL_PATH}"
231+
return 0
232+
else
233+
return 0
234+
fi
235+
fi
236+
fi
237+
238+
SYSTEM_MILL_VERSION=$(${SYSTEM_MILL_PATH} --version | head -n1 | sed -n 's/^Mill.*version \(.*\)/\1/p')
35239

36-
if [ ! -s "$MILL_EXEC_PATH" ] ; then
37-
mkdir -p "$MILL_DOWNLOAD_PATH"
38-
if [ "$MILL_MAJOR_VERSION" -gt 0 ] || [ "$MILL_MINOR_VERSION" -ge 5 ] ; then
39-
ASSEMBLY="-assembly"
240+
cat <<EOF > "${SYSTEM_MILL_INFO_FILE}"
241+
CACHED_SYSTEM_MILL_PATH="${SYSTEM_MILL_PATH}"
242+
CACHED_SYSTEM_MILL_VERSION="${SYSTEM_MILL_VERSION}"
243+
CACHED_SYSTEM_MILL_SIZE="${SYSTEM_MILL_SIZE}"
244+
CACHED_SYSTEM_MILL_MTIME="${SYSTEM_MILL_MTIME}"
245+
EOF
246+
247+
if [ "${SYSTEM_MILL_VERSION}" = "${MILL_VERSION}" ]; then
248+
MILL="${SYSTEM_MILL_PATH}"
40249
fi
41-
DOWNLOAD_FILE=$MILL_EXEC_PATH-tmp-download
42-
MILL_VERSION_TAG=$(echo $MILL_VERSION | sed -E 's/([^-]+)(-M[0-9]+)?(-.*)?/\1\2/')
43-
MILL_DOWNLOAD_URL="https://repo1.maven.org/maven2/com/lihaoyi/mill-dist/$MILL_VERSION/mill-dist-$MILL_VERSION.jar"
44-
curl --fail -L -o "$DOWNLOAD_FILE" "$MILL_DOWNLOAD_URL"
45-
chmod +x "$DOWNLOAD_FILE"
46-
mv "$DOWNLOAD_FILE" "$MILL_EXEC_PATH"
250+
}
251+
try_to_use_system_mill
252+
253+
# If not already downloaded, download it
254+
if [ ! -s "${MILL}" ] || [ "$MILL_TEST_DRY_RUN_LAUNCHER_SCRIPT" = "1" ] ; then
255+
case $MILL_VERSION in
256+
0.0.* | 0.1.* | 0.2.* | 0.3.* | 0.4.* )
257+
DOWNLOAD_SUFFIX=""
258+
DOWNLOAD_FROM_MAVEN=0
259+
;;
260+
0.5.* | 0.6.* | 0.7.* | 0.8.* | 0.9.* | 0.10.* | 0.11.0-M* )
261+
DOWNLOAD_SUFFIX="-assembly"
262+
DOWNLOAD_FROM_MAVEN=0
263+
;;
264+
*)
265+
DOWNLOAD_SUFFIX="-assembly"
266+
DOWNLOAD_FROM_MAVEN=1
267+
;;
268+
esac
269+
case $MILL_VERSION in
270+
0.12.0 | 0.12.1 | 0.12.2 | 0.12.3 | 0.12.4 | 0.12.5 | 0.12.6 | 0.12.7 | 0.12.8 | 0.12.9 | 0.12.10 | 0.12.11 )
271+
DOWNLOAD_EXT="jar"
272+
;;
273+
0.12.* )
274+
DOWNLOAD_EXT="exe"
275+
;;
276+
0.* )
277+
DOWNLOAD_EXT="jar"
278+
;;
279+
*)
280+
DOWNLOAD_EXT="exe"
281+
;;
282+
esac
283+
284+
DOWNLOAD_FILE=$(mktemp mill.XXXXXX)
285+
if [ "$DOWNLOAD_FROM_MAVEN" = "1" ] ; then
286+
DOWNLOAD_URL="https://repo1.maven.org/maven2/com/lihaoyi/mill-dist${ARTIFACT_SUFFIX}/${MILL_VERSION}/mill-dist${ARTIFACT_SUFFIX}-${MILL_VERSION}.${DOWNLOAD_EXT}"
287+
else
288+
MILL_VERSION_TAG=$(echo "$MILL_VERSION" | sed -E 's/([^-]+)(-M[0-9]+)?(-.*)?/\1\2/')
289+
DOWNLOAD_URL="${GITHUB_RELEASE_CDN}${MILL_REPO_URL}/releases/download/${MILL_VERSION_TAG}/${MILL_VERSION}${DOWNLOAD_SUFFIX}"
290+
unset MILL_VERSION_TAG
291+
fi
292+
293+
if [ "$MILL_TEST_DRY_RUN_LAUNCHER_SCRIPT" = "1" ] ; then
294+
echo $DOWNLOAD_URL
295+
echo $MILL
296+
exit 0
297+
fi
298+
# TODO: handle command not found
299+
echo "Downloading mill ${MILL_VERSION} from ${DOWNLOAD_URL} ..." 1>&2
300+
${CURL_CMD} -f -L -o "${DOWNLOAD_FILE}" "${DOWNLOAD_URL}"
301+
chmod +x "${DOWNLOAD_FILE}"
302+
mkdir -p "${MILL_DOWNLOAD_PATH}"
303+
mv "${DOWNLOAD_FILE}" "${MILL}"
304+
47305
unset DOWNLOAD_FILE
48-
unset MILL_DOWNLOAD_URL
306+
unset DOWNLOAD_SUFFIX
49307
fi
50308

51309
if [ -z "$MILL_MAIN_CLI" ] ; then
52310
MILL_MAIN_CLI="${0}"
53311
fi
54312

55313
MILL_FIRST_ARG=""
56-
57-
# first arg is a long flag for "--interactive" or starts with "-i"
58-
if [ "$1" = "--bsp" ] || [ "${1#"-i"}" != "$1" ] || [ "$1" = "--interactive" ] || [ "$1" = "--no-server" ] || [ "$1" = "--repl" ] || [ "$1" = "--help" ] ; then
314+
if [ "$1" = "--bsp" ] || [ "${1#"-i"}" != "$1" ] || [ "$1" = "--interactive" ] || [ "$1" = "--no-server" ] || [ "$1" = "--no-daemon" ] || [ "$1" = "--repl" ] || [ "$1" = "--help" ] ; then
59315
# Need to preserve the first position of those listed options
60316
MILL_FIRST_ARG=$1
61317
shift
62318
fi
63319

64320
unset MILL_DOWNLOAD_PATH
321+
unset MILL_OLD_DOWNLOAD_PATH
322+
unset OLD_MILL
65323
unset MILL_VERSION
324+
unset MILL_REPO_URL
66325

67-
exec $MILL_EXEC_PATH $MILL_FIRST_ARG -D "mill.main.cli=${MILL_MAIN_CLI}" "$@"
326+
# -D mill.main.cli is for compatibility with Mill 0.10.9 - 0.13.0-M2
327+
# We don't quote MILL_FIRST_ARG on purpose, so we can expand the empty value without quotes
328+
# shellcheck disable=SC2086
329+
exec "${MILL}" $MILL_FIRST_ARG -D "mill.main.cli=${MILL_MAIN_CLI}" "$@"

0 commit comments

Comments
 (0)