|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +#--------------------------------------------------------------------- |
| 4 | +usage () |
| 5 | +{ |
| 6 | + cat <<EOT |
| 7 | +
|
| 8 | +${0##*/} |
| 9 | + Provides a wrapper around \`composer install\` that takes into |
| 10 | + account the current APP_ENV environment setting, as well as the |
| 11 | + presence or absence of a composer.json file and a composer |
| 12 | + executable itself. Can be safely called even if these items are |
| 13 | + missing (with the result being essentially a no-op and >0 exit |
| 14 | + code.) |
| 15 | + |
| 16 | + There is a certain amount of irony in this script being |
| 17 | + included in a composer package, since you would have had to |
| 18 | + already run 'composer install' in order to have access to this |
| 19 | + convenience script. If nothing else, this script exists here |
| 20 | + as a copy/paste reference of how to achieve a certain amount |
| 21 | + of gracefulness in running a tool that may or may not be |
| 22 | + present. |
| 23 | +
|
| 24 | +Usage: |
| 25 | + bin/${0##*/} |
| 26 | +
|
| 27 | +
|
| 28 | +EOT |
| 29 | + |
| 30 | + exit 0 |
| 31 | +} |
| 32 | +if [ "$1" = '-h' ]; then |
| 33 | + usage |
| 34 | +fi |
| 35 | + |
| 36 | + |
| 37 | +DIR="$( cd -P "$( dirname "$0" )"/.. >/dev/null 2>&1 && pwd )" |
| 38 | +BIN_DIR="${DIR}/bin" |
| 39 | +COMPOSER_CONFIG_FILE="$DIR/composer.json" |
| 40 | +GUESSES=( "$( which composer )" "$BIN_DIR/composer" "$( which composer.phar )" "$BIN_DIR/composer.phar" ) |
| 41 | + |
| 42 | +# Bail out if there's no config file present. |
| 43 | +if [ ! -e "$COMPOSER_CONFIG_FILE" ]; then |
| 44 | + echo "!! No composer config file at '$COMPOSER_CONFIG_FILE'." |
| 45 | + exit 2 |
| 46 | +fi |
| 47 | +echo "## Found composer config at: ${COMPOSER_CONFIG_FILE}" |
| 48 | + |
| 49 | +# Bail out if composer isn't available to us. |
| 50 | +for GUESS in "$GUESSES"; do |
| 51 | + if [ -x "$GUESS" ]; then |
| 52 | + COMPOSER="$GUESS" |
| 53 | + break |
| 54 | + fi |
| 55 | +done |
| 56 | +if [ -z "$COMPOSER" ]; then |
| 57 | + echo "!! The 'composer' command was not found on this system." |
| 58 | + echo "" |
| 59 | + exit 1 |
| 60 | +fi |
| 61 | +echo "## Found composer at: ${COMPOSER}" |
| 62 | + |
| 63 | + |
| 64 | +# Set options based on APP_ENV. |
| 65 | +case "$APP_ENV" in |
| 66 | + prod|production) COMPOSER_OPTIONS="--no-dev" ;; |
| 67 | + stage|staging) COMPOSER_OPTIONS="--no-dev" ;; |
| 68 | + dev|development) COMPOSER_OPTIONS="--dev" ;; |
| 69 | + *) COMPOSER_OPTIONS="--dev" |
| 70 | +esac |
| 71 | + |
| 72 | +# Execute `composer install` |
| 73 | +"$COMPOSER" install $COMPOSER_OPTIONS --no-interaction |
| 74 | +"$COMPOSER" dumpautoload --optimize |
0 commit comments