feat: smart rust executable symlink to ~/.local/bin
2 files changed, +38 -7
+0-7before_plugins.sh
| @@ -4,13 +4,6 @@ export PATH="$PATH:$HOME/.local/share/solana/install/active_release/bin" | ||
| 4 | 4 | export PATH="$HOME/.local/share/fnm:$PATH" |
| 5 | 5 | export PATH="$PATH:/usr/local/flutter/bin" |
| 6 | 6 | |
| 7 | -# Set up a cached file as a subprocess with all the home rust debug paths in it | |
| 8 | -( | |
| 9 | - mkdir -p ~/.cache | |
| 10 | - fd -I -c never -E 'doc' -E 'package' --full-path 'target/debug$' ~ | tr '\n' ':' >~/.cache/RUST_DEBUG_PATHS | |
| 11 | -) | |
| 12 | -[ -f ~/.cache/RUST_DEBUG_PATHS ] && RUST_DEBUG_PATHS="$(cat ~/.cache/RUST_DEBUG_PATHS)" && export PATH="$PATH:$RUST_DEBUG_PATHS" | |
| 13 | - | |
| 14 | 7 | # Add environments |
| 15 | 8 | [ -f ~/.fzf.zsh ] && . ~/.fzf.zsh |
| 16 | 9 | [ -f ~/.cargo/env ] && . "$HOME/.cargo/env" |
+38-0custom-plugins/symlink_rust_executables.zsh
| @@ -0,0 +1,38 @@ | ||
| 1 | +export SYMLINK_RUST_VERSION="0.0.1" | |
| 2 | + | |
| 3 | +export SYMLINK_RUST_ENV="${SYMLINK_RUST_ENV:-debug}" # debug/release: defaults to debug | |
| 4 | +export SYMLINK_DEBUGGING_MODE="${SYMLINK_DEBUGGING_MODE:-0}" # 0 for off and 1 for on | |
| 5 | + | |
| 6 | +# Make sure local bin is available | |
| 7 | +[ ! -d ~/.local/bin ] && mkdir -p ~/.local/bin | |
| 8 | +echo "$PATH" | grep -q "$HOME/.local/bin" || export PATH="$PATH:$HOME/.local/bin" | |
| 9 | + | |
| 10 | +# Keep track of programs added through this plugin | |
| 11 | +SYMLINKED_PROGRAMS=() | |
| 12 | + | |
| 13 | +# Create a symlink from debug/release to target. | |
| 14 | +# Can switch from debug to release at any time with `export SYMLINK_RUST_ENV=debug/release`. | |
| 15 | +# Your next command will switch out the symlink. | |
| 16 | +function _symlink_rust_executables() { | |
| 17 | + [[ "$SYMLINK_DEBUGGING_MODE" == "1" ]] && echo "searching target/$SYMLINK_RUST_ENV for executables" | |
| 18 | + for exe in target/${SYMLINK_RUST_ENV:-debug}/*; do | |
| 19 | + [[ -f "$exe" && -x "$exe" ]] || continue | |
| 20 | + local name="$(basename "$exe")" | |
| 21 | + local src="$(realpath "$exe")" | |
| 22 | + local dst="$HOME/.local/bin/$name" | |
| 23 | + [[ -x "$exe" ]] && ln -sf "$src" "$dst" | |
| 24 | + [[ "$SYMLINK_DEBUGGING_MODE" == "1" ]] && echo "linked $src to $dst" | |
| 25 | + SYMLINKED_PROGRAMS+=("$dst") | |
| 26 | + done | |
| 27 | +} | |
| 28 | + | |
| 29 | +# Remove all the programs created | |
| 30 | +function _cleanup_symlink_rust_executables() { | |
| 31 | + for program in "${SYMLINKED_PROGRAMS[@]}"; do | |
| 32 | + rm -f "$program" | |
| 33 | + done | |
| 34 | +} | |
| 35 | + | |
| 36 | +autoload -Uz add-zsh-hook | |
| 37 | +add-zsh-hook precmd _symlink_rust_executables | |
| 38 | +add-zsh-hook zshexit _cleanup_symlink_rust_executables |