Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 274 lines (237 sloc) 6.58 KB
#!/bin/sh
# ——— Config ———
# If it exists, this script is executed first. Config variables can be set there.
if [ -f "$HOME/.config/run-rust" ]
then
source "$HOME/.config/run-rust"
fi
# Directory where Rust should be installed; this should be user-writable if you
# want to install/update rust easily, e.g. "$HOME/.local/share/rust".
if [ -z "$RUST_INST" ]; then
RUST_INST="$HOME/.local/share/rust"
fi
# Platform thing
if [ -z "$PLATFORM" ]; then
PLATFORM="x86_64-unknown-linux-gnu"
fi
# ——— Instructions ———
# 1. Put this script somewhere in your PATH, e.g. ~/.bin or ~/.local/bin
# (Note: it must be called run-rust, because a switch below checks if this is the name.)
#
# 2. In the same directory, create sym-links to it for each rust binary you want:
# for name in rustc rustdoc rust-gdb rust-lldb cargo; do ln -s run-rust $name; done
#
# 3. Check usage and install rust:
# run-rust
# run-rust install nightly
#
# 4. Run cargo/rustc/...
# rustc --version
# cargo new my_project
#
# 5. Temporarily use a different installed rust version via environment variable:
# export RUST_VER=1.14.0
# rustc --version
# or:
# RUST_VER=nightly rustc --version
# ——— Script functions ———
usage() {
echo "run-rust 2.0-rc1
To manage installs:
run-rust install VER [DATE]
VER should look like 1.14.0 or beta or nightly (you may have to look up the
latest stable version).
If a DATE (e.g. 2017-01-16) is given, download from the archives for this date
(e.g. run-rust install nightly 2017-01-16).
run-rust list
Shows installed versions.
run-rust set VER [NICK]
Set a rust version VER to be the default, or give it nickname NICK.
Rust rust-VER should be installed.
To delete installed versions, manually delete from $RUST_INST .
To select a (non-default) rust version, set the environment variable RUST_VER:
export RUST_VER=nightly
rustc --version
or:
RUST_VER=nightly rustc --version
run-rust run CMD ARGS
To run the rust binary CMD.
CMD ARGS
To run CMD (assuming a symlink exists in your path with the name CMD but
pointing to this script)."
}
list() {
if [ ! -e "$RUST_INST" ]; then
echo "No installation directory. Maybe run install first?" >&2
exit 1
fi
echo "Rust installations found in $RUST_INST :"
cd "$RUST_INST" && ls rust-*
echo "(You can omit rust- prefix when setting RUST_VER.)"
}
install() {
mkdir -p "$RUST_INST" || exit 1
if [ $# -eq 0 ]; then
echo "Which version? E.g. 1.14.0 or nightly" >&2
exit 1
elif [ $# -eq 1 ]; then
VER="$1"
DATE=""
DEST="$RUST_INST/rust-$VER"
elif [ $# -eq 2 ]; then
VER="$1"
DATE="$2"
DEST="$RUST_INST/rust-$DATE-$VER"
else
echo "Too many args: $@" >&2
fi
NAME="rust-$VER-$PLATFORM"
# Both .tar.gz and .tar.xz are supported:
FILENAME="$NAME.tar.xz"
if [ -z "$DATE" ]; then
SOURCE="https://static.rust-lang.org/dist/$FILENAME"
else
SOURCE="https://static.rust-lang.org/dist/$DATE/$FILENAME"
fi
TMP=$(mktemp -d)
cd "$TMP" || exit 1
echo "Downloading: $SOURCE.sha256"
curl -O "$SOURCE.sha256" || exit 1
SHA="$FILENAME.sha256"
if [ -e "$DEST" ]; then
if [ -f "$DEST/$SHA" ]
then
if diff -q "$DEST/$SHA" "$SHA"
then
echo "Already up to date"
exit 0
else
echo "Removing outdated installation"
rm -rf "$DEST"
fi
else
echo "Error: directory used; not upgradeable: $DEST" >&2
exit 1
fi
fi
echo "Downloading: $SOURCE"
curl -O "$SOURCE" || exit 1
shasum -a 256 -c "$SHA" || exit 1
tar xaf "$FILENAME" || exit 1
"$NAME/install.sh" "--prefix=$DEST" || exit 1
mv "$SHA" "$DEST/$SHA"
cd -
rm -rf "$TMP"
echo "Installed: $DEST"
if [ ! -e "$RUST_INST/rust-default" ]; then
setlink "$VER"
echo "Set as default"
fi
}
setlink() {
if [ ! -e "$RUST_INST" ]; then
echo "No installation directory. Maybe run install first?" >&2
exit 1
fi
if [ $# -eq 0 ]; then
echo "Set what version as default (or give what version a nickname)?" >&2
exit 1
elif [ $# -eq 1 ]; then
VER="$1"
NICK="default"
elif [ $# -eq 2 ]; then
VER="$1"
NICK="$2"
else
echo "Too many args: $@" >&2
fi
TARG="rust-$VER"
LINK="$RUST_INST/rust-$NICK"
if [ -e "$LINK" ]; then
if [ -h "$LINK" ]; then
# consider links safe to delete
rm "$LINK"
else
echo "Already exists: $LINK" >&2
exit 1
fi
fi
ln -s "$TARG" "$LINK"
}
run() {
if [ -z "$RUST_VER" ]
then
# there should be a symlink with this name:
RUST_VER=default
fi
RUST_HOME="$RUST_INST/rust-$RUST_VER"
# Sanity check
if [ ! -e "$RUST_INST" ]; then
echo "No installation directory. Maybe run install first?" >&2
exit 1
elif [ ! -e "$RUST_HOME" ]
then
echo "run-rust: not found: $RUST_HOME" >&2
echo "run-rust: did you install?" >&2
if [ "$RUST_VER" != "default" ]
then
echo "run-rust: did you set the wrong RUST_VER?" >&2
fi
exit 1
elif [ ! -x "$RUST_HOME/bin/rustc" ]
then
echo "run-rust: not found: $RUST_HOME/bin/rustc" >&2
echo "run-rust: bad installation?" >&2
exit 1
elif [ ! -x "$RUST_HOME/bin/$BIN" ]; then
echo "run-rust: could not find binary: $RUST_HOME/bin/$BIN" >&2
echo "run-rust: binaries available:" >&2
ls "$RUST_HOME/bin" >&2
exit 1
fi
export PATH="$PATH:$RUST_HOME/bin"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$RUST_HOME/lib"
"$RUST_HOME/bin/$BIN" "$@"
}
# ——— script entrypoint ———
BIN=$(basename $0)
if [ "$BIN" == "run-rust" ]
then
# management / general help
if [ $# -eq 0 ]; then
usage
fi
CMD="$1"
shift
case "$CMD" in
list)
list
;;
install)
install "$@"
;;
run)
if [ $# -eq 0 ]; then
echo "Run what?" >&2
exit 1
fi
BIN="$1"
shift
run "$@"
;;
set)
setlink "$@"
;;
help)
usage
;;
*)
echo "run-rust: unknown command $CMD" >&2
usage
exit 1
;;
esac
else
# assume user wanted to run a rust binary
run "$@"
fi
You can’t perform that action at this time.