This repository has been archived by the owner on Mar 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
entrypoint.sh
executable file
·292 lines (263 loc) · 7.43 KB
/
entrypoint.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
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
#!/bin/bash
ary=()
# Script configurations
SCRIPT="kicad-exports"
# Mandatory arguments
margs=1
# Arguments and their default values
CONFIG=""
BOARD=""
SCHEMA=""
SKIP=""
DIR=""
OVERWRITE=""
VERBOSE=""
COMMIT=""
if ! [ $CI ]; then
DISPLAY=":0"
fi
# Exit error code
EXIT_ERROR=1
function msg_example {
echo -e "example: $SCRIPT -d docs -b example.kicad_pcb -e example.sch -c docs.kibot.yaml"
}
function msg_usage {
echo -e "usage: $SCRIPT [OPTIONS]... -c <yaml-config-file>"
}
function msg_disclaimer {
echo -e "This is free software: you are free to change and redistribute it"
echo -e "There is NO WARRANTY, to the extent permitted by law.\n"
echo -e "See <https://github.com/nerdyscout/kicad-exports>."
}
function msg_version {
echo -e "kicad-exports version: $BUILD"
}
function msg_illegal_arg {
echo -e "$SCRIPT: illegal option $@"
}
function msg_help {
echo -e "Mandatory arguments:"
echo -e " -c, --config FILE .kibot.yaml config file"
echo -e "\nOptional control arguments:"
echo -e " -d, --dir DIR output path. Default: current dir, will be used as prefix of dir configured in config file"
echo -e " -b, --board FILE .kicad_pcb board file. Default: first board file found in current folder."
echo -e " -e, --schema FILE .sch schematic file. Default: first schematic file found in current folder."
echo -e " -s, --skip Skip preflights, comma separated or 'all'"
echo -e " -o, --overwrite parameter of config file key=value"
echo -e "\nMiscellaneous:"
echo -e " -v, --verbose annotate program execution"
echo -e " -x, --diff HASH output differntial files"
echo -e " -h, --help display this message and exit"
echo -e " -V, --version output version information and exit"
}
function msg_more_info {
echo -e "Try '$SCRIPT --help' for more information."
}
function helpme {
msg_usage
echo ""
msg_help
echo ""
msg_example
echo ""
msg_disclaimer
}
function version {
msg_version
echo ""
msg_disclaimer
}
function illegal_arg {
msg_illegal_arg "$@"
echo ""
msg_usage
echo ""
msg_example
echo ""
msg_more_info
}
function usage {
msg_usage
echo ""
msg_more_info
}
# Ensures that the number of passed args are at least equals
# to the declared number of mandatory args.
# It also handles the special case of the -h or --help arg.
function margs_precheck {
if [ "$1" -lt "$margs" ]; then
if [ "$2" == "--help" ] || [ "$2" == "-h" ]; then
helpme
elif [ "$2" == "--version" ] || [ "$2" == "-V" ]; then
version
else
usage
fi
exit $EXIT_ERROR
fi
}
# Ensures that all the mandatory args are not empty
function margs_check {
if [ "$#" -lt "$margs" ]; then
usage
exit $EXIT_ERROR
fi
}
function add_config {
if [ -f $1 ]; then
ary+=("-c $1")
elif [ -f "/opt/kibot/config/$1" ]; then
ary+=("-c /opt/kibot/config/$1")
else
echo "config file '$1' not found! Please pass own file or choose from:"
ls /opt/kibot/config/*.yaml
exit $EXIT_ERROR
fi
}
function args_process {
while [ -n "$1" ]; do
case "$1" in
-c | --config ) shift
if [[ "$1" == *.kibot.yml || "$1" == *.kibot.yaml ]]; then
# only one config given
add_config "$1"
# multiple configs given
while [[ "$2" == *.kibot.yml || "$2" == *.kibot.yaml ]]; do
add_config "$2"
shift
done
elif [[ "$1" == *.kibot.lst ]]; then
for CONFIG in $(cat $1) ; do
add_config $CONFIG
done
fi
;;
-b | --board ) shift
if [ -f $1 ]; then
BOARD="-b $1"
elif [ -z $1 ]; then
BOARD=""
else
echo "error: board $1 does not exist"
exit $EXIT_ERROR
fi
;;
-e | --schematic ) shift
if [ -f $1 ]; then
SCHEMA="-e $1"
elif [ -z $1 ]; then
SCHEMA=""
else
echo "error: schmatic $1 does not exist"
exit $EXIT_ERROR
fi
;;
-d | --dir) shift
DIR="$1"
;;
-s | --skip) shift
SKIP="-s $1"
;;
-o | --overwrite) shift
OVERWRITE="-g $1"
;;
-x | --diff) shift
COMMIT="$1"
;;
-v | --verbose)
if [ $CI ]; then
shift
if [ "$1" == "1" ]; then
VERBOSE="-v"
elif [ "$1" == "2" ]; then
VERBOSE="-vv"
elif [ "$1" == "3" ]; then
VERBOSE="-vvv"
elif [ "$1" == "4" ]; then
VERBOSE="-vvvv"
else
VERBOSE=""
fi
else
VERBOSE="-v"
fi
;;
-vv )
VERBOSE="-vv"
;;
-vvv )
VERBOSE="-vvv"
;;
-vvvv )
VERBOSE="-vvvv"
;;
-h | --help )
helpme
exit
;;
-V | --version)
version
exit
;;
*)
illegal_arg "$@"
exit $EXIT_ERROR
;;
esac
shift
done
}
function run {
if [ -d .git ]; then
# kicad-git-filters - https://github.com/INTI-CMNB/kicad-git-filters/
filter="/opt/git-filters/kicad-git-filters.py"
if [ -f $filter ]; then
python3 $filter
else
echo -e "warning: $filter not found!"
exit $EXIT_ERROR
fi
# kicad-diff - https://github.com/Gasman2014/KiCad-Diff
if [ $COMMIT ]; then
kicad_diff="/opt/kicad-diff/kidiff_linux.py"
if git cat-file -e $COMMIT; then
if [ -f $kicad_diff ]; then
$kicad_diff --display $DISPLAY -b $COMMIT --scm Git --webserver-disable $BOARD
if [ $DIR ]; then
mv -f $(dirname $BOARD"/kidiff") $DIR
fi
exit 0
else
echo -e "warning: $kicad_diff not found!"
exit $EXIT_ERROR
fi
fi
fi
else
if [ $COMMIT ]; then
echo "please run from root of git repository"
fi
fi
# kibot - https://github.com/INTI-CMNB/kibot
if [ $DIR ]; then
DIR="-d $DIR"
fi
for CONFIG in "${ary[@]}" ; do
kibot $CONFIG $DIR $BOARD $SCHEMA $SKIP $OVERWRITE $VERBOSE && ERR=$?
if [ $ERR ]; then
EXIT_ERROR=$ERR
fi
done
return $EXIT_ERROR
}
function main {
margs_precheck "$#" "$1"
args_process "$@" && msg_version
run && return $?
}
# Removes quotes
args=$(xargs <<<"$@")
# Arguments as an array
IFS=' ' read -r -a args <<< "$args"
# Run main
main "${args[@]}"