Skip to content

Commit 69a5955

Browse files
committed
.
1 parent b04c679 commit 69a5955

File tree

1 file changed

+333
-0
lines changed

1 file changed

+333
-0
lines changed

mill

Lines changed: 333 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,333 @@
1+
#!/usr/bin/env sh
2+
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
32+
33+
set -e
34+
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+
41+
if [ -z "${DEFAULT_MILL_VERSION}" ] ; then
42+
DEFAULT_MILL_VERSION=1.0.3
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"
66+
elif [ -f "build.mill.scala" ] ; then
67+
MILL_BUILD_SCRIPT="build.mill.scala"
68+
elif [ -f "build.sc" ] ; then
69+
MILL_BUILD_SCRIPT="build.sc"
70+
fi
71+
72+
# Please note, that if a MILL_VERSION is already set in the environment,
73+
# We reuse it's value and skip searching for a value.
74+
75+
# If not already set, read .mill-version file
76+
if [ -z "${MILL_VERSION}" ] ; then
77+
if [ -f ".mill-version" ] ; then
78+
MILL_VERSION="$(tr '\r' '\n' < .mill-version | head -n 1 2> /dev/null)"
79+
elif [ -f ".config/mill-version" ] ; then
80+
MILL_VERSION="$(tr '\r' '\n' < .config/mill-version | head -n 1 2> /dev/null)"
81+
elif [ -n "${MILL_BUILD_SCRIPT}" ] ; then
82+
MILL_VERSION="$(cat ${MILL_BUILD_SCRIPT} | grep '//[|] *mill-version: *' | sed 's;//| *mill-version: *;;')"
83+
fi
84+
fi
85+
86+
MILL_USER_CACHE_DIR="${XDG_CACHE_HOME:-${HOME}/.cache}/mill"
87+
88+
if [ -z "${MILL_DOWNLOAD_PATH}" ] ; then
89+
MILL_DOWNLOAD_PATH="${MILL_USER_CACHE_DIR}/download"
90+
fi
91+
92+
# If not already set, try to fetch newest from Github
93+
if [ -z "${MILL_VERSION}" ] ; then
94+
# TODO: try to load latest version from release page
95+
echo "No mill version specified." 1>&2
96+
echo "You should provide a version via a '//| mill-version: ' comment or a '.mill-version' file." 1>&2
97+
98+
mkdir -p "${MILL_DOWNLOAD_PATH}"
99+
LANG=C touch -d '1 hour ago' "${MILL_DOWNLOAD_PATH}/.expire_latest" 2>/dev/null || (
100+
# we might be on OSX or BSD which don't have -d option for touch
101+
# but probably a -A [-][[hh]mm]SS
102+
touch "${MILL_DOWNLOAD_PATH}/.expire_latest"; touch -A -010000 "${MILL_DOWNLOAD_PATH}/.expire_latest"
103+
) || (
104+
# in case we still failed, we retry the first touch command with the intention
105+
# to show the (previously suppressed) error message
106+
LANG=C touch -d '1 hour ago' "${MILL_DOWNLOAD_PATH}/.expire_latest"
107+
)
108+
109+
# POSIX shell variant of bash's -nt operator, see https://unix.stackexchange.com/a/449744/6993
110+
# if [ "${MILL_DOWNLOAD_PATH}/.latest" -nt "${MILL_DOWNLOAD_PATH}/.expire_latest" ] ; then
111+
if [ -n "$(find -L "${MILL_DOWNLOAD_PATH}/.latest" -prune -newer "${MILL_DOWNLOAD_PATH}/.expire_latest")" ]; then
112+
# we know a current latest version
113+
MILL_VERSION=$(head -n 1 "${MILL_DOWNLOAD_PATH}"/.latest 2> /dev/null)
114+
fi
115+
116+
if [ -z "${MILL_VERSION}" ] ; then
117+
# we don't know a current latest version
118+
echo "Retrieving latest mill version ..." 1>&2
119+
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"
120+
MILL_VERSION=$(head -n 1 "${MILL_DOWNLOAD_PATH}"/.latest 2> /dev/null)
121+
fi
122+
123+
if [ -z "${MILL_VERSION}" ] ; then
124+
# Last resort
125+
MILL_VERSION="${DEFAULT_MILL_VERSION}"
126+
echo "Falling back to hardcoded mill version ${MILL_VERSION}" 1>&2
127+
else
128+
echo "Using mill version ${MILL_VERSION}" 1>&2
129+
fi
130+
fi
131+
132+
MILL_NATIVE_SUFFIX="-native"
133+
MILL_JVM_SUFFIX="-jvm"
134+
FULL_MILL_VERSION=$MILL_VERSION
135+
ARTIFACT_SUFFIX=""
136+
set_artifact_suffix(){
137+
if [ "$(expr substr $(uname -s) 1 5 2>/dev/null)" = "Linux" ]; then
138+
if [ "$(uname -m)" = "aarch64" ]; then
139+
ARTIFACT_SUFFIX="-native-linux-aarch64"
140+
else
141+
ARTIFACT_SUFFIX="-native-linux-amd64"
142+
fi
143+
elif [ "$(uname)" = "Darwin" ]; then
144+
if [ "$(uname -m)" = "arm64" ]; then
145+
ARTIFACT_SUFFIX="-native-mac-aarch64"
146+
else
147+
ARTIFACT_SUFFIX="-native-mac-amd64"
148+
fi
149+
else
150+
echo "This native mill launcher supports only Linux and macOS." 1>&2
151+
exit 1
152+
fi
153+
}
154+
155+
case "$MILL_VERSION" in
156+
*"$MILL_NATIVE_SUFFIX")
157+
MILL_VERSION=${MILL_VERSION%"$MILL_NATIVE_SUFFIX"}
158+
set_artifact_suffix
159+
;;
160+
161+
*"$MILL_JVM_SUFFIX")
162+
MILL_VERSION=${MILL_VERSION%"$MILL_JVM_SUFFIX"}
163+
;;
164+
165+
*)
166+
case "$MILL_VERSION" in
167+
0.1.*) ;;
168+
0.2.*) ;;
169+
0.3.*) ;;
170+
0.4.*) ;;
171+
0.5.*) ;;
172+
0.6.*) ;;
173+
0.7.*) ;;
174+
0.8.*) ;;
175+
0.9.*) ;;
176+
0.10.*) ;;
177+
0.11.*) ;;
178+
0.12.*) ;;
179+
*)
180+
set_artifact_suffix
181+
esac
182+
;;
183+
esac
184+
185+
MILL="${MILL_DOWNLOAD_PATH}/$MILL_VERSION$ARTIFACT_SUFFIX"
186+
187+
try_to_use_system_mill() {
188+
if [ "$(uname)" != "Linux" ]; then
189+
return 0
190+
fi
191+
192+
MILL_IN_PATH="$(command -v mill || true)"
193+
194+
if [ -z "${MILL_IN_PATH}" ]; then
195+
return 0
196+
fi
197+
198+
SYSTEM_MILL_FIRST_TWO_BYTES=$(head --bytes=2 "${MILL_IN_PATH}")
199+
if [ "${SYSTEM_MILL_FIRST_TWO_BYTES}" = "#!" ]; then
200+
# MILL_IN_PATH is (very likely) a shell script and not the mill
201+
# executable, ignore it.
202+
return 0
203+
fi
204+
205+
SYSTEM_MILL_PATH=$(readlink -e "${MILL_IN_PATH}")
206+
SYSTEM_MILL_SIZE=$(stat --format=%s "${SYSTEM_MILL_PATH}")
207+
SYSTEM_MILL_MTIME=$(stat --format=%y "${SYSTEM_MILL_PATH}")
208+
209+
if [ ! -d "${MILL_USER_CACHE_DIR}" ]; then
210+
mkdir -p "${MILL_USER_CACHE_DIR}"
211+
fi
212+
213+
SYSTEM_MILL_INFO_FILE="${MILL_USER_CACHE_DIR}/system-mill-info"
214+
if [ -f "${SYSTEM_MILL_INFO_FILE}" ]; then
215+
parseSystemMillInfo() {
216+
LINE_NUMBER="${1}"
217+
# Select the line number of the SYSTEM_MILL_INFO_FILE, cut the
218+
# variable definition in that line in two halves and return
219+
# the value, and finally remove the quotes.
220+
sed -n "${LINE_NUMBER}p" "${SYSTEM_MILL_INFO_FILE}" |\
221+
cut -d= -f2 |\
222+
sed 's/"\(.*\)"/\1/'
223+
}
224+
225+
CACHED_SYSTEM_MILL_PATH=$(parseSystemMillInfo 1)
226+
CACHED_SYSTEM_MILL_VERSION=$(parseSystemMillInfo 2)
227+
CACHED_SYSTEM_MILL_SIZE=$(parseSystemMillInfo 3)
228+
CACHED_SYSTEM_MILL_MTIME=$(parseSystemMillInfo 4)
229+
230+
if [ "${SYSTEM_MILL_PATH}" = "${CACHED_SYSTEM_MILL_PATH}" ] \
231+
&& [ "${SYSTEM_MILL_SIZE}" = "${CACHED_SYSTEM_MILL_SIZE}" ] \
232+
&& [ "${SYSTEM_MILL_MTIME}" = "${CACHED_SYSTEM_MILL_MTIME}" ]; then
233+
if [ "${CACHED_SYSTEM_MILL_VERSION}" = "${MILL_VERSION}" ]; then
234+
MILL="${SYSTEM_MILL_PATH}"
235+
return 0
236+
else
237+
return 0
238+
fi
239+
fi
240+
fi
241+
242+
SYSTEM_MILL_VERSION=$(${SYSTEM_MILL_PATH} --version | head -n1 | sed -n 's/^Mill.*version \(.*\)/\1/p')
243+
244+
cat <<EOF > "${SYSTEM_MILL_INFO_FILE}"
245+
CACHED_SYSTEM_MILL_PATH="${SYSTEM_MILL_PATH}"
246+
CACHED_SYSTEM_MILL_VERSION="${SYSTEM_MILL_VERSION}"
247+
CACHED_SYSTEM_MILL_SIZE="${SYSTEM_MILL_SIZE}"
248+
CACHED_SYSTEM_MILL_MTIME="${SYSTEM_MILL_MTIME}"
249+
EOF
250+
251+
if [ "${SYSTEM_MILL_VERSION}" = "${MILL_VERSION}" ]; then
252+
MILL="${SYSTEM_MILL_PATH}"
253+
fi
254+
}
255+
try_to_use_system_mill
256+
257+
# If not already downloaded, download it
258+
if [ ! -s "${MILL}" ] || [ "$MILL_TEST_DRY_RUN_LAUNCHER_SCRIPT" = "1" ] ; then
259+
case $MILL_VERSION in
260+
0.0.* | 0.1.* | 0.2.* | 0.3.* | 0.4.* )
261+
DOWNLOAD_SUFFIX=""
262+
DOWNLOAD_FROM_MAVEN=0
263+
;;
264+
0.5.* | 0.6.* | 0.7.* | 0.8.* | 0.9.* | 0.10.* | 0.11.0-M* )
265+
DOWNLOAD_SUFFIX="-assembly"
266+
DOWNLOAD_FROM_MAVEN=0
267+
;;
268+
*)
269+
DOWNLOAD_SUFFIX="-assembly"
270+
DOWNLOAD_FROM_MAVEN=1
271+
;;
272+
esac
273+
case $MILL_VERSION in
274+
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 )
275+
DOWNLOAD_EXT="jar"
276+
;;
277+
0.12.* )
278+
DOWNLOAD_EXT="exe"
279+
;;
280+
0.* )
281+
DOWNLOAD_EXT="jar"
282+
;;
283+
*)
284+
DOWNLOAD_EXT="exe"
285+
;;
286+
esac
287+
288+
DOWNLOAD_FILE=$(mktemp mill.XXXXXX)
289+
if [ "$DOWNLOAD_FROM_MAVEN" = "1" ] ; then
290+
DOWNLOAD_URL="https://repo1.maven.org/maven2/com/lihaoyi/mill-dist${ARTIFACT_SUFFIX}/${MILL_VERSION}/mill-dist${ARTIFACT_SUFFIX}-${MILL_VERSION}.${DOWNLOAD_EXT}"
291+
else
292+
MILL_VERSION_TAG=$(echo "$MILL_VERSION" | sed -E 's/([^-]+)(-M[0-9]+)?(-.*)?/\1\2/')
293+
DOWNLOAD_URL="${GITHUB_RELEASE_CDN}${MILL_REPO_URL}/releases/download/${MILL_VERSION_TAG}/${MILL_VERSION}${DOWNLOAD_SUFFIX}"
294+
unset MILL_VERSION_TAG
295+
fi
296+
297+
if [ "$MILL_TEST_DRY_RUN_LAUNCHER_SCRIPT" = "1" ] ; then
298+
echo $DOWNLOAD_URL
299+
echo $MILL
300+
exit 0
301+
fi
302+
# TODO: handle command not found
303+
echo "Downloading mill ${MILL_VERSION} from ${DOWNLOAD_URL} ..." 1>&2
304+
${CURL_CMD} -f -L -o "${DOWNLOAD_FILE}" "${DOWNLOAD_URL}"
305+
chmod +x "${DOWNLOAD_FILE}"
306+
mkdir -p "${MILL_DOWNLOAD_PATH}"
307+
mv "${DOWNLOAD_FILE}" "${MILL}"
308+
309+
unset DOWNLOAD_FILE
310+
unset DOWNLOAD_SUFFIX
311+
fi
312+
313+
if [ -z "$MILL_MAIN_CLI" ] ; then
314+
MILL_MAIN_CLI="${0}"
315+
fi
316+
317+
MILL_FIRST_ARG=""
318+
if [ "$1" = "--bsp" ] || [ "${1#"-i"}" != "$1" ] || [ "$1" = "--interactive" ] || [ "$1" = "--no-server" ] || [ "$1" = "--no-daemon" ] || [ "$1" = "--repl" ] || [ "$1" = "--help" ] ; then
319+
# Need to preserve the first position of those listed options
320+
MILL_FIRST_ARG=$1
321+
shift
322+
fi
323+
324+
unset MILL_DOWNLOAD_PATH
325+
unset MILL_OLD_DOWNLOAD_PATH
326+
unset OLD_MILL
327+
unset MILL_VERSION
328+
unset MILL_REPO_URL
329+
330+
# -D mill.main.cli is for compatibility with Mill 0.10.9 - 0.13.0-M2
331+
# We don't quote MILL_FIRST_ARG on purpose, so we can expand the empty value without quotes
332+
# shellcheck disable=SC2086
333+
exec "${MILL}" $MILL_FIRST_ARG -D "mill.main.cli=${MILL_MAIN_CLI}" "$@"

0 commit comments

Comments
 (0)