Last active
August 19, 2016 18:22
-
-
Save javiervivanco/d912e68b9901f79e03ba4e775eab497a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/bin/bash | |
# Usage info | |
show_help() { | |
cat << EOF | |
Usage: ${0##*/} [-hv] [-i INSTALL_DIR] | |
Install PHP QA Tools | |
-h display this help and exit | |
-i INSTALL_DIR directory to install PHP QA Tools | |
By default install into ./bin | |
If not exists, create it | |
-v verbose mode. | |
EOF | |
} | |
# Initialize our own variables: | |
VERSION_PHPLOC='phploc.phar' | |
VERSION_PHPUNIT='phpunit-old.phar' | |
VERSION_PHPCS='phpcs.phar' | |
VERSION_PHPCBF='phpcbf.phar' | |
VERSION_PHPCPD='phpcpd.phar' | |
VERSION_PHPMD='2.4.2' | |
VERSION_PHP_CS_FIXER='v1.11.6' | |
VERSION_PHPLINT='phpLint' | |
COMMAND_PHPLOC='phploc' | |
COMMAND_PHPUNIT='phpunit' | |
COMMAND_PHPCS='phpcs' | |
COMMAND_PHPCBF='phpcbf' | |
COMMAND_PHPCPD='phpcpd' | |
COMMAND_PHPMD='phpmd' | |
COMMAND_PHP_CS_FIXER='php-cs-fixer' | |
COMMAND_PHPLINT='phpLint' | |
URL_PHPMD='http://static.phpmd.org/php/%s/phpmd.phar' | |
URL_PHP_CS_FIXER='https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/%s/php-cs-fixer.phar' | |
URL_PHPLOC='https://phar.phpunit.de/%s' | |
URL_PHPUNIT='https://phar.phpunit.de/%s' | |
URL_PHPCS='https://squizlabs.github.io/PHP_CodeSniffer/%s' | |
URL_PHPCBF='https://squizlabs.github.io/PHP_CodeSniffer/%s' | |
URL_PHPCPD='https://phar.phpunit.de/%s' | |
URL_PHPLINT='https://raw.githubusercontent.com/javiervivanco/phpLintBash/master/%s' | |
verbose=0 | |
INSTALL_DIR="$PWD/bin" | |
OPTIND=1 # Reset is necessary if getopts was used previously in the script. It is a good idea to make this local in a function. | |
while getopts "hvi:" opt; do | |
case "$opt" in | |
h) | |
show_help | |
exit 0 | |
;; | |
v) verbose=$((verbose+1)) | |
;; | |
i) INSTALL_DIR=$(realpath $OPTARG) | |
;; | |
'?') | |
show_help >&2 | |
exit 1 | |
;; | |
esac | |
done | |
shift "$((OPTIND-1))" # Shift off the options and optional --. | |
function title { | |
tput setab 4; tput smul; echo "$1"; tput rmul; tput setab 9; echo | |
} | |
function title_warning { | |
tput setab 1;tput smul; echo "$1";tput rmul;tput setab 9; | |
} | |
function subtitle { | |
tput setab 2 | |
echo " - $1" | |
tput setab 9 | |
echo | |
} | |
function subtitle_warning { | |
tput setab 3 | |
echo " - $1" | |
tput setab 9 | |
echo | |
} | |
function install_composer { | |
EXPECTED_SIGNATURE=$(php -r "echo file_get_contents('https://composer.github.io/installer.sig');") | |
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" | |
ACTUAL_SIGNATURE=$(php -r "echo hash_file('SHA384', 'composer-setup.php');") | |
if [ "$EXPECTED_SIGNATURE" = "$ACTUAL_SIGNATURE" ] | |
then | |
php composer-setup.php --quiet | |
RESULT=$? | |
rm composer-setup.php | |
composer.phar --version | |
mv composer.phar $INSTALL_DIR/composer | |
else | |
>&2 echo 'ERROR: Invalid installer signature' | |
rm composer-setup.php | |
exit 1 | |
fi | |
} | |
function install { | |
URL=$1 | |
PROGRAM=$2 | |
if [ ! -e "$INSTALL_DIR/$PROGRAM" ]; then | |
subtitle "Install in $INSTALL_DIR/$PROGRAM" | |
download $URL $PROGRAM | |
chmod +x $PROGRAM | |
mv $PROGRAM $INSTALL_DIR/ | |
$INSTALL_DIR/$PROGRAM --version | |
else | |
subtitle_warning "$INSTALL_DIR/$PROGRAM exist" | |
fi | |
} | |
function download { | |
URL=$1 | |
PROGRAM=$2 | |
subtitle_warning "Download $PROGRAM in $INSTALL_DIR from $URL" | |
PHP_SRC="file_put_contents('$PROGRAM', file_get_contents('$URL'));" | |
php -r "$PHP_SRC" | |
} | |
title "Install QA Tools PHP in $INSTALL_DIR" | |
if [ ! -d $INSTALL_DIR ] ; then | |
subtitle "Creating $INSTALL_DIR" | |
mkdir -p $INSTALL_DIR | |
fi | |
if [ ! -e $INSTALL_DIR/composer ]; then | |
subtitle "Install $INSTALL_DIR/composer" | |
install_composer | |
else | |
subtitle_warning "$INSTALL_DIR/composer exist" | |
fi | |
install "$(printf $URL_PHPLOC $VERSION_PHPLOC)" $COMMAND_PHPLOC | |
install "$(printf $URL_PHPUNIT $VERSION_PHPUNIT)" $COMMAND_PHPUNIT | |
install "$(printf $URL_PHPCS $VERSION_PHPCS)" $COMMAND_PHPCS | |
install "$(printf $URL_PHPCBF $VERSION_PHPCBF) " $COMMAND_PHPCBF | |
install "$(printf $URL_PHPCPD $VERSION_PHPCPD)" $COMMAND_PHPCPD | |
install "$(printf $URL_PHPMD $VERSION_PHPMD)" $COMMAND_PHPMD | |
install "$(printf $URL_PHP_CS_FIXER $VERSION_PHP_CS_FIXER)" $COMMAND_PHP_CS_FIXER | |
install "$(printf $URL_PHPLINT $VERSION_PHPLINT)" $COMMAND_PHPLINT | |
echo "Ok" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment