Skip to content

Instantly share code, notes, and snippets.

@mente
Last active December 20, 2015 10:39
Show Gist options
  • Save mente/6117397 to your computer and use it in GitHub Desktop.
Save mente/6117397 to your computer and use it in GitHub Desktop.
Composer wrapper script that checks whether composer is installed in the system. If not - install it to bin folder of this script. Idea is taken from gradle and gradlew that I've been struggling with during this weekend.
#!/bin/bash
# Composer wrapper script that checks whether composer is installed in the system. If not - install it to bin folder of this script
# Idea is taken from gradle and gradlew that I've been struggling with during this weekend
# author - Alex Vasilenko http://github.com/mente
# Exit on any failure
set -e
#http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
BASEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
#http://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script
cmdPath() {
echo `command -v "${1}"`
}
#http://getcomposer.org/download/
installComposer() {
PHP_CMD=$(cmdPath "php")
if [ -z ${PHP_CMD} ]; then
echo "PHP executable not found in $PATH"
exit 1
fi
CURL_CMD=$(cmdPath "curl")
if [ ${CURL_CMD} ]; then
${CURL_CMD} -sS https://getcomposer.org/installer | ${PHP_CMD} -- --install-dir=$1
else
${PHP_CMD} -r "eval('?>'.file_get_contents('https://getcomposer.org/installer'));" -- --install-dir=$1
fi
}
BIN=${BASEDIR}/bin
if [ -f ${BIN}/composer.phar ]; then
COMPOSER_CMD=${BIN}/composer.phar
else
COMPOSER_CMD=$(cmdPath "composer") || $(cmdPath "composer.phar")
fi
if [ -z ${COMPOSER_CMD} ]; then
echo "Unable to find composer in PATH. Installing"
installComposer ${BIN}
COMPOSER_CMD=${BIN}/composer.phar
fi
exec "$COMPOSER_CMD" "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment