Created
August 23, 2024 11:28
-
-
Save warpfork/51a5a9f964fbf257905ed6d708d7a355 to your computer and use it in GitHub Desktop.
rustget: a shell script to install rust and rust-analyzer, without the crazy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# The short shell script for you who wants an installer that does the job and stops. | |
# (And thus doesn't want anything to do with rustup.) | |
# | |
# Find the latest rust version numbers by looking at | |
# https://forge.rust-lang.org/infra/other-installation-methods.html | |
# You'll need to give that number as an argument. | |
set -euo pipefail | |
VERSION=${1?"version number must be provided as arg 1"} | |
ARCHISH=${2:-"x86_64-unknown-linux-gnu"} | |
function progress { | |
>&2 printf "\033[33m%s\033[m\n" "$@" | |
} | |
mkdir -p /tmp/rustget | |
cd /tmp/rustget | |
# Get the stuff. | |
mkdir -p rust-installer-archives | |
(cd rust-installer-archives | |
progress "Downloading tarballs..." | |
# This is the actual binaries that you want. | |
[ -f "rust-${VERSION}-${ARCHISH}.tar.gz" ] || wget https://static.rust-lang.org/dist/rust-${VERSION}-${ARCHISH}.tar.gz | |
# This contains the stdlib source (and a bunch of other stuff we don't need) which is needed by rust-analyzer to have a good day. | |
[ -f "rustc-${VERSION}-src.tar.gz" ] || wget https://static.rust-lang.org/dist/rustc-${VERSION}-src.tar.gz | |
) | |
# Unpack most of the stuff. | |
mkdir -p rust-${VERSION}-${ARCHISH} | |
(cd rust-${VERSION}-${ARCHISH} | |
progress "Extracting binaries..." | |
# Extract the tarball. | |
tar -xf ../rust-installer-archives/rust-${VERSION}-${ARCHISH}.tar.gz | |
# The tarball contains a single directory that's very redundant, so let's do away with that. | |
mv rust-${VERSION}-${ARCHISH}/* . ; rmdir rust-${VERSION}-${ARCHISH}/ | |
# Put all the files where they actually should be relative to each other. | |
# Basically, move everything up one directory. (This is so silly. But this is how they package Rust.) | |
progress "Rearranging files..." | |
for x in */; do (cd $x && find -type d | xargs -I{} -- mkdir -p ../{} && find -type f,l | xargs -I{} mv {} ../{}) && rm -r $x; done | |
# Make the directory that we're going to drop the stdlib source into, for rust-analyzer to find it. | |
mkdir -p lib/rustlib/src/rust/ | |
progress "Main extraction and rearrangement done." | |
) | |
# Unpack the stdlib sources, put them in the right place, and throw away the rest. | |
mkdir -p sillysauce | |
(cd sillysauce | |
progress "Extracting stdlib source..." | |
# Extract this tarball that contains all the stdlib source... and all the rustc source, and all the test data, and a million other things we _don't need_. | |
# Ask tar to extract just the library dir, which contains that stdlib source. | |
tar -xf ../rust-installer-archives/rustc-${VERSION}-src.tar.gz rustc-${VERSION}-src/library/ | |
# Move it to the place rust-analyzer will expect it. | |
progress "Rearranging stdlib source to where it's expected..." | |
mv rustc-${VERSION}-src/library ../rust-${VERSION}-${ARCHISH}/lib/rustlib/src/rust/ | |
rmdir * | |
) | |
rmdir sillysauce | |
progress "Done!" | |
# Done! | |
# You can now add `rust-${VERSION}-${ARCHISH}/bin` to your $PATH, and it DTRT! | |
# You can keep or throw away the rust-installer-archives dir, as you wish. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment