Skip to content

Commit d23d599

Browse files
authored
Update Mill to 0.11.1, migrate script to millw (#20)
1 parent 3e5082a commit d23d599

File tree

3 files changed

+188
-28
lines changed

3 files changed

+188
-28
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ classes/
2828

2929
# mill
3030
out/
31+
32+
# bsp
33+
.bsp/

.mill-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.10.7
1+
0.11.1

mill

Lines changed: 184 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,206 @@
11
#!/usr/bin/env sh
22

33
# 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
4+
# You can give the required mill version with --mill-version parameter
55
# If no version is given, it falls back to the value of DEFAULT_MILL_VERSION
6-
DEFAULT_MILL_VERSION=0.10.5
6+
#
7+
# Project page: https://github.com/lefou/millw
8+
# Script Version: 0.4.7
9+
#
10+
# If you want to improve this script, please also contribute your changes back!
11+
#
12+
# Licensed under the Apache License, Version 2.0
713

814
set -e
915

10-
if [ -z "$MILL_VERSION" ] ; then
16+
if [ -z "${DEFAULT_MILL_VERSION}" ] ; then
17+
DEFAULT_MILL_VERSION="0.11.0"
18+
fi
19+
20+
21+
if [ -z "${GITHUB_RELEASE_CDN}" ] ; then
22+
GITHUB_RELEASE_CDN=""
23+
fi
24+
25+
26+
MILL_REPO_URL="https://github.com/com-lihaoyi/mill"
27+
28+
if [ -z "${CURL_CMD}" ] ; then
29+
CURL_CMD=curl
30+
fi
31+
32+
# Explicit commandline argument takes precedence over all other methods
33+
if [ "$1" = "--mill-version" ] ; then
34+
shift
35+
if [ "x$1" != "x" ] ; then
36+
MILL_VERSION="$1"
37+
shift
38+
else
39+
echo "You specified --mill-version without a version." 1>&2
40+
echo "Please provide a version that matches one provided on" 1>&2
41+
echo "${MILL_REPO_URL}/releases" 1>&2
42+
false
43+
fi
44+
fi
45+
46+
# Please note, that if a MILL_VERSION is already set in the environment,
47+
# We reuse it's value and skip searching for a value.
48+
49+
# If not already set, read .mill-version file
50+
if [ -z "${MILL_VERSION}" ] ; then
1151
if [ -f ".mill-version" ] ; then
1252
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)
53+
elif [ -f ".config/mill-version" ] ; then
54+
MILL_VERSION="$(head -n 1 .config/mill-version 2> /dev/null)"
55+
fi
56+
fi
57+
58+
if [ -z "${MILL_DOWNLOAD_PATH}" ] ; then
59+
if [ -n "${XDG_CACHE_HOME}" ] ; then
60+
MILL_DOWNLOAD_PATH="${XDG_CACHE_HOME}/mill/download"
1561
else
16-
MILL_VERSION=$DEFAULT_MILL_VERSION
62+
MILL_DOWNLOAD_PATH="${HOME}/.cache/mill/download"
1763
fi
1864
fi
1965

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"
66+
# If not already set, try to fetch newest from Github
67+
if [ -z "${MILL_VERSION}" ] ; then
68+
# TODO: try to load latest version from release page
69+
echo "No mill version specified." 1>&2
70+
echo "You should provide a version via '.mill-version' file or --mill-version option." 1>&2
71+
72+
mkdir -p "${MILL_DOWNLOAD_PATH}"
73+
LANG=C touch -d '1 hour ago' "${MILL_DOWNLOAD_PATH}/.expire_latest" 2>/dev/null || (
74+
# we might be on OSX or BSD which don't have -d option for touch
75+
# but probably a -A [-][[hh]mm]SS
76+
touch "${MILL_DOWNLOAD_PATH}/.expire_latest"; touch -A -010000 "${MILL_DOWNLOAD_PATH}/.expire_latest"
77+
) || (
78+
# in case we still failed, we retry the first touch command with the intention
79+
# to show the (previously suppressed) error message
80+
LANG=C touch -d '1 hour ago' "${MILL_DOWNLOAD_PATH}/.expire_latest"
81+
)
82+
83+
# POSIX shell variant of bash's -nt operator, see https://unix.stackexchange.com/a/449744/6993
84+
# if [ "${MILL_DOWNLOAD_PATH}/.latest" -nt "${MILL_DOWNLOAD_PATH}/.expire_latest" ] ; then
85+
if [ -n "$(find -L "${MILL_DOWNLOAD_PATH}/.latest" -prune -newer "${MILL_DOWNLOAD_PATH}/.expire_latest")" ]; then
86+
# we know a current latest version
87+
MILL_VERSION=$(head -n 1 "${MILL_DOWNLOAD_PATH}"/.latest 2> /dev/null)
88+
fi
89+
90+
if [ -z "${MILL_VERSION}" ] ; then
91+
# we don't know a current latest version
92+
echo "Retrieving latest mill version ..." 1>&2
93+
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"
94+
MILL_VERSION=$(head -n 1 "${MILL_DOWNLOAD_PATH}"/.latest 2> /dev/null)
95+
fi
96+
97+
if [ -z "${MILL_VERSION}" ] ; then
98+
# Last resort
99+
MILL_VERSION="${DEFAULT_MILL_VERSION}"
100+
echo "Falling back to hardcoded mill version ${MILL_VERSION}" 1>&2
101+
else
102+
echo "Using mill version ${MILL_VERSION}" 1>&2
103+
fi
24104
fi
25-
MILL_EXEC_PATH="${MILL_DOWNLOAD_PATH}/${MILL_VERSION}"
26105

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#*.}"
106+
MILL="${MILL_DOWNLOAD_PATH}/${MILL_VERSION}"
107+
108+
try_to_use_system_mill() {
109+
MILL_IN_PATH="$(command -v mill || true)"
110+
111+
if [ -z "${MILL_IN_PATH}" ]; then
112+
return
113+
fi
114+
115+
UNIVERSAL_SCRIPT_MAGIC="@ 2>/dev/null # 2>nul & echo off & goto BOF"
30116

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"
117+
if ! head -c 128 "${MILL_IN_PATH}" | grep -qF "${UNIVERSAL_SCRIPT_MAGIC}"; then
118+
if [ -n "${MILLW_VERBOSE}" ]; then
119+
echo "Could not determine mill version of ${MILL_IN_PATH}, as it does not start with the universal script magic2" 1>&2
120+
fi
121+
return
35122
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
123+
124+
# Roughly the size of the universal script.
125+
MILL_VERSION_SEARCH_RANGE="2403"
126+
MILL_IN_PATH_VERSION=$(head -c "${MILL_VERSION_SEARCH_RANGE}" "${MILL_IN_PATH}" |\
127+
sed -n 's/^.*-DMILL_VERSION=\([^\s]*\) .*$/\1/p' |\
128+
head -n 1)
129+
130+
if [ -z "${MILL_IN_PATH_VERSION}" ]; then
131+
echo "Could not determine mill version, even though ${MILL_IN_PATH} has the universal script magic" 1>&2
132+
return
133+
fi
134+
135+
if [ "${MILL_IN_PATH_VERSION}" = "${MILL_VERSION}" ]; then
136+
MILL="${MILL_IN_PATH}"
137+
fi
138+
}
139+
try_to_use_system_mill
140+
141+
# If not already downloaded, download it
142+
if [ ! -s "${MILL}" ] ; then
143+
144+
# support old non-XDG download dir
145+
MILL_OLD_DOWNLOAD_PATH="${HOME}/.mill/download"
146+
OLD_MILL="${MILL_OLD_DOWNLOAD_PATH}/${MILL_VERSION}"
147+
if [ -x "${OLD_MILL}" ] ; then
148+
MILL="${OLD_MILL}"
149+
else
150+
case $MILL_VERSION in
151+
0.0.* | 0.1.* | 0.2.* | 0.3.* | 0.4.* )
152+
DOWNLOAD_SUFFIX=""
153+
DOWNLOAD_FROM_MAVEN=0
154+
;;
155+
0.5.* | 0.6.* | 0.7.* | 0.8.* | 0.9.* | 0.10.* | 0.11.0-M* )
156+
DOWNLOAD_SUFFIX="-assembly"
157+
DOWNLOAD_FROM_MAVEN=0
158+
;;
159+
*)
160+
DOWNLOAD_SUFFIX="-assembly"
161+
DOWNLOAD_FROM_MAVEN=1
162+
;;
163+
esac
164+
165+
DOWNLOAD_FILE=$(mktemp mill.XXXXXX)
166+
167+
if [ "$DOWNLOAD_FROM_MAVEN" = "1" ] ; then
168+
DOWNLOAD_URL="https://repo1.maven.org/maven2/com/lihaoyi/mill-dist/${MILL_VERSION}/mill-dist-${MILL_VERSION}.jar"
169+
else
170+
MILL_VERSION_TAG=$(echo $MILL_VERSION | sed -E 's/([^-]+)(-M[0-9]+)?(-.*)?/\1\2/')
171+
DOWNLOAD_URL="${GITHUB_RELEASE_CDN}${MILL_REPO_URL}/releases/download/${MILL_VERSION_TAG}/${MILL_VERSION}${DOWNLOAD_SUFFIX}"
172+
unset MILL_VERSION_TAG
173+
fi
174+
175+
# TODO: handle command not found
176+
echo "Downloading mill ${MILL_VERSION} from ${DOWNLOAD_URL} ..." 1>&2
177+
${CURL_CMD} -f -L -o "${DOWNLOAD_FILE}" "${DOWNLOAD_URL}"
178+
chmod +x "${DOWNLOAD_FILE}"
179+
mkdir -p "${MILL_DOWNLOAD_PATH}"
180+
mv "${DOWNLOAD_FILE}" "${MILL}"
181+
182+
unset DOWNLOAD_FILE
183+
unset DOWNLOAD_SUFFIX
184+
fi
185+
fi
186+
187+
if [ -z "$MILL_MAIN_CLI" ] ; then
188+
MILL_MAIN_CLI="${0}"
189+
fi
190+
191+
MILL_FIRST_ARG=""
192+
if [ "$1" = "--bsp" ] || [ "$1" = "-i" ] || [ "$1" = "--interactive" ] || [ "$1" = "--no-server" ] || [ "$1" = "--repl" ] || [ "$1" = "--help" ] ; then
193+
# Need to preserve the first position of those listed options
194+
MILL_FIRST_ARG=$1
195+
shift
44196
fi
45197

46198
unset MILL_DOWNLOAD_PATH
199+
unset MILL_OLD_DOWNLOAD_PATH
200+
unset OLD_MILL
47201
unset MILL_VERSION
202+
unset MILL_REPO_URL
48203

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

0 commit comments

Comments
 (0)