Skip to content
This repository was archived by the owner on May 4, 2025. It is now read-only.

Commit ed5a8f0

Browse files
authored
Merge pull request #188 from ckipp01/updateMill
2 parents 69e27a5 + eb76082 commit ed5a8f0

1 file changed

Lines changed: 294 additions & 31 deletions

File tree

mill

Lines changed: 294 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,312 @@
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
6-
DEFAULT_MILL_VERSION=0.10.5
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+
# - if accessible, find the latest stable version available on Maven Central (https://repo1.maven.org/maven2)
10+
# - env-variable `DEFAULT_MILL_VERSION`
11+
#
12+
# If a version has the suffix '-native' a native binary will be used.
13+
# If a version has the suffix '-jvm' an executable jar file will be used, requiring an already installed Java runtime.
14+
# If no such suffix is found, the script will pick a default based on version and platform.
15+
#
16+
# Once a version was determined, it tries to use either
17+
# - a system-installed mill, if found and it's version matches
18+
# - an already downloaded version under ~/.cache/mill/download
19+
#
20+
# If no working mill version was found on the system,
21+
# this script downloads a binary file from Maven Central or Github Pages (this is version dependent)
22+
# into a cache location (~/.cache/mill/download).
23+
#
24+
# Mill Project URL: https://github.com/com-lihaoyi/mill
25+
# Script Version: 0.13.0-M1-13-935205
26+
#
27+
# If you want to improve this script, please also contribute your changes back!
28+
# This script was generated from: scripts/src/mill.sh
29+
#
30+
# Licensed under the Apache License, Version 2.0
731

832
set -e
933

10-
if [ -z "$MILL_VERSION" ] ; then
11-
if [ -f ".mill-version" ] ; then
12-
MILL_VERSION="$(head -n 1 .mill-version 2> /dev/null)"
13-
elif [ -f "mill" ] && [ "$0" != "mill" ] ; then
14-
MILL_VERSION=$(grep -F "DEFAULT_MILL_VERSION=" "mill" | head -n 1 | cut -d= -f2)
34+
if [ -z "${DEFAULT_MILL_VERSION}" ] ; then
35+
DEFAULT_MILL_VERSION="0.12.10"
36+
fi
37+
38+
39+
if [ -z "${GITHUB_RELEASE_CDN}" ] ; then
40+
GITHUB_RELEASE_CDN=""
41+
fi
42+
43+
44+
MILL_REPO_URL="https://github.com/com-lihaoyi/mill"
45+
46+
if [ -z "${CURL_CMD}" ] ; then
47+
CURL_CMD=curl
48+
fi
49+
50+
# Explicit commandline argument takes precedence over all other methods
51+
if [ "$1" = "--mill-version" ] ; then
52+
shift
53+
if [ "x$1" != "x" ] ; then
54+
MILL_VERSION="$1"
55+
shift
1556
else
16-
MILL_VERSION=$DEFAULT_MILL_VERSION
57+
echo "You specified --mill-version without a version." 1>&2
58+
echo "Please provide a version that matches one provided on" 1>&2
59+
echo "${MILL_REPO_URL}/releases" 1>&2
60+
false
61+
fi
62+
fi
63+
64+
# Please note, that if a MILL_VERSION is already set in the environment,
65+
# We reuse it's value and skip searching for a value.
66+
67+
# If not already set, read .mill-version file
68+
if [ -z "${MILL_VERSION}" ] ; then
69+
if [ -f ".mill-version" ] ; then
70+
MILL_VERSION="$(tr '\r' '\n' < .mill-version | head -n 1 2> /dev/null)"
71+
elif [ -f ".config/mill-version" ] ; then
72+
MILL_VERSION="$(tr '\r' '\n' < .config/mill-version | head -n 1 2> /dev/null)"
1773
fi
1874
fi
1975

20-
if [ "x${XDG_CACHE_HOME}" != "x" ] ; then
21-
MILL_DOWNLOAD_PATH="${XDG_CACHE_HOME}/mill/download"
22-
else
23-
MILL_DOWNLOAD_PATH="${HOME}/.cache/mill/download"
76+
MILL_USER_CACHE_DIR="${XDG_CACHE_HOME:-${HOME}/.cache}/mill"
77+
78+
if [ -z "${MILL_DOWNLOAD_PATH}" ] ; then
79+
MILL_DOWNLOAD_PATH="${MILL_USER_CACHE_DIR}/download"
2480
fi
25-
MILL_EXEC_PATH="${MILL_DOWNLOAD_PATH}/${MILL_VERSION}"
2681

27-
version_remainder="$MILL_VERSION"
28-
MILL_MAJOR_VERSION="${version_remainder%%.*}"; version_remainder="${version_remainder#*.}"
29-
MILL_MINOR_VERSION="${version_remainder%%.*}"; version_remainder="${version_remainder#*.}"
82+
# If not already set, try to fetch newest from Github
83+
if [ -z "${MILL_VERSION}" ] ; then
84+
# TODO: try to load latest version from release page
85+
echo "No mill version specified." 1>&2
86+
echo "You should provide a version via '.mill-version' file or --mill-version option." 1>&2
87+
88+
mkdir -p "${MILL_DOWNLOAD_PATH}"
89+
LANG=C touch -d '1 hour ago' "${MILL_DOWNLOAD_PATH}/.expire_latest" 2>/dev/null || (
90+
# we might be on OSX or BSD which don't have -d option for touch
91+
# but probably a -A [-][[hh]mm]SS
92+
touch "${MILL_DOWNLOAD_PATH}/.expire_latest"; touch -A -010000 "${MILL_DOWNLOAD_PATH}/.expire_latest"
93+
) || (
94+
# in case we still failed, we retry the first touch command with the intention
95+
# to show the (previously suppressed) error message
96+
LANG=C touch -d '1 hour ago' "${MILL_DOWNLOAD_PATH}/.expire_latest"
97+
)
98+
99+
# POSIX shell variant of bash's -nt operator, see https://unix.stackexchange.com/a/449744/6993
100+
# if [ "${MILL_DOWNLOAD_PATH}/.latest" -nt "${MILL_DOWNLOAD_PATH}/.expire_latest" ] ; then
101+
if [ -n "$(find -L "${MILL_DOWNLOAD_PATH}/.latest" -prune -newer "${MILL_DOWNLOAD_PATH}/.expire_latest")" ]; then
102+
# we know a current latest version
103+
MILL_VERSION=$(head -n 1 "${MILL_DOWNLOAD_PATH}"/.latest 2> /dev/null)
104+
fi
105+
106+
if [ -z "${MILL_VERSION}" ] ; then
107+
# we don't know a current latest version
108+
echo "Retrieving latest mill version ..." 1>&2
109+
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"
110+
MILL_VERSION=$(head -n 1 "${MILL_DOWNLOAD_PATH}"/.latest 2> /dev/null)
111+
fi
112+
113+
if [ -z "${MILL_VERSION}" ] ; then
114+
# Last resort
115+
MILL_VERSION="${DEFAULT_MILL_VERSION}"
116+
echo "Falling back to hardcoded mill version ${MILL_VERSION}" 1>&2
117+
else
118+
echo "Using mill version ${MILL_VERSION}" 1>&2
119+
fi
120+
fi
121+
122+
MILL_NATIVE_SUFFIX="-native"
123+
MILL_JVM_SUFFIX="-jvm"
124+
FULL_MILL_VERSION=$MILL_VERSION
125+
ARTIFACT_SUFFIX=""
126+
set_artifact_suffix(){
127+
if [ "$(expr substr $(uname -s) 1 5 2>/dev/null)" = "Linux" ]; then
128+
if [ "$(uname -m)" = "aarch64" ]; then
129+
ARTIFACT_SUFFIX="-native-linux-aarch64"
130+
else
131+
ARTIFACT_SUFFIX="-native-linux-amd64"
132+
fi
133+
elif [ "$(uname)" = "Darwin" ]; then
134+
if [ "$(uname -m)" = "arm64" ]; then
135+
ARTIFACT_SUFFIX="-native-mac-aarch64"
136+
else
137+
ARTIFACT_SUFFIX="-native-mac-amd64"
138+
fi
139+
else
140+
echo "This native mill launcher supports only Linux and macOS." 1>&2
141+
exit 1
142+
fi
143+
}
144+
145+
case "$MILL_VERSION" in
146+
*"$MILL_NATIVE_SUFFIX")
147+
MILL_VERSION=${MILL_VERSION%"$MILL_NATIVE_SUFFIX"}
148+
set_artifact_suffix
149+
;;
150+
151+
*"$MILL_JVM_SUFFIX")
152+
MILL_VERSION=${MILL_VERSION%"$MILL_JVM_SUFFIX"}
153+
;;
30154

31-
if [ ! -s "$MILL_EXEC_PATH" ] ; then
32-
mkdir -p "$MILL_DOWNLOAD_PATH"
33-
if [ "$MILL_MAJOR_VERSION" -gt 0 ] || [ "$MILL_MINOR_VERSION" -ge 5 ] ; then
34-
ASSEMBLY="-assembly"
155+
*)
156+
case "$MILL_VERSION" in
157+
0.1.*) ;;
158+
0.2.*) ;;
159+
0.3.*) ;;
160+
0.4.*) ;;
161+
0.5.*) ;;
162+
0.6.*) ;;
163+
0.7.*) ;;
164+
0.8.*) ;;
165+
0.9.*) ;;
166+
0.10.*) ;;
167+
0.11.*) ;;
168+
0.12.*) ;;
169+
*)
170+
set_artifact_suffix
171+
esac
172+
;;
173+
esac
174+
175+
MILL="${MILL_DOWNLOAD_PATH}/$MILL_VERSION$ARTIFACT_SUFFIX"
176+
177+
try_to_use_system_mill() {
178+
if [ "$(uname)" != "Linux" ]; then
179+
return 0
180+
fi
181+
182+
MILL_IN_PATH="$(command -v mill || true)"
183+
184+
if [ -z "${MILL_IN_PATH}" ]; then
185+
return 0
186+
fi
187+
188+
SYSTEM_MILL_FIRST_TWO_BYTES=$(head --bytes=2 "${MILL_IN_PATH}")
189+
if [ "${SYSTEM_MILL_FIRST_TWO_BYTES}" = "#!" ]; then
190+
# MILL_IN_PATH is (very likely) a shell script and not the mill
191+
# executable, ignore it.
192+
return 0
193+
fi
194+
195+
SYSTEM_MILL_PATH=$(readlink -e "${MILL_IN_PATH}")
196+
SYSTEM_MILL_SIZE=$(stat --format=%s "${SYSTEM_MILL_PATH}")
197+
SYSTEM_MILL_MTIME=$(stat --format=%y "${SYSTEM_MILL_PATH}")
198+
199+
if [ ! -d "${MILL_USER_CACHE_DIR}" ]; then
200+
mkdir -p "${MILL_USER_CACHE_DIR}"
201+
fi
202+
203+
SYSTEM_MILL_INFO_FILE="${MILL_USER_CACHE_DIR}/system-mill-info"
204+
if [ -f "${SYSTEM_MILL_INFO_FILE}" ]; then
205+
parseSystemMillInfo() {
206+
LINE_NUMBER="${1}"
207+
# Select the line number of the SYSTEM_MILL_INFO_FILE, cut the
208+
# variable definition in that line in two halves and return
209+
# the value, and finally remove the quotes.
210+
sed -n "${LINE_NUMBER}p" "${SYSTEM_MILL_INFO_FILE}" |\
211+
cut -d= -f2 |\
212+
sed 's/"\(.*\)"/\1/'
213+
}
214+
215+
CACHED_SYSTEM_MILL_PATH=$(parseSystemMillInfo 1)
216+
CACHED_SYSTEM_MILL_VERSION=$(parseSystemMillInfo 2)
217+
CACHED_SYSTEM_MILL_SIZE=$(parseSystemMillInfo 3)
218+
CACHED_SYSTEM_MILL_MTIME=$(parseSystemMillInfo 4)
219+
220+
if [ "${SYSTEM_MILL_PATH}" = "${CACHED_SYSTEM_MILL_PATH}" ] \
221+
&& [ "${SYSTEM_MILL_SIZE}" = "${CACHED_SYSTEM_MILL_SIZE}" ] \
222+
&& [ "${SYSTEM_MILL_MTIME}" = "${CACHED_SYSTEM_MILL_MTIME}" ]; then
223+
if [ "${CACHED_SYSTEM_MILL_VERSION}" = "${MILL_VERSION}" ]; then
224+
MILL="${SYSTEM_MILL_PATH}"
225+
return 0
226+
else
227+
return 0
228+
fi
229+
fi
230+
fi
231+
232+
SYSTEM_MILL_VERSION=$(${SYSTEM_MILL_PATH} --version | head -n1 | sed -n 's/^Mill.*version \(.*\)/\1/p')
233+
234+
cat <<EOF > "${SYSTEM_MILL_INFO_FILE}"
235+
CACHED_SYSTEM_MILL_PATH="${SYSTEM_MILL_PATH}"
236+
CACHED_SYSTEM_MILL_VERSION="${SYSTEM_MILL_VERSION}"
237+
CACHED_SYSTEM_MILL_SIZE="${SYSTEM_MILL_SIZE}"
238+
CACHED_SYSTEM_MILL_MTIME="${SYSTEM_MILL_MTIME}"
239+
EOF
240+
241+
if [ "${SYSTEM_MILL_VERSION}" = "${MILL_VERSION}" ]; then
242+
MILL="${SYSTEM_MILL_PATH}"
35243
fi
36-
DOWNLOAD_FILE=$MILL_EXEC_PATH-tmp-download
37-
MILL_VERSION_TAG=$(echo $MILL_VERSION | sed -E 's/([^-]+)(-M[0-9]+)?(-.*)?/\1\2/')
38-
MILL_DOWNLOAD_URL="https://github.com/lihaoyi/mill/releases/download/${MILL_VERSION_TAG}/$MILL_VERSION${ASSEMBLY}"
39-
curl --fail -L -o "$DOWNLOAD_FILE" "$MILL_DOWNLOAD_URL"
40-
chmod +x "$DOWNLOAD_FILE"
41-
mv "$DOWNLOAD_FILE" "$MILL_EXEC_PATH"
42-
unset DOWNLOAD_FILE
43-
unset MILL_DOWNLOAD_URL
244+
}
245+
try_to_use_system_mill
246+
247+
# If not already downloaded, download it
248+
if [ ! -s "${MILL}" ] ; then
249+
250+
# support old non-XDG download dir
251+
MILL_OLD_DOWNLOAD_PATH="${HOME}/.mill/download"
252+
OLD_MILL="${MILL_OLD_DOWNLOAD_PATH}/${MILL_VERSION}"
253+
if [ -x "${OLD_MILL}" ] ; then
254+
MILL="${OLD_MILL}"
255+
else
256+
case $MILL_VERSION in
257+
0.0.* | 0.1.* | 0.2.* | 0.3.* | 0.4.* )
258+
DOWNLOAD_SUFFIX=""
259+
DOWNLOAD_FROM_MAVEN=0
260+
;;
261+
0.5.* | 0.6.* | 0.7.* | 0.8.* | 0.9.* | 0.10.* | 0.11.0-M* )
262+
DOWNLOAD_SUFFIX="-assembly"
263+
DOWNLOAD_FROM_MAVEN=0
264+
;;
265+
*)
266+
DOWNLOAD_SUFFIX="-assembly"
267+
DOWNLOAD_FROM_MAVEN=1
268+
;;
269+
esac
270+
271+
DOWNLOAD_FILE=$(mktemp mill.XXXXXX)
272+
273+
if [ "$DOWNLOAD_FROM_MAVEN" = "1" ] ; then
274+
DOWNLOAD_URL="https://repo1.maven.org/maven2/com/lihaoyi/mill-dist${ARTIFACT_SUFFIX}/${MILL_VERSION}/mill-dist${ARTIFACT_SUFFIX}-${MILL_VERSION}.jar"
275+
else
276+
MILL_VERSION_TAG=$(echo "$MILL_VERSION" | sed -E 's/([^-]+)(-M[0-9]+)?(-.*)?/\1\2/')
277+
DOWNLOAD_URL="${GITHUB_RELEASE_CDN}${MILL_REPO_URL}/releases/download/${MILL_VERSION_TAG}/${MILL_VERSION}${DOWNLOAD_SUFFIX}"
278+
unset MILL_VERSION_TAG
279+
fi
280+
281+
# TODO: handle command not found
282+
echo "Downloading mill ${MILL_VERSION} from ${DOWNLOAD_URL} ..." 1>&2
283+
${CURL_CMD} -f -L -o "${DOWNLOAD_FILE}" "${DOWNLOAD_URL}"
284+
chmod +x "${DOWNLOAD_FILE}"
285+
mkdir -p "${MILL_DOWNLOAD_PATH}"
286+
mv "${DOWNLOAD_FILE}" "${MILL}"
287+
288+
unset DOWNLOAD_FILE
289+
unset DOWNLOAD_SUFFIX
290+
fi
291+
fi
292+
293+
if [ -z "$MILL_MAIN_CLI" ] ; then
294+
MILL_MAIN_CLI="${0}"
295+
fi
296+
297+
MILL_FIRST_ARG=""
298+
if [ "$1" = "--bsp" ] || [ "$1" = "-i" ] || [ "$1" = "--interactive" ] || [ "$1" = "--no-server" ] || [ "$1" = "--repl" ] || [ "$1" = "--help" ] ; then
299+
# Need to preserve the first position of those listed options
300+
MILL_FIRST_ARG=$1
301+
shift
44302
fi
45303

46304
unset MILL_DOWNLOAD_PATH
305+
unset MILL_OLD_DOWNLOAD_PATH
306+
unset OLD_MILL
47307
unset MILL_VERSION
308+
unset MILL_REPO_URL
48309

49-
exec $MILL_EXEC_PATH "$@"
310+
# We don't quote MILL_FIRST_ARG on purpose, so we can expand the empty value without quotes
311+
# shellcheck disable=SC2086
312+
exec "${MILL}" $MILL_FIRST_ARG -D "mill.main.cli=${MILL_MAIN_CLI}" "$@"

0 commit comments

Comments
 (0)