Skip to content

Commit 22a7169

Browse files
Update mill launchers
1 parent 5c5547f commit 22a7169

File tree

2 files changed

+93
-36
lines changed

2 files changed

+93
-36
lines changed

Diff for: mill

+77-26
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,33 @@
55
# If no version is given, it falls back to the value of DEFAULT_MILL_VERSION
66
#
77
# Project page: https://github.com/lefou/millw
8+
# Script Version: 0.4.2
89
#
910
# If you want to improve this script, please also contribute your changes back!
1011
#
1112
# Licensed under the Apache License, Version 2.0
1213

1314

14-
DEFAULT_MILL_VERSION=0.9.5
15+
DEFAULT_MILL_VERSION=0.10.0
1516

1617
set -e
1718

1819
MILL_REPO_URL="https://github.com/com-lihaoyi/mill"
1920

21+
if [ -z "${CURL_CMD}" ] ; then
22+
CURL_CMD=curl
23+
fi
24+
2025
# Explicit commandline argument takes precedence over all other methods
21-
if [ "x$1" = "x--mill-version" ] ; then
26+
if [ "$1" = "--mill-version" ] ; then
2227
shift
2328
if [ "x$1" != "x" ] ; then
2429
MILL_VERSION="$1"
2530
shift
2631
else
27-
echo "You specified --mill-version without a version."
28-
echo "Please provide a version that matches one provided on"
29-
echo "${MILL_REPO_URL}/releases"
32+
echo "You specified --mill-version without a version." 1>&2
33+
echo "Please provide a version that matches one provided on" 1>&2
34+
echo "${MILL_REPO_URL}/releases" 1>&2
3035
false
3136
fi
3237
fi
@@ -35,59 +40,103 @@ fi
3540
# We reuse it's value and skip searching for a value.
3641

3742
# If not already set, read .mill-version file
38-
if [ "x${MILL_VERSION}" = "x" ] ; then
43+
if [ -z "${MILL_VERSION}" ] ; then
3944
if [ -f ".mill-version" ] ; then
4045
MILL_VERSION="$(head -n 1 .mill-version 2> /dev/null)"
4146
fi
4247
fi
4348

44-
if [ "x${XDG_CACHE_HOME}" != "x" ] ; then
49+
if [ -n "${XDG_CACHE_HOME}" ] ; then
4550
MILL_DOWNLOAD_PATH="${XDG_CACHE_HOME}/mill/download"
4651
else
4752
MILL_DOWNLOAD_PATH="${HOME}/.cache/mill/download"
4853
fi
4954

5055
# If not already set, try to fetch newest from Github
51-
if [ "x${MILL_VERSION}" = "x" ] ; then
56+
if [ -z "${MILL_VERSION}" ] ; then
5257
# TODO: try to load latest version from release page
53-
echo "No mill version specified."
54-
echo "You should provide a version via '.mill-version' file or --mill-version option."
58+
echo "No mill version specified." 1>&2
59+
echo "You should provide a version via '.mill-version' file or --mill-version option." 1>&2
5560

5661
mkdir -p "${MILL_DOWNLOAD_PATH}"
57-
LANG=C touch -d '1 hour ago' "${MILL_DOWNLOAD_PATH}/.expire_latest"
58-
if [ "${MILL_DOWNLOAD_PATH}/.latest" -nt "${MILL_DOWNLOAD_PATH}/.expire_latest" ] ; then
62+
LANG=C touch -d '1 hour ago' "${MILL_DOWNLOAD_PATH}/.expire_latest" 2>/dev/null || (
63+
# we might be on OSX or BSD which don't have -d option for touch
64+
# but probably a -A [-][[hh]mm]SS
65+
touch "${MILL_DOWNLOAD_PATH}/.expire_latest"; touch -A -010000 "${MILL_DOWNLOAD_PATH}/.expire_latest"
66+
) || (
67+
# in case we still failed, we retry the first touch command with the intention
68+
# to show the (previously suppressed) error message
69+
LANG=C touch -d '1 hour ago' "${MILL_DOWNLOAD_PATH}/.expire_latest"
70+
)
71+
72+
# POSIX shell variant of bash's -nt operator, see https://unix.stackexchange.com/a/449744/6993
73+
# if [ "${MILL_DOWNLOAD_PATH}/.latest" -nt "${MILL_DOWNLOAD_PATH}/.expire_latest" ] ; then
74+
if [ -n "$(find -L "${MILL_DOWNLOAD_PATH}/.latest" -prune -newer "${MILL_DOWNLOAD_PATH}/.expire_latest")" ]; then
5975
# we know a current latest version
60-
MILL_VERSION="$(head -n 1 ${MILL_DOWNLOAD_PATH}/.latest 2> /dev/null)"
76+
MILL_VERSION=$(head -n 1 "${MILL_DOWNLOAD_PATH}"/.latest 2> /dev/null)
6177
fi
6278

63-
if [ "x${MILL_VERSION}" = "x" ] ; then
79+
if [ -z "${MILL_VERSION}" ] ; then
6480
# we don't know a current latest version
65-
echo "Retrieving latest mill version ..."
66-
LANG=C curl -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"
67-
MILL_VERSION="$(head -n 1 ${MILL_DOWNLOAD_PATH}/.latest 2> /dev/null)"
81+
echo "Retrieving latest mill version ..." 1>&2
82+
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"
83+
MILL_VERSION=$(head -n 1 "${MILL_DOWNLOAD_PATH}"/.latest 2> /dev/null)
6884
fi
6985

70-
if [ "x${MILL_VERSION}" = "x" ] ; then
86+
if [ -z "${MILL_VERSION}" ] ; then
7187
# Last resort
7288
MILL_VERSION="${DEFAULT_MILL_VERSION}"
73-
echo "Falling back to hardcoded mill version ${MILL_VERSION}"
89+
echo "Falling back to hardcoded mill version ${MILL_VERSION}" 1>&2
7490
else
75-
echo "Using mill version ${MILL_VERSION}"
91+
echo "Using mill version ${MILL_VERSION}" 1>&2
7692
fi
7793
fi
7894

7995
MILL="${MILL_DOWNLOAD_PATH}/${MILL_VERSION}"
8096

97+
try_to_use_system_mill() {
98+
MILL_IN_PATH="$(command -v mill || true)"
99+
100+
if [ -z "${MILL_IN_PATH}" ]; then
101+
return
102+
fi
103+
104+
UNIVERSAL_SCRIPT_MAGIC="@ 2>/dev/null # 2>nul & echo off & goto BOF"
105+
106+
if ! head -c 128 "${MILL_IN_PATH}" | grep -qF "${UNIVERSAL_SCRIPT_MAGIC}"; then
107+
if [ -n "${MILLW_VERBOSE}" ]; then
108+
echo "Could not determine mill version of ${MILL_IN_PATH}, as it does not start with the universal script magic2" 1>&2
109+
fi
110+
return
111+
fi
112+
113+
# Roughly the size of the universal script.
114+
MILL_VERSION_SEARCH_RANGE="2403"
115+
MILL_IN_PATH_VERSION=$(head -c "${MILL_VERSION_SEARCH_RANGE}" "${MILL_IN_PATH}" |\
116+
sed -n 's/^.*-DMILL_VERSION=\([^\s]*\) .*$/\1/p' |\
117+
head -n 1)
118+
119+
if [ -z "${MILL_IN_PATH_VERSION}" ]; then
120+
echo "Could not determine mill version, even though ${MILL_IN_PATH} has the universal script magic" 1>&2
121+
return
122+
fi
123+
124+
if [ "${MILL_IN_PATH_VERSION}" = "${MILL_VERSION}" ]; then
125+
MILL="${MILL_IN_PATH}"
126+
fi
127+
}
128+
try_to_use_system_mill
129+
81130
# If not already downloaded, download it
82131
if [ ! -s "${MILL}" ] ; then
83-
132+
84133
# support old non-XDG download dir
85134
MILL_OLD_DOWNLOAD_PATH="${HOME}/.mill/download"
86135
OLD_MILL="${MILL_OLD_DOWNLOAD_PATH}/${MILL_VERSION}"
87136
if [ -x "${OLD_MILL}" ] ; then
88137
MILL="${OLD_MILL}"
89138
else
90-
VERSION_PREFIX="$(echo -n $MILL_VERSION | cut -b -4)"
139+
VERSION_PREFIX="$(echo $MILL_VERSION | cut -b -4)"
91140
case $VERSION_PREFIX in
92141
0.0. | 0.1. | 0.2. | 0.3. | 0.4. )
93142
DOWNLOAD_SUFFIX=""
@@ -98,10 +147,11 @@ if [ ! -s "${MILL}" ] ; then
98147
esac
99148
unset VERSION_PREFIX
100149

101-
DOWNLOAD_FILE=$(mktemp mill.XXXX)
150+
DOWNLOAD_FILE=$(mktemp mill.XXXXXX)
102151
# TODO: handle command not found
103-
echo "Downloading mill ${MILL_VERSION} from ${MILL_REPO_URL}/releases ..."
104-
curl -L -o "${DOWNLOAD_FILE}" "${MILL_REPO_URL}/releases/download/${MILL_VERSION%%-*}/${MILL_VERSION}${DOWNLOAD_SUFFIX}"
152+
echo "Downloading mill ${MILL_VERSION} from ${MILL_REPO_URL}/releases ..." 1>&2
153+
MILL_VERSION_TAG=$(echo $MILL_VERSION | sed -E 's/([^-]+)(-M[0-9]+)?(-.*)?/\1\2/')
154+
${CURL_CMD} -f -L -o "${DOWNLOAD_FILE}" "${MILL_REPO_URL}/releases/download/${MILL_VERSION_TAG}/${MILL_VERSION}${DOWNLOAD_SUFFIX}"
105155
chmod +x "${DOWNLOAD_FILE}"
106156
mkdir -p "${MILL_DOWNLOAD_PATH}"
107157
mv "${DOWNLOAD_FILE}" "${MILL}"
@@ -115,6 +165,7 @@ unset MILL_DOWNLOAD_PATH
115165
unset MILL_OLD_DOWNLOAD_PATH
116166
unset OLD_MILL
117167
unset MILL_VERSION
168+
unset MILL_VERSION_TAG
118169
unset MILL_REPO_URL
119170

120-
exec $MILL "$@"
171+
exec "${MILL}" "$@"

Diff for: mill.bat

+16-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ rem You can give the required mill version with --mill-version parameter
55
rem If no version is given, it falls back to the value of DEFAULT_MILL_VERSION
66
rem
77
rem Project page: https://github.com/lefou/millw
8+
rem Script Version: 0.4.2
89
rem
910
rem If you want to improve this script, please also contribute your changes back!
1011
rem
@@ -14,7 +15,7 @@ rem setlocal seems to be unavailable on Windows 95/98/ME
1415
rem but I don't think we need to support them in 2019
1516
setlocal enabledelayedexpansion
1617

17-
set "DEFAULT_MILL_VERSION=0.9.5"
18+
set "DEFAULT_MILL_VERSION=0.10.0"
1819

1920
set "MILL_REPO_URL=https://github.com/com-lihaoyi/mill"
2021

@@ -25,9 +26,9 @@ if [%~1%]==[--mill-version] (
2526
set MILL_VERSION=%~2%
2627
set "STRIP_VERSION_PARAMS=true"
2728
) else (
28-
echo You specified --mill-version without a version.
29-
echo Please provide a version that matches one provided on
30-
echo %MILL_REPO_URL%/releases
29+
echo You specified --mill-version without a version. 1>&2
30+
echo Please provide a version that matches one provided on 1>&2
31+
echo %MILL_REPO_URL%/releases 1>&2
3132
exit /b 1
3233
)
3334
)
@@ -57,31 +58,36 @@ if not exist "%MILL%" (
5758
if [!VERSION_PREFIX!]==[0.4.] set DOWNLOAD_SUFFIX=
5859
set VERSION_PREFIX=
5960

60-
for /F "delims=-" %%A in ("!MILL_VERSION!") do (
61-
set MILL_BASE_VERSION=%%A
61+
for /F "delims=- tokens=1" %%A in ("!MILL_VERSION!") do set MILL_VERSION_BASE=%%A
62+
for /F "delims=- tokens=2" %%A in ("!MILL_VERSION!") do set MILL_VERSION_MILESTONE=%%A
63+
set VERSION_MILESTONE_START=!MILL_VERSION_MILESTONE:~0,1!
64+
if [!VERSION_MILESTONE_START!]==[M] (
65+
set MILL_VERSION_TAG="!MILL_VERSION_BASE!-!MILL_VERSION_MILESTONE!"
66+
) else (
67+
set MILL_VERSION_TAG=!MILL_VERSION_BASE!
6268
)
6369

6470
rem there seems to be no way to generate a unique temporary file path (on native Windows)
6571
set DOWNLOAD_FILE=%MILL%.tmp
6672

67-
set DOWNLOAD_URL=%MILL_REPO_URL%/releases/download/!MILL_BASE_VERSION!/!MILL_VERSION!!DOWNLOAD_SUFFIX!
73+
set DOWNLOAD_URL=%MILL_REPO_URL%/releases/download/!MILL_VERSION_TAG!/!MILL_VERSION!!DOWNLOAD_SUFFIX!
6874

69-
echo Downloading mill %MILL_VERSION% from %MILL_REPO_URL%/releases ...
75+
echo Downloading mill %MILL_VERSION% from %MILL_REPO_URL%/releases ... 1>&2
7076

7177
if not exist "%MILL_DOWNLOAD_PATH%" mkdir "%MILL_DOWNLOAD_PATH%"
7278
rem curl is bundled with recent Windows 10
7379
rem but I don't think we can expect all the users to have it in 2019
7480
where /Q curl
7581
if %ERRORLEVEL% EQU 0 (
76-
curl -L "!DOWNLOAD_URL!" -o "!DOWNLOAD_FILE!"
82+
curl -f -L "!DOWNLOAD_URL!" -o "!DOWNLOAD_FILE!"
7783
) else (
7884
rem bitsadmin seems to be available on Windows 7
7985
rem without /dynamic, github returns 403
8086
rem bitsadmin is sometimes needlessly slow but it looks better with /priority foreground
8187
bitsadmin /transfer millDownloadJob /dynamic /priority foreground "!DOWNLOAD_URL!" "!DOWNLOAD_FILE!"
8288
)
8389
if not exist "!DOWNLOAD_FILE!" (
84-
echo Could not download mill %MILL_VERSION%
90+
echo Could not download mill %MILL_VERSION% 1>&2
8591
exit /b 1
8692
)
8793

0 commit comments

Comments
 (0)