Skip to content

Commit

Permalink
[cargo.sh] Auto-install toolchains and components (#577)
Browse files Browse the repository at this point in the history
`cargo.sh` detects missing Rust toolchains and the `rust-src` component.
When missing, it offers to install them automatically via `rustup`.

Closes #564
  • Loading branch information
joshlf committed Oct 31, 2023
1 parent 03dac07 commit 011688c
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions cargo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ function get-rustflags {
[ "$1" == nightly ] && echo "--cfg __INTERNAL_USE_ONLY_NIGHLTY_FEATURES_IN_TESTS"
}

function prompt {
PROMPT="$1"
YES="$2"
while true; do
read -p "$PROMPT " yn
case "$yn" in
[Yy]) $YES; return $?; ;;
[Nn]) return 1; ;;
*) break; ;;
esac
done
}

case "$1" in
# cargo.sh --version <toolchain-name>
--version)
Expand All @@ -81,6 +94,17 @@ case "$1" in
# cargo.sh +<toolchain-name> [...]
+*)
TOOLCHAIN="$(lookup-version ${1:1})"

cargo "+$TOOLCHAIN" version &>/dev/null && \
rustup "+$TOOLCHAIN" component list | grep '^rust-src (installed)$' >/dev/null || {
echo "[cargo.sh] missing either toolchain '$TOOLCHAIN' or component 'rust-src'" >&2
# If we're running in a GitHub action, then it's better to bail than to
# hang waiting for input we're never going to get.
[ -z ${GITHUB_RUN_ID+x} ] || exit 1
prompt "[cargo.sh] would you like to install toolchain '$TOOLCHAIN' and component 'rust-src' via 'rustup'?" \
"rustup toolchain install $TOOLCHAIN -c rust-src"
} || exit 1

RUSTFLAGS="$(get-rustflags ${1:1}) $RUSTFLAGS" cargo "+$TOOLCHAIN" ${@:2}
;;
*)
Expand Down

0 comments on commit 011688c

Please sign in to comment.