forked from BedrockStreaming/Coke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoke
executable file
·208 lines (174 loc) · 3.9 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
#!/usr/bin/env bash
file=".coke"
params=""
options=""
standard=""
standardPath=""
composerCommand="vendor/squizlabs/php_codesniffer/scripts/phpcs"
command="phpcs";
if [ -e "$composerCommand" ]
then
command="$composerCommand"
fi
filesAllowed=""
filesIgnored=""
ignored=0
allowed=0
verbose=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*" ] && 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
}
# Résolution des 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}"
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
;;
esac
options="$options ${CUR_PARAM}"
else # it's a file
allow "$CUR_PARAM"
allowed=1
fi
shift
done
# 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 [ "$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\""
$command --config-set installed_paths $standardPath
fi
# Punch it!
if [ -n "$filesAllowed" ]
then
debug "Files allowed : \"$filesAllowed\""
$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"
$command --config-delete installed_paths
fi