Skip to content

Instantly share code, notes, and snippets.

@aczietlow
Created August 15, 2017 16:02
Show Gist options
  • Save aczietlow/6468b39ecec02f31a93479e69485bb6a to your computer and use it in GitHub Desktop.
Save aczietlow/6468b39ecec02f31a93479e69485bb6a to your computer and use it in GitHub Desktop.
Docksal command that runs phpcbf against the project's code base.
#!/usr/bin/env bash
## Runs phpcbf to clean up code-sniffer errors.
## In composer.json:
## "require": {
## . "drupal/coder": "^8.2"
## }
# Abort if anything fails
set -e
SITE_DIRECTORY="default"
DOCROOT_PATH="${PROJECT_ROOT}/${DOCROOT}"
SITEDIR_PATH="${DOCROOT_PATH}/sites/${SITE_DIRECTORY}"
# Console colors
red='\033[0;31m'
green='\033[0;32m'
green_bg='\033[42m'
yellow='\033[1;33m'
NC='\033[0m'
echo-red () { echo -e "${red}$1${NC}"; }
echo-green () { echo -e "${green}$1${NC}"; }
echo-green-bg () { echo -e "${green_bg}$1${NC}"; }
echo-yellow () { echo -e "${yellow}$1${NC}"; }
# Yes/no confirmation dialog with an optional message
# @param $1 confirmation message
_confirm ()
{
while true; do
read -p "$1 [y/n]: " answer
case "$answer" in
[Yy]|[Yy][Ee][Ss] )
break
;;
[Nn]|[Nn][Oo] )
exit 1
;;
* )
echo 'Please answer yes or no.'
esac
done
}
# Exits fin if previous command exited with non-zero code
if_failed ()
{
if [ ! $? -eq 0 ]; then
echo-red "$*"
exit 1
fi
}
cd $PROJECT_ROOT
# Make sure Composer dependencies are installed
if [[ ! -d "$PROJECT_ROOT/vendor" ]]; then
fin exec "cd $PROJECT_ROOT && composer install --prefer-source --no-interaction"
fi
if [[ ! -x "bin/phpcs" ]]; then
echo -e "${yellow} bin/phpcs AND bin/phpcbf ${NC} are not set to be executable."
_confirm "Fix automatically?"
chmod +x "bin/phpcs"
chmod +x "bin/phpcbf"
if_failed "Could not make $command_script executable"
fi
fin exec "bin/phpcs --config-set installed_paths vendor/drupal/coder/coder_sniffer"
fin exec "bin/phpcbf --extensions="inc,php,module" --standard=Drupal --ignore=*views_handler* --ignore=*/vendor/* --ignore=web/sites/all/modules/custom/one_time_exception/* --ignore=web/sites/all/modules/custom/piper_def_last_custom_exception/* --ignore=web/sites/all/modules/custom/piper_mail/includes/MimeMailSystem__SmtpMailSystem.mail.inc --ignore=web/sites/all/modules/contrib/piper_commerce/tests/ web/sites/all/modules/custom"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment