Skip to content

Commit f39a238

Browse files
committed
Adds a new "robustness" script for running composer-install.
Not entirely useful since you will have already had to run `composer install` to have this script available, but a nice reference.
1 parent 040c3b1 commit f39a238

3 files changed

Lines changed: 77 additions & 2 deletions

File tree

composer-binaries

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* @param string $scriptName Typically set to basename($argv[0]) when the method is called.
1313
* @return void
1414
*/
15-
function usage ($scriptName) {
15+
function usage ($scriptName, $exitCode = 0) {
1616
$usage = <<<EOD
1717
1818
{$scriptName}
@@ -34,7 +34,7 @@ Usage:
3434
EOD;
3535

3636
echo $usage;
37-
exit(0);
37+
exit($exitCode);
3838
}
3939

4040
/**

composer-install

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"bin-selfupdate",
2323
"clear-cache",
2424
"clear-logs",
25+
"composer-install",
2526
"db-backup",
2627
"db-credentials",
2728
"db-loadschema",

0 commit comments

Comments
 (0)