2019-03-07 02:16:31 +07:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2020-08-04 09:51:02 +07:00
|
|
|
# Compare two version strings, v1 and v2, passed as arguments (e.g 1.31 and
|
|
|
|
# 1.33.0). Returns 1 if v1 > v2, 0 if v1 == v2, 2 if v1 < v2.
|
|
|
|
compare_versions() {
|
|
|
|
[ "$1" == "$2" ] && return 0
|
|
|
|
declare -a v1=() v2=()
|
|
|
|
mapfile -d . -t v1 < <(echo "$1")
|
|
|
|
mapfile -d . -t v2 < <(echo "$1")
|
|
|
|
declare -ir max_len=$(( ${#v1[@]} > ${#v2[@]} ? ${#v1[@]} : ${#v2[@]} ))
|
|
|
|
for i in $(seq 0 "$max_len")
|
|
|
|
do
|
|
|
|
# Fill empty fields with zeros.
|
|
|
|
[ -z "${v1[$i]}" ] && v1["$i"]=0
|
|
|
|
[ -z "${v2[$i]}" ] && v2["$i"]=0
|
|
|
|
# Compare the fields.
|
|
|
|
[ ${v1[$i]} -gt ${v2[$i]} ] && return 1
|
|
|
|
[ ${v1[$i]} -lt ${v2[$i]} ] && return 2
|
|
|
|
done
|
|
|
|
return 0
|
|
|
|
}
|
2019-03-07 02:16:31 +07:00
|
|
|
|
2020-08-04 09:51:02 +07:00
|
|
|
# Return 0 if the argument passed represents a valid command on the machine,
|
|
|
|
# 1 otherwise.
|
|
|
|
verify_dependency() {
|
|
|
|
declare -r package="$1"
|
|
|
|
if [ -x "$(command -v "$package")" ]
|
|
|
|
then
|
|
|
|
echo "SUCCESS: $package is installed"
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
echo "ERROR: $package does not seem to be installed." >&2
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
2019-03-07 02:16:31 +07:00
|
|
|
|
2020-08-04 09:51:02 +07:00
|
|
|
declare Clippy Py RustVersion Version
|
|
|
|
declare -r CargoBin="${CARGO_HOME:-$HOME/.cargo}/bin"
|
|
|
|
declare -r Path=${1:-rustlings/}
|
|
|
|
declare -r MinRustVersion=1.31
|
2019-03-07 02:16:31 +07:00
|
|
|
|
2020-08-04 09:51:02 +07:00
|
|
|
echo "Let's get you set up with Rustlings!"
|
2019-03-07 02:16:31 +07:00
|
|
|
|
2020-08-04 09:51:02 +07:00
|
|
|
# Verify that the required dependencies are installed.
|
|
|
|
echo "Checking requirements..."
|
|
|
|
for dep in git rustc cargo curl
|
|
|
|
do
|
|
|
|
verify_dependency "$dep" || exit 1
|
|
|
|
done
|
|
|
|
# Look up Python installations; start with 3 and fallback to 2 if required.
|
2020-04-30 02:08:34 +07:00
|
|
|
if [ -x "$(command -v python3)" ]
|
|
|
|
then
|
2020-08-04 09:51:02 +07:00
|
|
|
Py="$(command -v python3)"
|
2020-04-30 02:08:34 +07:00
|
|
|
elif [ -x "$(command -v python)" ]
|
|
|
|
then
|
2020-08-04 09:51:02 +07:00
|
|
|
Py="$(command -v python)"
|
2020-04-30 02:08:34 +07:00
|
|
|
elif [ -x "$(command -v python2)" ]
|
|
|
|
then
|
2020-08-04 09:51:02 +07:00
|
|
|
Py="$(command -v python2)"
|
2020-04-30 02:08:34 +07:00
|
|
|
else
|
2020-08-04 09:51:02 +07:00
|
|
|
echo ERROR: No working python installation was found. >&2
|
|
|
|
echo Please install python and add it to the PATH variable. >&2
|
2020-04-30 02:08:34 +07:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-08-04 09:51:02 +07:00
|
|
|
# Ensure the installed Rust compiler is sufficiently up-to-date.
|
2019-03-20 18:18:39 +07:00
|
|
|
RustVersion=$(rustc --version | cut -d " " -f 2)
|
2020-08-04 09:51:02 +07:00
|
|
|
compare_versions "$RustVersion" "$MinRustVersion"
|
2019-03-20 18:18:39 +07:00
|
|
|
if [ $? -eq 2 ]
|
|
|
|
then
|
2020-08-04 09:51:02 +07:00
|
|
|
echo ERROR: Rust version is too old: "$RustVersion" - needs at least \
|
|
|
|
"$MinRustVersion" >&2
|
|
|
|
echo Please update Rust with \`rustup update\`. >&2
|
2019-03-20 18:18:39 +07:00
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "SUCCESS: Rust is up to date"
|
|
|
|
fi
|
|
|
|
|
2020-08-04 09:51:02 +07:00
|
|
|
# Clone the Git repository.
|
|
|
|
echo "Cloning Rustlings into $Path..."
|
|
|
|
git clone -q https://github.com/rust-lang/rustlings "$Path"
|
|
|
|
cd "$Path" || exit 1
|
|
|
|
|
|
|
|
# Determine which version of Rustlings to install.
|
|
|
|
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 [ -z "$Version" ]
|
2020-06-06 03:33:14 +07:00
|
|
|
then
|
2020-08-04 09:51:02 +07:00
|
|
|
echo The latest tag version could not be fetched remotely.
|
|
|
|
echo Using the local git repository...
|
|
|
|
Version=$(ls -t .git/refs/tags/ | head --lines=1)
|
|
|
|
if [ -z "$Version" ]
|
2020-06-06 03:33:14 +07:00
|
|
|
then
|
2020-08-04 09:51:02 +07:00
|
|
|
echo No valid tag version found
|
|
|
|
echo Rustlings will be installed using the main branch
|
2020-06-14 19:48:51 +07:00
|
|
|
Version="main"
|
2020-06-06 03:33:14 +07:00
|
|
|
else
|
2020-08-04 09:51:02 +07:00
|
|
|
Version="tags/$Version"
|
2020-06-06 03:33:14 +07:00
|
|
|
fi
|
|
|
|
else
|
2020-08-04 09:51:02 +07:00
|
|
|
Version="tags/$Version"
|
2020-06-06 03:33:14 +07:00
|
|
|
fi
|
|
|
|
|
2020-08-04 09:51:02 +07:00
|
|
|
# Install the selected version of Rustlings.
|
2020-02-28 01:19:31 +07:00
|
|
|
echo "Checking out version $Version..."
|
2020-08-04 09:51:02 +07:00
|
|
|
git checkout -q "$Version"
|
2019-03-07 02:16:31 +07:00
|
|
|
echo "Installing the 'rustlings' executable..."
|
2020-02-28 01:19:31 +07:00
|
|
|
cargo install --force --path .
|
2019-03-07 02:16:31 +07:00
|
|
|
|
2020-08-04 09:51:02 +07:00
|
|
|
# Verify that the Rustlings installation was successful.
|
2019-06-07 08:59:59 +07:00
|
|
|
if ! [ -x "$(command -v rustlings)" ]
|
2019-03-07 02:16:31 +07:00
|
|
|
then
|
2020-08-04 09:51:02 +07:00
|
|
|
echo WARNING: Please ensure "'$CargoBin'" is in your PATH environment \
|
|
|
|
variable!
|
2020-02-14 21:25:03 +07:00
|
|
|
fi
|
|
|
|
|
2020-08-04 09:51:02 +07:00
|
|
|
# Ensure Clippy is installed (due to a bug in Cargo, this must be done with
|
|
|
|
# Rustup: https://github.com/rust-lang/rustup/issues/1514).
|
2020-02-14 21:25:03 +07:00
|
|
|
Clippy=$(rustup component list | grep "clippy" | grep "installed")
|
|
|
|
if [ -z "$Clippy" ]
|
|
|
|
then
|
|
|
|
echo "Installing the 'cargo-clippy' executable..."
|
|
|
|
rustup component add clippy
|
2019-03-07 02:16:31 +07:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo "All done! Run 'rustlings' to get started."
|