2019-03-06 22:16:31 +03:00
|
|
|
#!/usr/bin/env bash
|
2022-04-03 20:49:59 +03:00
|
|
|
set -euo pipefail
|
2019-03-06 22:16:31 +03:00
|
|
|
|
2023-10-18 21:59:58 +03:00
|
|
|
echo -e "\nLet's get you set up with Rustlings!"
|
2019-03-06 22:16:31 +03:00
|
|
|
|
|
|
|
echo "Checking requirements..."
|
2019-03-28 12:51:54 +03:00
|
|
|
if [ -x "$(command -v git)" ]
|
2019-03-06 22:16:31 +03:00
|
|
|
then
|
2019-03-28 13:53:29 +03:00
|
|
|
echo "SUCCESS: Git is installed"
|
|
|
|
else
|
2020-02-21 20:52:47 +03:00
|
|
|
echo "ERROR: Git does not seem to be installed."
|
2019-03-06 22:16:31 +03:00
|
|
|
echo "Please download Git using your package manager or over https://git-scm.com/!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-02-04 17:38:55 +03:00
|
|
|
if [ -x "$(command -v cc)" ]
|
|
|
|
then
|
|
|
|
echo "SUCCESS: cc is installed"
|
|
|
|
else
|
|
|
|
echo "ERROR: cc does not seem to be installed."
|
|
|
|
echo "Please download (g)cc using your package manager."
|
|
|
|
echo "OSX: xcode-select --install"
|
|
|
|
echo "Deb: sudo apt install gcc"
|
|
|
|
echo "Yum: sudo yum -y install gcc"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-05-17 12:24:39 +03:00
|
|
|
if [ -x "$(command -v rustup)" ]
|
|
|
|
then
|
|
|
|
echo "SUCCESS: rustup is installed"
|
|
|
|
else
|
|
|
|
echo "ERROR: rustup does not seem to be installed."
|
|
|
|
echo "Please download rustup using https://rustup.rs!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-03-28 12:51:54 +03:00
|
|
|
if [ -x "$(command -v rustc)" ]
|
2019-03-06 22:16:31 +03:00
|
|
|
then
|
2019-03-28 13:53:29 +03:00
|
|
|
echo "SUCCESS: Rust is installed"
|
|
|
|
else
|
2020-02-21 20:52:47 +03:00
|
|
|
echo "ERROR: Rust does not seem to be installed."
|
2022-05-17 12:24:39 +03:00
|
|
|
echo "Please download Rust using rustup!"
|
2019-03-06 22:16:31 +03:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-03-28 12:51:54 +03:00
|
|
|
if [ -x "$(command -v cargo)" ]
|
2019-03-06 22:16:31 +03:00
|
|
|
then
|
2019-03-28 13:53:29 +03:00
|
|
|
echo "SUCCESS: Cargo is installed"
|
|
|
|
else
|
2020-02-21 20:52:47 +03:00
|
|
|
echo "ERROR: Cargo does not seem to be installed."
|
2022-05-17 12:24:39 +03:00
|
|
|
echo "Please download Rust and Cargo using rustup!"
|
2019-03-06 22:16:31 +03:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-04-29 22:08:34 +03:00
|
|
|
# Look up python installations, starting with 3 with a fallback of 2
|
|
|
|
if [ -x "$(command -v python3)" ]
|
|
|
|
then
|
|
|
|
PY="$(command -v python3)"
|
|
|
|
elif [ -x "$(command -v python)" ]
|
|
|
|
then
|
|
|
|
PY="$(command -v python)"
|
|
|
|
elif [ -x "$(command -v python2)" ]
|
|
|
|
then
|
|
|
|
PY="$(command -v python2)"
|
|
|
|
else
|
|
|
|
echo "ERROR: No working python installation was found"
|
|
|
|
echo "Please install python and add it to the PATH variable"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-03-20 14:18:39 +03:00
|
|
|
# Function that compares two versions strings v1 and v2 given in arguments (e.g 1.31 and 1.33.0).
|
|
|
|
# Returns 1 if v1 > v2, 0 if v1 == v2, 2 if v1 < v2.
|
|
|
|
function vercomp() {
|
|
|
|
if [[ $1 == $2 ]]
|
|
|
|
then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
v1=( ${1//./ } )
|
|
|
|
v2=( ${2//./ } )
|
|
|
|
len1=${#v1[@]}
|
|
|
|
len2=${#v2[@]}
|
|
|
|
max_len=$len1
|
|
|
|
if [[ $max_len -lt $len2 ]]
|
|
|
|
then
|
|
|
|
max_len=$len2
|
|
|
|
fi
|
2022-05-03 19:46:49 +03:00
|
|
|
|
|
|
|
#pad right in short arr
|
|
|
|
if [[ len1 -gt len2 ]];
|
|
|
|
then
|
|
|
|
for ((i = len2; i < len1; i++));
|
|
|
|
do
|
|
|
|
v2[$i]=0
|
|
|
|
done
|
|
|
|
else
|
|
|
|
for ((i = len1; i < len2; i++));
|
|
|
|
do
|
|
|
|
v1[$i]=0
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2023-02-12 02:18:34 +03:00
|
|
|
for i in `seq 0 $((max_len-1))`
|
2019-03-20 14:18:39 +03:00
|
|
|
do
|
|
|
|
# Fill empty fields with zeros in v1
|
|
|
|
if [ -z "${v1[$i]}" ]
|
|
|
|
then
|
|
|
|
v1[$i]=0
|
|
|
|
fi
|
2019-03-20 14:21:15 +03:00
|
|
|
# And in v2
|
2019-03-20 14:18:39 +03:00
|
|
|
if [ -z "${v2[$i]}" ]
|
|
|
|
then
|
|
|
|
v2[$i]=0
|
|
|
|
fi
|
|
|
|
if [ ${v1[$i]} -gt ${v2[$i]} ]
|
|
|
|
then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
if [ ${v1[$i]} -lt ${v2[$i]} ]
|
|
|
|
then
|
|
|
|
return 2
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
RustVersion=$(rustc --version | cut -d " " -f 2)
|
2023-09-22 06:32:39 +03:00
|
|
|
MinRustVersion=1.70
|
2022-04-03 20:49:59 +03:00
|
|
|
vercomp "$RustVersion" $MinRustVersion || ec=$?
|
|
|
|
if [ ${ec:-0} -eq 2 ]
|
2019-03-20 14:18:39 +03:00
|
|
|
then
|
2020-02-21 20:52:47 +03:00
|
|
|
echo "ERROR: Rust version is too old: $RustVersion - needs at least $MinRustVersion"
|
2019-03-20 14:18:39 +03:00
|
|
|
echo "Please update Rust with 'rustup update'"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "SUCCESS: Rust is up to date"
|
|
|
|
fi
|
|
|
|
|
2020-02-27 21:19:31 +03:00
|
|
|
Path=${1:-rustlings/}
|
|
|
|
echo "Cloning Rustlings at $Path..."
|
2024-02-06 02:54:04 +03:00
|
|
|
git clone -q https://github.com/rust-lang/rustlings.git "$Path"
|
2020-02-27 21:19:31 +03:00
|
|
|
|
2022-04-03 20:49:59 +03:00
|
|
|
cd "$Path"
|
2020-06-05 23:33:14 +03:00
|
|
|
|
2023-02-26 19:00:57 +03:00
|
|
|
Version=$(curl -s https://api.github.com/repos/rust-lang/rustlings/releases/latest | ${PY} -c "import json,sys;obj=json.load(sys.stdin);print(obj['tag_name']) if 'tag_name' in obj else sys.exit(f\"Error: {obj['message']}\");")
|
2020-02-14 17:25:03 +03:00
|
|
|
CargoBin="${CARGO_HOME:-$HOME/.cargo}/bin"
|
2019-03-06 22:16:31 +03:00
|
|
|
|
2020-06-05 23:33:14 +03:00
|
|
|
if [[ -z ${Version} ]]
|
|
|
|
then
|
|
|
|
echo "The latest tag version could not be fetched remotely."
|
|
|
|
echo "Using the local git repository..."
|
|
|
|
Version=$(ls -tr .git/refs/tags/ | tail -1)
|
|
|
|
if [[ -z ${Version} ]]
|
|
|
|
then
|
|
|
|
echo "No valid tag version found"
|
2020-06-14 15:48:51 +03:00
|
|
|
echo "Rustlings will be installed using the main branch"
|
|
|
|
Version="main"
|
2020-06-05 23:33:14 +03:00
|
|
|
else
|
|
|
|
Version="tags/${Version}"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
Version="tags/${Version}"
|
|
|
|
fi
|
|
|
|
|
2020-02-27 21:19:31 +03:00
|
|
|
echo "Checking out version $Version..."
|
2020-06-05 23:33:14 +03:00
|
|
|
git checkout -q ${Version}
|
2020-02-27 21:19:31 +03:00
|
|
|
|
2019-03-06 22:16:31 +03:00
|
|
|
echo "Installing the 'rustlings' executable..."
|
2024-04-24 14:28:25 +03:00
|
|
|
cargo install --locked --force --path .
|
2019-03-06 22:16:31 +03:00
|
|
|
|
2019-06-07 04:59:59 +03:00
|
|
|
if ! [ -x "$(command -v rustlings)" ]
|
2019-03-06 22:16:31 +03:00
|
|
|
then
|
2020-02-14 17:25:03 +03:00
|
|
|
echo "WARNING: Please check that you have '$CargoBin' in your PATH environment variable!"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Checking whether Clippy is installed.
|
|
|
|
# Due to a bug in Cargo, this must be done with Rustup: https://github.com/rust-lang/rustup/issues/1514
|
|
|
|
Clippy=$(rustup component list | grep "clippy" | grep "installed")
|
|
|
|
if [ -z "$Clippy" ]
|
|
|
|
then
|
|
|
|
echo "Installing the 'cargo-clippy' executable..."
|
|
|
|
rustup component add clippy
|
2019-03-06 22:16:31 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo "All done! Run 'rustlings' to get started."
|