From 72dde13eb7e8d25be2b501dd6f00be2a6b4e6559 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Thu, 9 Mar 2023 23:18:50 +0000 Subject: [PATCH] feat(rust): improve overall setup around Rust --- install.sh | 7 +++++ zsh/rust.zsh | 89 +++++++++++++++++++++++++++++++++++++++++++++++----- zshenv | 6 ++++ 3 files changed, 94 insertions(+), 8 deletions(-) diff --git a/install.sh b/install.sh index 4101ae8..c96b4bf 100755 --- a/install.sh +++ b/install.sh @@ -119,6 +119,10 @@ install_emacs_config() { git_clone 'git@github.com:jimeh/.emacs.d.git' "$TARGET/.config/emacs-siren" } +install_rustup() { + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh +} + # # Helper functions # @@ -269,6 +273,9 @@ case "$1" in rbenv) install_rbenv ;; + rustup | rust) + install_rustup + ;; launch_agents | launch-agents | agents) install_launch_agents ;; diff --git a/zsh/rust.zsh b/zsh/rust.zsh index ee9956e..ece64bc 100644 --- a/zsh/rust.zsh +++ b/zsh/rust.zsh @@ -2,13 +2,86 @@ # Rust environment setup. # -install_rust_global_packages() { - local packages=( - rls - rust-analysis - rust-src - rustfmt - ) +# ============================================================================== +# aliases +# ============================================================================== - rustup component add "${packages[@]}" +alias c="cargo" + +if command-exists exa; then + alias ll="exa -lagHS --color-scale --icons --git" + alias llt="ll --tree" +fi + +if command-exists bat; then + alias cat="bat -P" +fi + +# ============================================================================== +# completions +# ============================================================================== + +if command-exists rustup; then + _rustup() { + unset -f _rustup + eval "$(rustup completions zsh)" + } + compctl -K _rustup rustup + + if command-exists cargo; then + _cargo() { + unset -f _cargo + eval "$(rustup completions zsh cargo)" + } + compctl -K _cargo cargo + fi +fi + +# ============================================================================== +# global rust packages +# ============================================================================== + +install_rust_global_packages() { + ( + set -e + + if ! command-exists rustup; then + read -q "REPLY?Rustup was not found. Install it? [y/N] " && + echo && + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh + fi + + if ! command-exists rustup; then + echo "Rustup was not found. Aborting." + return 1 + fi + + rustup component add \ + clippy \ + rust-analyzer \ + rust-src \ + rustfmt + + # Install or update cargo-binstall + if ! command-exists cargo-binstall; then + RUSTC_WRAPPER="${commands[sccache]}" cargo install cargo-binstall + fi + + # Install sccache before the rest of the packages. + if ! command-exists sccache; then + RUSTC_WRAPPER="" cargo binstall sccache + fi + + RUSTC_WRAPPER=sccache cargo binstall cargo-quickinstall + + RUSTC_WRAPPER=sccache cargo quickinstall \ + cargo-audit \ + cargo-info + + RUSTC_WRAPPER=sccache cargo binstall \ + cargo-update \ + cargo-edit \ + bacon \ + dirstat-rs + ) } diff --git a/zshenv b/zshenv index cc2d59d..93c8f52 100644 --- a/zshenv +++ b/zshenv @@ -184,9 +184,15 @@ path_prepend "/opt/tmux/bin" path_prepend "$HOME/.rbenv/shims" # Rust setup +export RUSTUP_HOME="$HOME/.rustup" export CARGO_HOME="$HOME/.cargo" path_prepend "$CARGO_HOME/bin" +export RUST_BACKTRACE=1 +if command-exists sccache; then + export RUSTC_WRAPPER=sccache +fi + # RTX shim setup path_prepend "$HOME/.rtx/shims"