-
Notifications
You must be signed in to change notification settings - Fork 1
/
pre-commit.sh
116 lines (85 loc) · 2.56 KB
/
pre-commit.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/sh
#########################
# #
# Initializing #
# #
#########################
PHPCS_BIN=./vendor/bin/phpcs
PHPCBF_BIN=./vendor/bin/phpcbf
# Check for PHPCS / PHPCBF
if [ ! -x $PHPCS_BIN ]; then
echo "[Vizir] PHP CodeSniffer is not installed locally."
echo "[Vizir] Please run 'composer install' or check the path: $PHPCS_BIN"
exit 1
fi
if [ ! -x $PHPCBF_BIN ]; then
echo "[Vizir] PHP Code Beautifier and Fixer is not installed locally."
echo "[Vizir] Please run 'composer install' or check the path: $PHPCBF_BIN"
exit 1
fi
PHPMD_BIN=./vendor/bin/phpmd
# Check for PHPMD
if [ ! -x $PHPMD_BIN ]; then
echo "[Vizir] PHP Mess Detect is not installed locally."
echo "[Vizir] Please run 'composer install' or check the path: $PHPMD_BIN"
exit 1
fi
PHPUNIT_BIN=./vendor/bin/phpunit
# Check for PHPUNIT
if [ ! -x $PHPUNIT_BIN ]; then
echo "[Vizir] PHPUnit is not installed locally."
echo "[Vizir] Please run 'composer install' or check the path: $PHPUNIT_BIN"
exit 1
fi
#########################
# #
# Starting #
# #
#########################
PROJECT=$(git rev-parse --show-toplevel)
# All files in staging area (no deletions)
FILES=$(git diff --cached --name-only --diff-filter=ACMR HEAD | grep .php)
if [ "$FILES" != "" ]
then
# Testing
echo "[Vizir] Running tests with PHPUNIT..."
$PHPUNIT_BIN ./tests
if [ $? != 0 ]
then
echo "[Vizir] Fix errors before commit."
exit 1
fi
# Coding Standards
echo "[Vizir] Checking PHPCS..."
$PHPCS_BIN -n $FILES
if [ $? != 0 ]
then
echo "[Vizir] Coding standards errors have been detected."
echo "[Vizir] Running PHP Code Beautifier and Fixer..."
$PHPCBF_BIN -n $FILES
echo "[Vizir] Checking PHPCS again..."
$PHPCS_BIN -n $FILES
if [ $? != 0 ]
then
echo "[Vizir] PHP Code Beautifier and Fixer wasn't able to solve all problems."
echo "[Vizir] Run 'composer lint' to check all errors and fix manually."
exit 1
fi
echo "[Vizir] All errors are fixed automatically."
git add $FILES
else
echo "[Vizir] No errors found."
fi
# Mess Detector
echo "\n[Vizir] Checking PHPMD...\n"
for FILE in $FILES
do
$PHPMD_BIN $PROJECT/$FILE text phpmd.xml
if [ $? != 0 ]
then
echo "\n[Vizir] Fix errors before commit."
exit 1
fi
done
fi
exit $?