9
9
10
10
RUST_VERSION=" ${VERSION:- " latest" } "
11
11
RUSTUP_PROFILE=" ${PROFILE:- " minimal" } "
12
+ RUSTUP_TARGETS=" ${TARGETS:- " " } "
12
13
13
14
export CARGO_HOME=" ${CARGO_HOME:- " /usr/local/cargo" } "
14
15
export RUSTUP_HOME=" ${RUSTUP_HOME:- " /usr/local/rustup" } "
@@ -19,7 +20,9 @@ UPDATE_RUST="${UPDATE_RUST:-"false"}"
19
20
set -e
20
21
21
22
# 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
23
26
24
27
if [ " $( id -u) " -ne 0 ]; then
25
28
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
36
39
USERNAME=" "
37
40
POSSIBLE_USERS=(" vscode" " node" " codespace" " $( awk -v val=1000 -F " :" ' $3==val{print $1}' /etc/passwd) " )
38
41
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
40
43
USERNAME=${CURRENT_USER}
41
44
break
42
45
fi
43
46
done
44
47
if [ " ${USERNAME} " = " " ]; then
45
48
USERNAME=root
46
49
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
48
51
USERNAME=root
49
52
fi
50
53
@@ -56,7 +59,7 @@ find_version_from_git_tags() {
56
59
local repository=$2
57
60
local prefix=${3:- " tags/v" }
58
61
local separator=${4:- " ." }
59
- local last_part_optional=${5:- " false" }
62
+ local last_part_optional=${5:- " false" }
60
63
if [ " $( echo " ${requested_version} " | grep -o " ." | wc -l) " != " 2" ]; then
61
64
local escaped_separator=${separator// ./ \\ .}
62
65
local last_part
@@ -66,7 +69,7 @@ find_version_from_git_tags() {
66
69
last_part=" ${escaped_separator} [0-9]+"
67
70
fi
68
71
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) "
70
73
if [ " ${requested_version} " = " latest" ] || [ " ${requested_version} " = " current" ] || [ " ${requested_version} " = " lts" ]; then
71
74
declare -g ${variable_name} =" $( echo " ${version_list} " | head -n 1) "
72
75
else
@@ -89,13 +92,13 @@ check_nightly_version_formatting() {
89
92
90
93
local version_date=$( echo ${requested_version} | sed -e " s/^nightly-//" )
91
94
92
- date -d ${version_date} & > /dev/null
93
- if [ $? != 0 ] ; then
95
+
96
+ if ! date -d " ${version_date} " & > /dev/null ; then
94
97
echo -e " Invalid ${variable_name} value: ${requested_version} \nNightly version should be in the format nightly-YYYY-MM-DD" >&2
95
98
exit 1
96
99
fi
97
100
98
- if [ $( date -d ${version_date} +%s) -ge $( date +%s) ]; then
101
+ if [ " $( date -d " ${version_date} " +%s) " -ge " $( date +%s) " ]; then
99
102
echo -e " Invalid ${variable_name} value: ${requested_version} \nNightly version should not exceed current date" >&2
100
103
exit 1
101
104
fi
141
144
architecture=" $( dpkg --print-architecture) "
142
145
download_architecture=" ${architecture} "
143
146
case ${download_architecture} in
144
- amd64)
147
+ amd64)
145
148
download_architecture=" x86_64"
146
149
;;
147
- arm64)
150
+ arm64)
148
151
download_architecture=" aarch64"
149
152
;;
150
153
* ) echo " (!) Architecture ${architecture} not supported."
154
157
155
158
# Install Rust
156
159
umask 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
158
161
groupadd -r rustlang
159
162
fi
160
163
usermod -a -G rustlang " ${USERNAME} "
172
175
fi
173
176
174
177
is_nightly=0
175
- echo ${RUST_VERSION} | grep -q " nightly" || is_nightly=$?
178
+ echo " ${RUST_VERSION} " | grep -q " nightly" || is_nightly=$?
176
179
if [ $is_nightly = 0 ]; then
177
180
check_nightly_version_formatting RUST_VERSION
178
181
else
189
192
cp /tmp/rustup/target/${download_architecture} -unknown-linux-gnu/release/rustup-init /tmp/rustup/rustup-init
190
193
sha256sum -c rustup-init.sha256
191
194
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}
193
196
cd ~
194
197
rm -rf /tmp/rustup
195
198
fi
202
205
echo " Installing common Rust dependencies..."
203
206
rustup component add rls rust-analysis rust-src rustfmt clippy 2>&1
204
207
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
+
205
216
# Add CARGO_HOME, RUSTUP_HOME and bin directory into bashrc/zshrc files (unless disabled)
206
217
updaterc " $( cat << EOF
207
218
export RUSTUP_HOME="${RUSTUP_HOME} "
0 commit comments