|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Check code style via '.php-cs-fixer.php' |
| 4 | + |
| 5 | +EXECUTABLE_NAME=php-cs-fixer |
| 6 | +EXECUTABLE_COMMAND=fix |
| 7 | +CONFIG_FILE=.php-cs-fixer.php |
| 8 | +CONFIG_FILE_PARAMETER='--config' |
| 9 | +ROOT=`pwd` |
| 10 | +ESC_SEQ="\x1b[" |
| 11 | +COL_RESET=$ESC_SEQ"39;49;00m" |
| 12 | +COL_RED=$ESC_SEQ"0;31m" |
| 13 | +COL_GREEN=$ESC_SEQ"0;32m" |
| 14 | +COL_YELLOW=$ESC_SEQ"0;33m" |
| 15 | +COL_BLUE=$ESC_SEQ"0;34m" |
| 16 | +COL_MAGENTA=$ESC_SEQ"0;35m" |
| 17 | +COL_CYAN=$ESC_SEQ"0;36m" |
| 18 | + |
| 19 | +echo "" |
| 20 | +printf "$COL_YELLOW%s$COL_RESET\n" "Running pre-commit hook: \"php-cs-fixer\"" |
| 21 | + |
| 22 | +# possible locations |
| 23 | +locations=( |
| 24 | + $ROOT/bin/$EXECUTABLE_NAME |
| 25 | + $ROOT/vendor/bin/$EXECUTABLE_NAME |
| 26 | +) |
| 27 | + |
| 28 | +for location in ${locations[*]} |
| 29 | +do |
| 30 | + if [[ -x $location ]]; then |
| 31 | + EXECUTABLE=$location |
| 32 | + break |
| 33 | + fi |
| 34 | +done |
| 35 | + |
| 36 | +if [[ ! -x $EXECUTABLE ]]; then |
| 37 | + echo "executable $EXECUTABLE_NAME not found, exiting..." |
| 38 | + echo "if you're sure this is incorrect, make sure they're executable (chmod +x)" |
| 39 | + exit |
| 40 | +fi |
| 41 | + |
| 42 | +echo "using \"$EXECUTABLE_NAME\" located at $EXECUTABLE" |
| 43 | +$EXECUTABLE --version |
| 44 | + |
| 45 | +if [[ -f $ROOT/$CONFIG_FILE ]]; then |
| 46 | + CONFIG=$ROOT/$CONFIG_FILE |
| 47 | + echo "config file located at $CONFIG loaded" |
| 48 | +fi |
| 49 | + |
| 50 | +FILES=`git status --porcelain | grep -e '^[AM]\(.*\).php$' | cut -c 3- | sed -e "s/_ide_helper.php$//" | sed -e "s/_ide_helper_models.php$//" | sed -e "s/.phpstorm.meta.php$//" | tr '\n' ' ' | sed 's/ *$//g'` |
| 51 | +if [ -z "$FILES" ]; then |
| 52 | + echo "No php files found to fix." |
| 53 | +else |
| 54 | + echo "Fixing files ${FILES} in project"; |
| 55 | + if [[ -f $CONFIG ]]; then |
| 56 | + $EXECUTABLE $EXECUTABLE_COMMAND $CONFIG_FILE_PARAMETER=$CONFIG ${FILES}; |
| 57 | + else |
| 58 | + $EXECUTABLE $EXECUTABLE_COMMAND ${FILES}; |
| 59 | + fi |
| 60 | + git add ${FILES} |
| 61 | +fi |
0 commit comments