Skip to content

Commit 2640e93

Browse files
committed
Improved installer script based on feedback & shellcheck.
1 parent 0aaab17 commit 2640e93

File tree

1 file changed

+29
-37
lines changed

1 file changed

+29
-37
lines changed

bin/install.sh

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,63 @@
11
#!/usr/bin/env bash
2+
set -eu
23

34
# Ensure PHP is installed
4-
php -v > /dev/null 2>&1
5-
if [[ $? -ne 0 ]]; then
5+
if ! command -v php > /dev/null 2>&1; then
66
echo "PHP is not found, installation aborted!"
77
exit 1
88
fi
99

10-
# Check if Composer is already globally available
11-
composer -v > /dev/null 2>&1
12-
1310
# Ensure Composer is installed/updated
14-
if [[ $? -ne 0 ]]; then
11+
if ! command -v composer > /dev/null 2>&1; then
1512
echo "*** Installing Composer ***"
16-
17-
EXPECTED_SIGNATURE=$(wget https://composer.github.io/installer.sig -O - -q)
1813
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
19-
ACTUAL_SIGNATURE=$(php -r "echo hash_file('SHA384', 'composer-setup.php');")
2014

21-
if [ "$EXPECTED_SIGNATURE" = "$ACTUAL_SIGNATURE" ]
22-
then
15+
if [ \
16+
"$(curl -S https://composer.github.io/installer.sig)" = \
17+
"$(php -r \"echo hash_file\('SHA384', 'composer-setup.php'\);\")" \
18+
]; then
2319
php composer-setup.php --quiet --filename=composer
24-
RESULT=$?
25-
if [[ $? -eq 0 ]]; then
26-
sudo mv composer /usr/local/bin/composer
27-
RESULT=$?
28-
fi
20+
sudo mv composer /usr/local/bin/composer
2921
rm composer-setup.php
30-
if [[ RESULT -ne 0 ]]; then
31-
exit $RESULT
32-
fi
3322
else
3423
>&2 echo 'ERROR: Invalid composer installer signature'
3524
rm composer-setup.php
3625
exit 1
3726
fi
38-
else
39-
echo "*** Updating Composer ***"
40-
composer selfupdate > /dev/null 2>&1
41-
if [[ $? -ne 0 ]]; then
42-
composer global update "composer/composer"
43-
fi
4427
fi
4528

4629
echo "*** Updating shell profiles ***"
4730
# Add Composer's Global Bin to ~/.profile path
48-
if [[ ! -f "~/.profile" ]]; then
49-
touch ~/.profile
31+
if [[ ! -f "$HOME/.profile" ]]; then
32+
touch "$HOME/.profile"
33+
fi
34+
if grep -q -F "export COMPOSER_HOME=$HOME/.composer" "$HOME/.profile"; then
35+
echo "export COMPOSER_HOME=$HOME/.composer" >> "$HOME/.profile"
36+
fi
37+
if grep -q -F "export PATH=$PATH:\$COMPOSER_HOME/vendor/bin" "$HOME/.profile"; then
38+
echo "export PATH=$PATH:\$COMPOSER_HOME/vendor/bin" >> "$HOME/.profile"
5039
fi
51-
grep -q -F 'export COMPOSER_HOME="~/.composer"' ~/.profile || echo 'export COMPOSER_HOME="~/.composer"' >> ~/.profile
52-
grep -q -F 'export PATH=$PATH:$COMPOSER_HOME/vendor/bin' ~/.profile || echo 'export PATH=$PATH:$COMPOSER_HOME/vendor/bin' >> ~/.profile
5340

5441
# Source the .profile to pick up changes
55-
. ~/.profile
42+
# shellcheck source=/dev/null
43+
source "$HOME/.profile"
5644

5745
# Test if ZSH is installed
58-
zsh --version > /dev/null 2>&1
59-
if [[ $? -eq 0 ]]; then
46+
if command -v zsh > /dev/null 2>&1; then
6047
# Add Composer's Global Bin to ~/.zprofile path
61-
if [[ ! -f "~/.zprofile" ]]; then
62-
touch ~/.zprofile
48+
if [[ ! -f "$HOME/.zprofile" ]]; then
49+
touch "$HOME/.zprofile"
50+
fi
51+
if grep -q -F "export COMPOSER_HOME=$HOME/.composer" "$HOME/.zprofile"; then
52+
echo "export COMPOSER_HOME=$HOME/.composer" >> "$HOME/.zprofile"
53+
fi
54+
if grep -q -F "export PATH=$PATH:$COMPOSER_HOME/vendor/bin" "$HOME/.zprofile"; then
55+
echo "export PATH=$PATH:$COMPOSER_HOME/vendor/bin" >> "$HOME/.zprofile"
6356
fi
64-
grep -q -F 'export COMPOSER_HOME="~/.composer"' ~/.zprofile || echo 'export COMPOSER_HOME="~/.composer"' >> ~/.zprofile
65-
grep -q -F 'export PATH=$PATH:$COMPOSER_HOME/vendor/bin' ~/.zprofile || echo 'export PATH=$PATH:$COMPOSER_HOME/vendor/bin' >> ~/.zprofile
6657

6758
# Source the .zprofile to pick up changes
68-
. ~/.zprofile
59+
# shellcheck source=/dev/null
60+
source "$HOME/.zprofile"
6961
fi
7062

7163
# Ensure Dealerdirect PHP QA tools are installed globally

0 commit comments

Comments
 (0)