Skip to content

Instantly share code, notes, and snippets.

@jelmerdemaat
Last active October 10, 2024 08:40
Show Gist options
  • Save jelmerdemaat/305bd1fc1faacb7d4ce5d4db88208f2c to your computer and use it in GitHub Desktop.
Save jelmerdemaat/305bd1fc1faacb7d4ce5d4db88208f2c to your computer and use it in GitHub Desktop.
Custom drush command to find project specific drush binary inside vendor folder

Install instructions

  1. Create file: sudo touch /usr/local/bin/drush
  2. Make file executable: sudo chmod +x /usr/local/bin/drush
  3. Open the file, and copy and paste the script to it, save it.
#!/bin/bash -u
function drush() {
local root_dir=''
# Parse options
while getopts "r:" opt; do
case $opt in
r)
root_dir=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
return 1
;;
esac
done
shift $((OPTIND - 1))
# Start at the current directory or the specified root directory
if [[ -n "$root_dir" ]]; then
dir="$root_dir"
else
dir=$PWD
fi
# While not at the root (/)
while [[ "$dir" != "/" ]]; do
# If vendor/bin/drush exists, run it
if [[ -x "$dir/vendor/bin/drush" ]]; then
"$dir/vendor/bin/drush" "$@"
return
fi
# Go up one directory level
dir=$(dirname "$dir")
done
echo "Could not find drush in any parent directory."
return 1
}
drush "$@"
@jelmerdemaat
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment