99
1010RUST_VERSION=" ${VERSION:- " latest" } "
1111RUSTUP_PROFILE=" ${PROFILE:- " minimal" } "
12+ RUSTUP_TARGETS=" ${TARGETS:- " " } "
1213
1314export CARGO_HOME=" ${CARGO_HOME:- " /usr/local/cargo" } "
1415export RUSTUP_HOME=" ${RUSTUP_HOME:- " /usr/local/rustup" } "
@@ -19,7 +20,9 @@ UPDATE_RUST="${UPDATE_RUST:-"false"}"
1920set -e
2021
2122# Clean up
22- rm -rf /var/lib/apt/lists/*
23+ if [ " $( ls -1 /var/lib/apt/lists/ | wc -l) " -gt -1 ]; then
24+ rm -rf /var/lib/apt/lists/*
25+ fi
2326
2427if [ " $( id -u) " -ne 0 ]; then
2528 echo -e ' Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
@@ -36,15 +39,15 @@ if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
3639 USERNAME=" "
3740 POSSIBLE_USERS=(" vscode" " node" " codespace" " $( awk -v val=1000 -F " :" ' $3==val{print $1}' /etc/passwd) " )
3841 for CURRENT_USER in " ${POSSIBLE_USERS[@]} " ; do
39- if id -u ${CURRENT_USER} > /dev/null 2>&1 ; then
42+ if id -u " ${CURRENT_USER} " > /dev/null 2>&1 ; then
4043 USERNAME=${CURRENT_USER}
4144 break
4245 fi
4346 done
4447 if [ " ${USERNAME} " = " " ]; then
4548 USERNAME=root
4649 fi
47- elif [ " ${USERNAME} " = " none" ] || ! id -u ${USERNAME} > /dev/null 2>&1 ; then
50+ elif [ " ${USERNAME} " = " none" ] || ! id -u " ${USERNAME} " > /dev/null 2>&1 ; then
4851 USERNAME=root
4952fi
5053
@@ -56,7 +59,7 @@ find_version_from_git_tags() {
5659 local repository=$2
5760 local prefix=${3:- " tags/v" }
5861 local separator=${4:- " ." }
59- local last_part_optional=${5:- " false" }
62+ local last_part_optional=${5:- " false" }
6063 if [ " $( echo " ${requested_version} " | grep -o " ." | wc -l) " != " 2" ]; then
6164 local escaped_separator=${separator// ./ \\ .}
6265 local last_part
@@ -66,7 +69,7 @@ find_version_from_git_tags() {
6669 last_part=" ${escaped_separator} [0-9]+"
6770 fi
6871 local regex=" ${prefix} \\ K[0-9]+${escaped_separator} [0-9]+${last_part} $"
69- local version_list=" $( git ls-remote --tags ${repository} | grep -oP " ${regex} " | tr -d ' ' | tr " ${separator} " " ." | sort -rV) "
72+ local version_list=" $( git ls-remote --tags " ${repository} " | grep -oP " ${regex} " | tr -d ' ' | tr " ${separator} " " ." | sort -rV) "
7073 if [ " ${requested_version} " = " latest" ] || [ " ${requested_version} " = " current" ] || [ " ${requested_version} " = " lts" ]; then
7174 declare -g ${variable_name} =" $( echo " ${version_list} " | head -n 1) "
7275 else
@@ -89,13 +92,13 @@ check_nightly_version_formatting() {
8992
9093 local version_date=$( echo ${requested_version} | sed -e " s/^nightly-//" )
9194
92- date -d ${version_date} & > /dev/null
93- if [ $? != 0 ] ; then
95+
96+ if ! date -d " ${version_date} " & > /dev/null ; then
9497 echo -e " Invalid ${variable_name} value: ${requested_version} \nNightly version should be in the format nightly-YYYY-MM-DD" >&2
9598 exit 1
9699 fi
97100
98- if [ $( date -d ${version_date} +%s) -ge $( date +%s) ]; then
101+ if [ " $( date -d " ${version_date} " +%s) " -ge " $( date +%s) " ]; then
99102 echo -e " Invalid ${variable_name} value: ${requested_version} \nNightly version should not exceed current date" >&2
100103 exit 1
101104 fi
141144architecture=" $( dpkg --print-architecture) "
142145download_architecture=" ${architecture} "
143146case ${download_architecture} in
144- amd64)
147+ amd64)
145148 download_architecture=" x86_64"
146149 ;;
147- arm64)
150+ arm64)
148151 download_architecture=" aarch64"
149152 ;;
150153 * ) echo " (!) Architecture ${architecture} not supported."
154157
155158# Install Rust
156159umask 0002
157- if ! cat /etc/group | grep -e " ^rustlang:" > /dev/null 2>&1 ; then
160+ if ! grep -e " ^rustlang:" /etc/group > /dev/null 2>&1 ; then
158161 groupadd -r rustlang
159162fi
160163usermod -a -G rustlang " ${USERNAME} "
172175 fi
173176
174177 is_nightly=0
175- echo ${RUST_VERSION} | grep -q " nightly" || is_nightly=$?
178+ echo " ${RUST_VERSION} " | grep -q " nightly" || is_nightly=$?
176179 if [ $is_nightly = 0 ]; then
177180 check_nightly_version_formatting RUST_VERSION
178181 else
189192 cp /tmp/rustup/target/${download_architecture} -unknown-linux-gnu/release/rustup-init /tmp/rustup/rustup-init
190193 sha256sum -c rustup-init.sha256
191194 chmod +x target/${download_architecture} -unknown-linux-gnu/release/rustup-init
192- target/${download_architecture} -unknown-linux-gnu/release/rustup-init -y --no-modify-path --profile ${RUSTUP_PROFILE} ${default_toolchain_arg}
195+ target/${download_architecture} -unknown-linux-gnu/release/rustup-init -y --no-modify-path --profile " ${RUSTUP_PROFILE} " ${default_toolchain_arg}
193196 cd ~
194197 rm -rf /tmp/rustup
195198fi
202205echo " Installing common Rust dependencies..."
203206rustup component add rls rust-analysis rust-src rustfmt clippy 2>&1
204207
208+ if [ -n " ${RUSTUP_TARGETS} " ]; then
209+ IFS=' ,' read -ra targets <<< " ${RUSTUP_TARGETS}"
210+ for target in " ${targets[@]} " ; do
211+ echo " Installing additional Rust target $target "
212+ rustup target add " $target " 2>&1
213+ done
214+ fi
215+
205216# Add CARGO_HOME, RUSTUP_HOME and bin directory into bashrc/zshrc files (unless disabled)
206217updaterc " $( cat << EOF
207218export RUSTUP_HOME="${RUSTUP_HOME} "
0 commit comments