Skip to content

Commit 32c187a

Browse files
committed
rustup: add caching of old nightlies
1 parent b59d4e7 commit 32c187a

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

src/etc/rustup.sh

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ validate_opt() {
230230
}
231231

232232
create_tmp_dir() {
233-
local TMP_DIR=./rustup-tmp-install
233+
local TMP_DIR=`pwd`/rustup-tmp-install
234234

235235
rm -Rf "${TMP_DIR}"
236236
need_ok "failed to remove temporary installation directory"
@@ -271,6 +271,8 @@ VAL_OPTIONS=""
271271
flag uninstall "only uninstall from the installation prefix"
272272
valopt prefix "" "set installation prefix"
273273
opt cargo 1 "install cargo with rust"
274+
valopt date "" "use the YYYY-MM-DD nightly instead of the current nightly"
275+
flag save "save the downloaded nightlies to ~/.rustup"
274276

275277
if [ $HELP -eq 1 ]
276278
then
@@ -418,6 +420,21 @@ CFG_TMP_DIR=$(mktemp -d 2>/dev/null \
418420
|| mktemp -d -t 'rustup-tmp-install' 2>/dev/null \
419421
|| create_tmp_dir)
420422

423+
# If we're saving nightlies and we didn't specify which one, grab todays.
424+
# Otherwise we'll use the latest version.
425+
if [ -n "${CFG_SAVE}" -a -z "${CFG_DATE}" ];
426+
then
427+
CFG_DATE=`date "+%Y-%m-%d"`
428+
fi
429+
430+
# If we're saving our nightlies, put them in $HOME/.rustup.
431+
if [ -n "${CFG_SAVE}" ]
432+
then
433+
CFG_DOWNLOAD_DIR="${HOME}/.rustup/${CFG_DATE}"
434+
else
435+
CFG_DOWNLOAD_DIR="${CFG_TMP_DIR}"
436+
fi
437+
421438
RUST_URL="https://static.rust-lang.org/dist"
422439
RUST_PACKAGE_NAME=rust-nightly
423440
RUST_PACKAGE_NAME_AND_TRIPLE="${RUST_PACKAGE_NAME}-${HOST_TRIPLE}"
@@ -432,6 +449,13 @@ CARGO_TARBALL_NAME="${CARGO_PACKAGE_NAME_AND_TRIPLE}.tar.gz"
432449
CARGO_LOCAL_INSTALL_DIR="${CFG_TMP_DIR}/${CARGO_PACKAGE_NAME_AND_TRIPLE}"
433450
CARGO_LOCAL_INSTALL_SCRIPT="${CARGO_LOCAL_INSTALL_DIR}/install.sh"
434451

452+
# add a date suffix if we want a particular nighly.
453+
if [ -n "${CFG_DATE}" ];
454+
then
455+
RUST_URL="${RUST_URL}/${CFG_DATE}"
456+
CARGO_URL="${CARGO_URL}/${CFG_DATE}"
457+
fi
458+
435459
verify_hash() {
436460
remote_sha256="$1"
437461
local_file="$2"
@@ -460,19 +484,25 @@ verify_hash() {
460484
fi
461485
}
462486

463-
# Fetch the package.
487+
# Fetch the package. Optionally caches the tarballs.
464488
download_package() {
465489
remote_tarball="$1"
466490
local_tarball="$2"
467491
remote_sha256="${remote_tarball}.sha256"
468492

469-
msg "Downloading ${remote_tarball} to ${local_tarball}"
493+
# Check if we've already downloaded this file.
494+
if [ ! -e "${local_tarball}" ]; then
495+
msg "Downloading ${remote_tarball} to ${local_tarball}"
470496

471-
"${CFG_CURL}" -f -o "${local_tarball}" "${remote_tarball}"
472-
if [ $? -ne 0 ]
473-
then
474-
rm -Rf "${CFG_TMP_DIR}"
475-
err "failed to download installer"
497+
mkdir -p "${CFG_DOWNLOAD_DIR}"
498+
need_ok "failed to create create download directory"
499+
500+
"${CFG_CURL}" -f -o "${local_tarball} "${remote_tarball}"
501+
if [ $? -ne 0 ]
502+
then
503+
rm -Rf "${CFG_TMP_DIR}"
504+
err "failed to download installer"
505+
fi
476506
fi
477507
478508
verify_hash "${remote_sha256}" "${local_tarball}"
@@ -482,9 +512,10 @@ download_package() {
482512
install_package() {
483513
tarball_name="$1"
484514
install_script="$2"
515+
local_tarball="${CFG_DOWNLOAD_DIR}/${tarball_name}"
485516
486517
msg "Extracting ${tarball_name}"
487-
(cd "${CFG_TMP_DIR}" && "${CFG_TAR}" -xzf "${tarball_name}")
518+
(cd "${CFG_TMP_DIR}" && "${CFG_TAR}" -xvf "${local_tarball}")
488519
if [ $? -ne 0 ]; then
489520
rm -Rf "${CFG_TMP_DIR}"
490521
err "failed to unpack installer"

0 commit comments

Comments
 (0)