Created
July 13, 2026 23:39
-
-
Save aklump/83e1f3be34d20fa61c6d9293a8c592d9 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
| #!/usr/bin/env bash | |
| # @file Composer Shim Script | |
| # | |
| # This script is used to shim Composer-installed executables located in a | |
| # sandboxed directory. By sourcing this script, the sandboxed `vendor/bin` | |
| # directory is prepended to the $PATH, allowing the sandboxed version of the | |
| # command to be executed instead of any global version. | |
| # | |
| # Usage: | |
| # . bin/shim-composer.sh <command> | |
| # | |
| # Expectations: | |
| # 1. The script MUST be sourced, not executed. | |
| # 2. A command argument MUST be provided. | |
| # 3. The directory structure MUST follow this convention (relative to the script's parent): | |
| # - ./.<command> (the sandbox directory) | |
| # - ./.<command>/vendor/bin/<command> (the executable) | |
| # | |
| # Example: | |
| # For a command `directio`: | |
| # - sandbox: ./.directio | |
| # - executable: ./.directio/vendor/bin/directio | |
| if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then | |
| echo "Error: this script must be sourced, not executed. Try: . bin/shim-composer.sh directio" >&2 | |
| return 1 2>/dev/null || exit 1 | |
| fi | |
| command="$1" | |
| if [[ -z "$command" ]]; then | |
| echo "Usage: . bin/shim-composer.sh <command>" >&2 | |
| return 1 2>/dev/null || exit 1 | |
| fi | |
| s="${BASH_SOURCE[0]}";[[ "$s" ]] || s="${(%):-%N}";while [ -h "$s" ];do d="$(cd -P "$(dirname "$s")" && pwd)";s="$(readlink "$s")";[[ $s != /* ]] && s="$d/$s";done;__DIR__=$(cd -P "$(dirname "$s")" && pwd) | |
| function create_shim() { | |
| local command="$1" | |
| local sandbox_dir="$(cd "$__DIR__/../.$command" 2>/dev/null && pwd)" | |
| if [[ ! -d "$sandbox_dir" ]]; then | |
| echo "Error: sandbox directory '.$command' not found relative to $(dirname "$__DIR__")" >&2 | |
| return 1 | |
| fi | |
| local bin_dir="$sandbox_dir/vendor/bin" | |
| if [[ ! -d "$bin_dir" ]]; then | |
| echo "Error: vendor/bin not found in sandbox directory '.$command'" >&2 | |
| return 1 | |
| fi | |
| local executable="$bin_dir/$command" | |
| if [[ ! -x "$executable" ]]; then | |
| echo "Error: executable '$command' not found or not executable in $bin_dir" >&2 | |
| return 1 | |
| fi | |
| case ":$PATH:" in | |
| *":$bin_dir:"*) ;; | |
| *) export PATH="$bin_dir:$PATH" ;; | |
| esac | |
| echo "😎 $command shimmed to $executable" | |
| } | |
| create_shim "$command" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment