Created
June 21, 2017 11:23
-
-
Save petrabarus/c08621f4cfbbe51aba486f75f8331e9f to your computer and use it in GitHub Desktop.
Wrapper Script for PHP, CSS, and JS style check in pre-commit
This file contains hidden or 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 | |
############################################################################### | |
# Check style for list of code | |
# For source code | |
# - PHP | |
# - JS (js, es6, jsx) | |
# - CSS (css, scss, less) | |
# This can be used in build server or pre-commit hook in git, depends on the | |
# SOURCE_FILE variable. | |
############################################################################### | |
# Binaries | |
PHP_BIN=${PHP_BIN:-'php'} | |
PHPCS_BIN=${PHPCS_BIN:-'./vendor/bin/phpcs'} | |
ESLINT_BIN=${ESLINT_BIN:-'./node_modules/.bin/eslint'} | |
STYLELINT_BIN=${STYLELINT_BIN:-'./node_modules/.bin/stylelint'} | |
# Getting list of files. Default is diff-ing with HEAD. | |
SOURCE_FILES=${SOURCE_FILES:-`git diff-index --name-only --diff-filter=ACMR HEAD`} | |
if [ "$SOURCE_FILES" == "" ]; then | |
exit 0 | |
fi | |
# Create temporary copy of staging area and move all Added, Copied, Modified, Renamed. | |
TMP_STAGING='./tmp_staging' | |
FILE_PATTERN="\.(php|phtml|js|jsx|es6|css|scss|less)$" | |
if [ -e $TMP_STAGING ]; then | |
rm -rf $TMP_STAGING | |
fi | |
mkdir $TMP_STAGING | |
for FILE in $SOURCE_FILES; do | |
echo "$FILE" | egrep -q "$FILE_PATTERN" | |
RETVAL=$? | |
if [ "$RETVAL" -eq "0" ]; then | |
cp --parents $FILE $TMP_STAGING | |
fi | |
done | |
######################################################## | |
# PHP | |
######################################################## | |
echo "Checking PHP..." | |
ERRORS_BUFFER="" | |
PHP_FILE_PATTERN='.(php|phtml)$' | |
for FILE in `find $TMP_STAGING | grep -E $PHP_FILE_PATTERN`; do | |
ERRORS=$(${PHP_BIN} -l $FILE 2>&1 | grep "Parse error") | |
ERRORS_BUFFER="$ERRORS_BUFFER\n$ERRORS" | |
done | |
ERRORS_BUFFER="$(echo -e "${ERRORS_BUFFER}" | sed -e 's/^[[:space:]]*//')" | |
if [ -n "$ERRORS_BUFFER" ]; then | |
echo "Found PHP parse errors: " | |
echo -e $ERRORS_BUFFER | |
echo "PHP parse errors found. Fix errors and commit again." | |
exit 1 | |
fi | |
PHPCS_CODING_STANDARD=./etc/phpcs/ruleset.xml | |
PHPCS_OPTS=${PHPCS_OPTS:-''} | |
PHP_STAGED_FILES='' | |
for FILE in `find $TMP_STAGING -type f | grep -E $PHP_FILE_PATTERN`; do | |
PHP_STAGED_FILES="$PHP_STAGED_FILES $FILE" | |
done | |
PHPCS_RETVAL=0 | |
if [ "$PHP_STAGED_FILES" != "" ]; then | |
PHPCS_OUTPUT=$(${PHPCS_BIN} $PHPCS_OPTS --standard=$PHPCS_CODING_STANDARD $PHP_STAGED_FILES) | |
PHPCS_RETVAL=$? | |
fi | |
######################################################## | |
# JS | |
# The config file should be stored in one directory | |
# config.js.yml, config.es6.yml, config.jsx.yml | |
# https://github.com/eslint/eslint/pull/8081 | |
######################################################## | |
echo "Checking JS..." | |
JS_EXT=(js jsx es6) | |
ESLINT_OUTPUT="" | |
ESLINT_STANDARD_DIR=./etc/eslint/ | |
ESLINT_RETVAL=0 | |
for EXT in "${JS_EXT[@]}"; | |
do | |
FILES=$(find $TMP_STAGING -type f | grep -E ".${EXT}$") | |
if [ "$FILES" == "" ]; then | |
continue | |
fi | |
ESLINT_EXT_ERRORS=$(${ESLINT_BIN} --no-ignore --no-color -c $ESLINT_STANDARD_DIR/config.${EXT}.yml $FILES) | |
ESLINT_EXT_RETVAL=$? | |
ESLINT_OUTPUT="$ESLINT_OUTPUT\n$ESLINT_EXT_ERRORS" | |
ESLINT_RETVAL=$((ESLINT_RETVAL+ESLINT_EXT_RETVAL)) | |
done | |
######################################################## | |
# CSS | |
######################################################## | |
echo "Checking CSS..." | |
STYLELINT_OPTS=${STYLELINT_OPTS:-''} | |
CSS_FILE_PATTERN='.(css|scss|less)$' | |
CSS_FILES=`find $TMP_STAGING -type f | grep -E $CSS_FILE_PATTERN` | |
STYLELINT_RETVAL=0 | |
if [ "$CSS_FILES" != "" ]; then | |
STYLELINT_OUTPUT=$(${STYLELINT_BIN} $STYLELINT_OPTS $CSS_FILES) | |
STYLELINT_RETVAL=$? | |
fi | |
################################################################################ | |
# Displaying all | |
################################################################################ | |
rm -rf $TMP_STAGING | |
if [ $PHPCS_RETVAL -ne 0 ]; then | |
echo "$PHPCS_OUTPUT" | |
fi | |
if [ $ESLINT_RETVAL -ne 0 ]; then | |
echo "$ESLINT_OUTPUT" | |
fi | |
if [ $STYLELINT_RETVAL -ne 0 ]; then | |
echo "$STYLELINT_OUTPUT" | |
fi | |
RETVAL=$(($PHPCS_RETVAL+$ESLINT_RETVAL+$STYLELINT_RETVAL)) | |
exit $RETVAL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment