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