forked from Shaked/php.tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp.tools
executable file
·404 lines (378 loc) · 10.7 KB
/
php.tools
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/bin/bash
# Copyright (c) 2014, Carlos C
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
which git &> /dev/null
if [ 0 != $? ];
then
echo "This tool needs Git to work."
echo "Please, install git using:"
echo " sudo yum install git"
echo "or"
echo " sudo apt-get install git"
exit 255;
fi
ARCH=${BASH_VERSINFO[5]}
CURPWD=$(php -r "echo realpath('.');")
if [[ $ARCH == *apple* ]]; then
DIRS="`find . -depth 1 -type d | egrep -iv "cover|vendor|web|fixtures|docs|^\.$" | php -r "echo implode(' ', array_map(function(\\\$v){return str_replace('$CURPWD', '.',realpath(\\\$v)); }, array_filter(explode(PHP_EOL, file_get_contents('php://stdin')))));"`"
FMT_CLEAN="find . -ipath '*~' -delete"
INCREMENTAL_TEST="find . -ipath '*~' -mmin -10 | grep -iv '_test.php'"
else
DIRS="`find -maxdepth 1 -type d | egrep -iv "cover|vendor|web|fixtures|docs|^\.$" | xargs -l1 readlink -f | tr "\\n" " " | sed "s|$CURPWD/|./|g"`"
FMT_CLEAN="find -iwholename '*~' -delete"
INCREMENTAL_TEST="find $DIRS -iwholename '*.php' -mmin -10 | grep -iv '_test.php'"
fi
FMTWORKERS=2
CLOC=false
which cloc &> /dev/null
if [ 0 == $? ];
then
CLOC=true
CLOCBIN=`which cloc`
fi
which ./cloc &> /dev/null
if [ 0 == $? ];
then
CLOC=true
CLOCBIN=`which ./cloc`
fi
CTAGS=false
which ctags &> /dev/null
if [ 0 == $? ];
then
CTAGS_VERSION=$(ctags --version)
if [[ $CTAGS_VERSION == *Exuberant* ]];
then
CTAGS=true
fi
fi
PHPUNIT=false
which vendor/bin/phpunit &> /dev/null
if [ 0 == $? ];
then
PHPUNIT=true
fi
FMT=false
if [ -e fmt.php ];
then
FMT=true
fi
if [ -e vendor/bin/fmt.php ];
then
FMT=true
fi
PHPDOC=false
if [ -e vendor/bin/phpdoc.php ];
then
PHPDOC=true
fi
function execute(){
echo "$0 [command]"
echo " lint - run lint on changed files"
echo " lint all - run lint on all files"
$CTAGS && echo " ctags - generate ctags"
$PHPUNIT && echo " test - execute PHPUnit"
$PHPUNIT && echo " cover - execute PHPUnit with cover output"
$PHPDOC && echo " doc - execute phpDocumentor"
$CLOC && echo " cloc - execute script to count lines of code"
$CTAGS && echo " watch ctags - execute PHPUnit, but keeps watching for file changes to trigger ctags generator"
$PHPUNIT && echo " watch test - execute PHPUnit, but keeps watching for file changes to trigger the test automatically"
$PHPUNIT && echo " watch cover - execute PHPUnit with cover output, but keeps watching for file changes to trigger the test automatically"
$PHPDOC && echo " watch doc - execute phpDocumentor, but keeps watching for file changes to trigger the generation automatically"
$CLOC && echo " watch doc - execute script to count lines of code, but keeps watching for file changes to trigger the count automatically"
$FMT && echo " fmt [filename] - format filename according to project formatting rules"
$FMT && echo " fmt all - format all files according to project formatting rules"
$FMT && echo " fmt clean - remove all backup files - *~"
$FMT && echo " watch fmt [all|filename] - watch for changes and format according to project formatting rules"
if [ !$CLOC ];
then
echo ""
echo " download cloc from http://cloc.sourceforge.net/"
fi
if [ !$PHPUNIT ];
then
echo ""
echo " add phpunit to composer.json:"
echo ' phpunit/phpunit": "4.0.*"'
fi
if [ !$PHPDOC ];
then
echo ""
echo " add phpdoc to composer.json:"
echo ' "phpdocumentor/phpdocumentor": "2.4.*"'
fi
if [ !$CTAGS ];
then
echo ""
echo " install exuberant ctags:"
echo " http://ctags.sourceforge.net/"
fi
exit 0
}
function update_phpunit_xml(){
#processIsolation=\"true\"\
phpunit_xml_template="\
<phpunit\
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\
xsi:noNamespaceSchemaLocation=\"http://schema.phpunit.de/3.7/phpunit.xsd\"\
colors=\"true\"\
verbose=\"false\">\
<testsuite>\
"
for i in $DIRS;
do
phpunit_xml_template="$phpunit_xml_template<directory suffix=\"_test.php\">$i</directory>"
done;
phpunit_xml_template="$phpunit_xml_template</testsuite>"
phpunit_xml_template="$phpunit_xml_template<filter>"
phpunit_xml_template="$phpunit_xml_template<whitelist processUncoveredFilesFromWhitelist=\"true\" >"
for i in $DIRS;
do
phpunit_xml_template="$phpunit_xml_template<directory suffix=\".php\">$i</directory>"
done;
phpunit_xml_template="$phpunit_xml_template<exclude>"
for i in $DIRS;
do
phpunit_xml_template="$phpunit_xml_template<directory suffix=\"_test.php\">$i</directory>"
done;
phpunit_xml_template="$phpunit_xml_template</exclude>"
phpunit_xml_template="$phpunit_xml_template</whitelist>"
phpunit_xml_template="$phpunit_xml_template</filter></phpunit>"
echo $phpunit_xml_template > phpunit.xml
}
WATCH=""
if [ "watch" == "$1" ];
then
WATCH="watch"
shift
fi
$CLOC && if [ "cloc" == "$1" ] ;
then
function execute(){
echo $DIRS;
nice -n 20 $CLOCBIN $DIRS;
}
fi;
$CTAGS && if [ "ctags" == "$1" ] ;
then
function execute(){
echo $DIRS;
nice -n 20 ctags --PHP-kinds=+cf \
--regex-PHP='/define\("([^ ]*)"/\1/d/' \
--regex-PHP="/define\('([^ ]*)'/\1/d/" \
--regex-PHP="/const ([^ ]*)/\1/d/" \
--regex-PHP='/trait ([^ ]*)/\1/c/' \
--regex-PHP='/final class ([^ ]*)/\1/c/' \
--regex-PHP='/final abstract class ([^ ]*)/\1/c/' \
--regex-PHP='/abstract class ([^ ]*)/\1/c/' \
--regex-PHP='/interface ([^ ]*)/\1/c/' \
--regex-PHP='/(public |static |abstract |protected |private |final public )+function ([^ (]*)/\2/f/' -R -f .tags-new $DIRS;
(cat .tags-new | sort -k1) &> .tags_sorted_by_file
mv .tags-new .tags;
}
fi;
$PHPUNIT && if [ "test" == "$1" ] ;
then
update_phpunit_xml
shift
function execute(){
TEST=""
if [[ "incremental" == "$1" ]]; then
shift
FILES=`eval $INCREMENTAL_TEST`
for i in $FILES;
do
echo -e "\n---\n\n\e[32m\e[7mTesting " ${i/.php/} "\e[27m\e[0m"
execute ${i/.php/} "$@"
done;
return 1
elif [[ $1 == */* ]]; then
TEST="$1Test $1_test.php"
shift
elif [[ $1 == *\\* ]]; then
FN=${1//\\/\/}
TEST="$1Test ${FN}_test.php"
shift
fi
vendor/bin/phpunit $TEST "$@"
}
fi;
$PHPUNIT && if [ "cover" == "$1" ] ;
then
update_phpunit_xml
shift
function execute(){
TEST=""
if [[ "incremental" == "$1" ]]; then
shift
FILES=`eval $INCREMENTAL_TEST`
for i in $FILES;
do
echo -e "\n---\n\n\e[32m\e[7mTesting " ${i/.php/} "\e[27m\e[0m"
execute ${i/.php/} "$@"
done;
return 1
elif [[ $1 == */* ]]; then
TEST="$1Test $1_test.php"
shift
elif [[ $1 == *\\* ]]; then
FN=${1//\\/\/}
TEST="$1Test ${FN}_test.php"
shift
fi
vendor/bin/phpunit --coverage-text --coverage-html=cover/ --coverage-clover=clover.xml --log-junit=junit.xml $TEST "$@"
}
fi;
$PHPDOC && if [ "doc" == "$1" ] ;
then
function execute(){
NEW_DIRS=""
for i in $DIRS;
do
NEW_DIRS="-d $i $NEW_DIRS"
done;
php vendor/bin/phpdoc.php $NEW_DIRS -t docs/
}
fi;
if [ "lint" == "$1" ];
then
if [ "" == "$2" ];
then
function execute(){
echo "Differential linting..."
which parallel &> /dev/null
if [ 0 == $? ];
then
git status -s | grep -i "\.php$" | awk -F ' ' '{ print $2 }' | parallel --no-notice php -l
else
files=$( git status -s | grep -i "\.php$" | awk -F ' ' '{ print $2 } ' )
for line in $files
do
(php -l $line &> /dev/null) || (php -l $line) && echo "$line"
done;
fi;
}
fi;
if [ "all" == "$2" ];
then
function execute(){
echo "Full linting..."
which parallel &> /dev/null
if [ 0 == $? ];
then
find . -ipath "*.php" | parallel --no-notice php -l
else
echo 'Needs GNU Parallel'
fi;
}
fi;
fi;
$FMT && if [ "fmt" == "$1" ];
then
if [ "" == "$2" ];
then
function execute(){
echo "Starting $FMTWORKERS workers..."
J=""
files=$( git status -s | grep -i "\.php$" | awk -F ' ' '{ print $2 } ' )
for line in $files
do
(php -l $line &> /dev/null) && (cp $line $line~; php fmt.php $line > $line-formatted; (php -l $line-formatted &> /dev/null) && mv -f $line-formatted $line) && echo "Formatted $line" &
J="$J $!"
JCNT=`echo $J | wc -w`
if [ $JCNT -ge $FMTWORKERS ];
then
for jj in $J;
do
wait $jj;
done;
J=""
fi
done;
for jj in $J;
do
wait $jj;
done;
}
elif [ "clean" == "$2" ];
then
function execute(){
eval $FMT_CLEAN
}
elif [ "all" == "$2" ];
then
function execute(){
echo "Starting $FMTWORKERS workers..."
J=""
for i in $DIRS;
do
files=`find $i -ipath "*.php"`
for line in $files
do
( (php -l $line &> /dev/null) && (cp $line $line~; php fmt.php $line > $line-formatted; (php -l $line-formatted &> /dev/null) && mv -f $line-formatted $line) && echo "Formatted $line" ) &
J="$J $!"
JCNT=`echo $J | wc -w`
if [ $JCNT -ge $FMTWORKERS ];
then
for jj in $J;
do
wait $jj;
done;
J=""
fi
done;
done;
for jj in $J;
do
wait $jj;
done;
}
else
line=$2
function execute(){
(php -l $line &> /dev/null) && (cp $line $line~; php fmt.php $line > $line-formatted; (php -l $line-formatted &> /dev/null) && mv -f $line-formatted $line) && echo "Formatted $line"
}
fi
fi
sha=0
previous_sha=0
update_sha () {
command="ls -lR $DIRS | sha1sum"
sha=`bash -c "$command"`
}
changed () {
echo "Running... $1 $2";
time execute "$@"
previous_sha=$sha
}
compare () {
update_sha
if [[ $sha != $previous_sha ]] ;
then
changed "$@";
fi
}
watch () {
while true; do
compare "$@"
read -s -t 1 && (
execute "$@"
)
done
}
if [ "watch" == "$WATCH" ];
then
watch "$@"
else
time execute "$@"
fi