Created
May 16, 2026 14:58
-
-
Save konsolebox/cf2e897b26a514959001329c25f11193 to your computer and use it in GitHub Desktop.
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 | |
| # Dynamically updates versioned symlinks in /usr/local/bin for independent Ruby gems. | |
| # Maps target binaries to their active Gentoo package ruby_targets using /var/db/pkg. | |
| # This script was composed with significant help from Google AI. | |
| shopt -s nullglob || exit | |
| LINK_DIR="/usr/local/bin" | |
| SELECTOR_PATH="/write/libexec/ruby/gem-binary-exec.bash" | |
| TARGET_BINARIES=(irb pry) | |
| GENFUN_MODULES="rc portage" | |
| source /lib/gentoo/functions.sh || exit | |
| function remove_links { | |
| local link | |
| for link; do | |
| einfo "Removing link: ${link}" | |
| rm -f -- "${link}" | |
| done | |
| } | |
| function process_binary { | |
| local binary=$1 link pkg target targets use_files IFS=' ' | |
| local -A created_links=() || exit | |
| if [[ ! -f /usr/bin/${binary} ]]; then | |
| ewarn "/usr/bin/${binary} doesn't exist." | |
| remove_links "${LINK_DIR}/${binary}"[0-9][0-9] | |
| return | |
| fi | |
| readarray -t pkg < <(qfile -qC "/usr/bin/${binary}") | |
| [[ ${pkg[1]+.} ]] && die "The ${binary} binary represents too many packages." | |
| use_files=(/var/db/pkg/"${pkg}"-[0-9]*/USE) | |
| [[ ${use_files+.} ]] || die "No USE files found with ${pkg}." | |
| readarray -t targets < <(grep -Poe '(?<=ruby_targets_ruby)[0-9][0-9]' "${use_files[@]}" | sort -u) | |
| [[ ${targets+.} ]] || die "No active Ruby target found with '${pkg}'." | |
| for target in "${targets[@]}"; do | |
| link=${LINK_DIR}/${binary}${target} | |
| einfo "Creating link: ${link} (${pkg})" | |
| ln -sf "${SELECTOR_PATH}" "${link}" | |
| created_links[${link}]=. | |
| done | |
| for link in "${LINK_DIR}/${binary}"[0-9][0-9]; do | |
| [[ ${created_links[${link}]+.} ]] || remove_links "${link}" | |
| done | |
| } | |
| function main { | |
| [[ -d ${LINK_DIR} ]] || die "Specified link directory not a directory: ${LINK_DIR}" | |
| [[ -e ${SELECTOR_PATH} ]] || die "Specified selector doesn't exist: ${SELECTOR_PATH}" | |
| local binary | |
| for binary in "${TARGET_BINARIES[@]}"; do | |
| process_binary "${binary}" | |
| done | |
| } | |
| main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment