This repository has been archived by the owner on Oct 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
coke
executable file
·258 lines (221 loc) · 5.26 KB
/
coke
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
#!/usr/bin/env bash
file=".coke"
params=""
options=""
standard=""
standardPath=""
command="phpcs";
commandPath=""
composerPath="vendor/squizlabs/php_codesniffer/scripts/"
filesAllowed=""
filesIgnored=""
ignored=0
allowed=0
verbose=0
fix=0
onlyGitChanged=0
function allow
{
if [ -e "$1" ]
then
if [ -z "$filesAllowed" ]
then
filesAllowed="$1"
else
filesAllowed="$filesAllowed $1"
fi
fi
}
function ignore
{
if [ -z "$filesIgnored" ]
then
filesIgnored="$1"
else
filesIgnored="$filesIgnored,$1"
fi
}
function output
{
OUTPUT="[%s%s$1%s] %s$2%s"
if [ -t 1 ] && [ $- != "*i*" ] && [ -n "$TERM" ] && which tput >/dev/null
then
OUTPUT=$(printf "$OUTPUT" "$(tput bold)" "$(tput setaf $3)" "$(tput sgr0)" "$(tput setaf $4)" "$(tput sgr0)" )
else
OUTPUT=$(printf "$OUTPUT" "" "" "" "" "" )
fi
echo "$OUTPUT"
}
function success
{
output "SUCCESS" "$1" 2 6
exit 0
}
function fail
{
output "FAIL" "$1" 1 6
exit 1
}
function warning
{
output "WARNING" "$1" 3 6
exit 0
}
function info
{
output "INFO" "$1" 4 6
}
function debug
{
if [ "$verbose" -eq 1 ]
then
info "$1"
fi
}
# Resolve options
while [ $# -ne 0 ]
do
CUR_PARAM="$1"
if [ "${CUR_PARAM:0:2}" = "--" ] # it's a parameter
then
if [ "${CUR_PARAM:2:9}" = "standard=" ]
then
standard="${CUR_PARAM:11}"
elif [ "${CUR_PARAM:2:7}" = "ignore=" ]
then
ignore "${CUR_PARAM:9}"
ignored=1
elif [ "${CUR_PARAM:2:14}" = "standard-path=" ]
then
standardPath="${CUR_PARAM:16}"
elif [ "${CUR_PARAM:2:5}" = "fix" ]
then
fix=1
elif [ "${CUR_PARAM:2:16}" = "only-git-changed" ]
then
onlyGitChanged=1
else
params="$params $CUR_PARAM"
fi
elif [ "${CUR_PARAM:0:1}" = "-" ] # it's an option
then
case "${CUR_PARAM:1:1}" in
v)
verbose=1
;;
f)
fix=1
;;
esac
options="$options ${CUR_PARAM}"
else # it's a file
allow "$CUR_PARAM"
allowed=1
fi
shift
done
# Resolve commands path to use
if [ -d "$composerPath" ]
then
debug "Command path used is \"$composerPath\""
commandPath="$composerPath"
fi
if [ $fix -eq 1 ]
then
debug "Use phpcbf command instead of phpcs"
command="phpcbf"
fi
# Resolve files to test
if [ -e "$file" ]
then
output=$( grep "^[^\#]" $file )
for ligne in $output
do
if [ "${ligne:0:8}" = "command=" ]
then
command="${ligne:8}"
elif [ -z "$standardPath" ] && [ "${ligne:0:14}" = "standard-path=" ]
then
standardPath="${ligne:14}"
elif [ -z "$standard" ] && [ "${ligne:0:9}" = "standard=" ]
then
standard="${ligne:9}"
elif [ "$verbose" -eq 0 ] && [ "${ligne:0:8}" = "verbose=" ] && [ "${ligne:8}" = "true" ]
then
verbose=1
elif [ "$onlyGitChanged" -eq 0 ] && [ "${ligne:0:17}" = "only-git-changed=" ] && [ "${ligne:17}" = "true" ]
then
onlyGitChanged=1
elif [ "$ignored" -eq 0 ] && [ "${ligne:0:1}" = "!" ]
then
ignore "${ligne:1}"
elif [ "$allowed" -eq 0 ] && [ -n "$ligne" ]
then
allow "$ligne"
fi
done
else
warning "No config file found"
fi
debug "Starting sniffing"
# Prepare command
if [ -n "$filesIgnored" ]
then
debug "Ignore files \"$filesIgnored\""
filesIgnored="--ignore=$filesIgnored"
fi
if [ -n "$standard" ]
then
debug "Standard used is \"$standard\""
standard="--standard=$standard"
else
fail "No coding standard definition provided"
fi
if [ -n "$standardPath" ]
then
debug "Change standard path to \"$standardPath\""
$commandPath$command --config-set installed_paths $standardPath
fi
# Check Git changes
if [ $onlyGitChanged -eq 1 ]
then
command -v git >/dev/null 2>&1 || { fail "Cannot find git. Git is required when --only-git-changed is used."; }
filesAllowedAndChanged=""
for file in $(git --no-pager diff --name-only --diff-filter=MARC)
do
allowed=0
for ligne in $filesAllowed
do
if [ "${ligne:0:1}" = '!' ] && [[ "$file" == *"${ligne:1}"* ]]
then
continue 2
elif [[ "$file" == "$ligne"* ]]
then
allowed=1
fi
done
if [ "$allowed" -eq 1 ]
then
filesAllowedAndChanged="$filesAllowedAndChanged $file"
fi
done
filesAllowed=$filesAllowedAndChanged
fi
# Punch it!
if [ -n "$filesAllowed" ]
then
debug "Files allowed : \"$filesAllowed\""
$commandPath$command $filesAllowed $filesIgnored $standard $options $params
if [ $? -ne 0 ]
then
fail "Some files do not match with \"${standard:11}\" requirements"
fi
else
warning "There was no file to test"
fi
success "All files match with \"${standard:11}\" requirements"
if [ -n "$standardPath" ]
then
debug "Reset standard path"
$commandPath$command --config-delete installed_paths
fi