forked from leoheck/kiri
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkdiff
More file actions
executable file
·882 lines (716 loc) · 22.5 KB
/
kdiff
File metadata and controls
executable file
·882 lines (716 loc) · 22.5 KB
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
#!/bin/bash
show_help()
{
IFS= read -r -d '' HELP_MESSAGE <<-EOM
USAGE :
kdiff [OPTIONS] KICAD_PROJECT_FILE
OPTIONS:
-a Track all commits (slower).
-o HASH Show commits starting from this one.
-n HASH Show commits until this one delimited by this one.
-r Remove kidiff folder before run
-l Do not launch browser at the end
-p PORT Set webserver port
-V Verbose
-h This help
EXAMPLES:
# Kicad project on the root of the repo
kdiff board.pro
# Nested Kicad projects
kdiff nested-project/board.pro -r -V
EOM
echo -e "$HELP_MESSAGE"
}
# ======================
# Custom tools
# ======================
readlink_osx()
{
target_file="$1"
dir_name=$(dirname "$target_file")
cd "${dir_name}" || exit
target_file=$(basename "$target_file")
# Iterate down a (possible) chain of symlinks
while [ -L "$target_file" ]
do
target_file=$(readlink "$target_file")
cd "$(dirname "$target_file")" || exit
target_file=$(basename "$target_file")
done
# Compute the canonicalized name by finding the physical path
# for the directory we're in and appending the target file.
phys_dir=$(pwd -P)
result="$phys_dir/$target_file"
echo "$result"
}
readlink_()
{
case $OSTYPE in
darwin*)
readlink_osx "$1"
;;
*)
readlink -f "$1"
;;
esac
}
tac_()
{
case $OSTYPE in
darwin*)
tail -r
;;
*)
tac
;;
esac
}
sed_()
{
case $OSTYPE in
darwin*)
gsed "$@"
;;
*)
sed "$@"
;;
esac
}
find_()
{
case $OSTYPE in
darwin*)
gfind "$@"
;;
*)
find "$@"
;;
esac
}
# ======================
ctrl_c()
{
echo "** Trapped CTRL-C"
exit 1
}
check_tool() {
tool=$1
if ! command -v "${tool}" &> /dev/null; then
# echo "${tool} could not be found"
#exit
echo 1
return 1
else
echo 0
return 0
fi
}
get_pages()
{
board_name=$1
main_schematic=$2
# First page is this one
pages="${board_name}|${board_name}"
pagenames=$(sed_ "s/\r$//g" "${main_schematic}" | grep "^F0 " | cut -d" " -f2 | tr "\n" "|" | sed_ "s/\"//g" | sed_ "s/|$//g" | tr "|" "\n")
pagepaths=$(sed_ "s/\r$//g" "${main_schematic}" | grep "^F1 " | cut -d" " -f2 | tr "\n" "|" | sed_ "s/\"//g" | sed_ "s/|$//g" | tr "|" "\n")
IFS=$'\n'
n=0
for pagename in $pagenames; do
n=$((n+1))
pagename=$(echo "$pagename" | sed_ "s/^ //g" | sed_ "s/ $//g")
pagepath=$(echo "$pagepaths" | sed_ -n ${n}p)
filename=$(basename "$pagepath" | sed_ "s/.sch//g")
page="${pagename}|${filename}"
pages="${pages}\n${page}"
done
echo -e "$pages"
}
insert_commits()
{
html="$1"
commits="$2"
project="$3"
kicad_pcb="$4"
sch_files=$(echo "$5" | sed_ "s/^[ \t]\+//g" | sed_ "s/[ \t]$\+//g")
# GIT COMMITS FORMAT
#
# fcd81ef | 2021-01-22 17:21:39 -0300 | Leandro Heck | Initial version
# d20b900 | 2021-01-22 16:59:29 -0300 | Leandro Heck | Initial commit
n=0
COMMITS_LIST_HTML=""
IFS=$'\n'
for commit in $commits; do
hash=$(echo "$commit" | cut -d'|' -f1 | sed_ "s/^ //g" | sed_ "s/[ t]\+//g" )
date=$(echo "$commit" | cut -d'|' -f2 | cut -d' ' -f1-2 | sed_ "s/^ //g" )
user=$(echo "$commit" | cut -d'|' -f3 | sed_ "s/^ //g" )
msg=$(echo "$commit" | cut -d'|' -f4 | sed_ "s/^ //g" )
empty_icon='<span class="iconify" style="padding-left: 0px; padding-right 0px; width: 14px; height: 14px; color: rgba(255, 0, 0, 0.5);" data-icon="bx:bx-x" data-inline="false"></span>'
# check for changes in .sch files
if git log --name-only --pretty='format:' -1 -U "${hash}" -- "${project}" | cat | grep -s -E "${sch_files// /|}" &> /dev/null; then
sch_icon='<span class="iconify" style="padding-left: 0px; padding-right 0px; width: 14px; height: 14px; color: #A6E22E;" data-icon="carbon:schematics" data-inline="false"></span>'
else
sch_icon="${empty_icon}"
fi
# check for changes in .kicad_pcb files
if git log --name-only --pretty='format:' -1 -U "${hash}" -- "${project}" | cat | grep -s "${kicad_pcb}" &> /dev/null; then
layout_icon='<span class="iconify" style="padding-left: 0px; padding-right 0px; width: 14px; height: 14px; color: #F92672;" data-icon="codicon:circuit-board" data-inline="false"></span>'
else
layout_icon="${empty_icon}"
fi
# Check for changes in textual files only
if git log --name-only --pretty='format:' -1 -U "${hash}" -- "${project}" | cat | grep -s -vE "${sch_files// /|}${kicad_pcb}" &> /dev/null; then
text_icon='<span class="iconify" style="padding-left: 0px; padding-right 0px; width: 14px; height: 14px; color: #888888;" data-icon="bi:file-earmark-text" data-inline="false"></span>'
else
text_icon="${empty_icon}"
fi
if [[ $n == 0 ]] || [[ $n == 1 ]]; then
checked="checked='checked'"
else
checked=""
fi
IFS= read -r -d '' COMMIT_HTML <<-EOM
<input class="chkGroup" type="checkbox" id="${hash}" name="commit" value="${hash}" ${checked} onchange="update_commits()"/>
<label class="text-sm-left list-group-item" for="${hash}">
<table style="width:100%">
<tr>
<td rowspan=2 style="vertical-align: top; width: 1.8em;">
<svg viewBox="0 0 15 15" fill="none" xmlns="http://www.w3.org/2000/svg" width="15" height="15">
<path d="M7.5 10.5a3 3 0 010-6m0 6a3 3 0 000-6m0 6V15m0-10.5V0" stroke="currentColor"></path>
</svg>
</td>
<td><em class="text-info" style=" line-height: 0.7;">${msg}</em></td>
</tr>
<tr>
<td white-space:nowrap;overflow: hidden;><span class="text-success text-uppercase font-weight-normal">${hash}</span> <span class="text-muted"> | </span> ${sch_icon} ${layout_icon} ${text_icon} <span class="text-muted font-weight-normal"> | ${date} | ${user}</td>
</tr>
</table>
</label>
EOM
n=$((n+1))
COMMITS_LIST_HTML="${COMMITS_LIST_HTML}${COMMIT_HTML}"
done
contents=$(<"${html}")
new_contents=${contents//"<!-- FILL_COMMITS_HERE -->"/${COMMITS_LIST_HTML}}
printf '%s' "${new_contents}" > "${html}"
}
insert_pages()
{
html="$1"
pages="$2"
# FORMAT
# pagename|pagepath.sch
n=0
PAGES_LIST_HTML=""
IFS=$'\n'
for page in $pages; do
page_name=$(echo "$page" | cut -d'|' -f1 | sed_ "s/[ \t]\+//g")
page_filename=$(echo "$page" | cut -d'|' -f2 | sed_ "s/[ \t]\+//g" | sed_ "s/.sch//g")
if [[ $n == 0 ]]; then
checked='checked="checked"'
else
checked=""
fi
IFS= read -r -d '' HTML_SNIPPET <<-EOM
<input type="radio" id="${page_filename}" value="${page_filename}" name="pages" ${checked} onchange="change_page()"/>
<label class="rounded text-sm-left list-group-item radio-box" for="${page_filename}">
<span style="margin-left:0.5em; margin-right:0.1em;" class="iconify" data-icon="gridicons:pages" data-inline="false"></span>
${page_name}
</label>
EOM
n=$((n+1))
PAGES_LIST_HTML="${PAGES_LIST_HTML}${HTML_SNIPPET}"
done
echo " - Updating html"
HTML_CONTENT=$(<"${html}")
HTML_CONTENT_UPDATED=${HTML_CONTENT//"<!-- FILL_PAGES_HERE -->"/${PAGES_LIST_HTML}}
printf '%s' "${HTML_CONTENT_UPDATED}" > "${html}"
}
insert_layers()
{
html="$1"
layers="$2"
n=0
LAYERS_LIST_HTML=""
IFS=$'\n'
for layer in $layers; do
# Fix layer name by replacing "." with "_"
layer=$(sed_ "s/\./_/g" <<< "${layer}")
case $layer in
B*Adhes) layer_color="#3545A8" ;;
B*CrtYd) layer_color="#D3D04B" ;;
B*Cu) layer_color="#359632" ;;
B*Fab) layer_color="#858585" ;;
B*Mask) layer_color="#943197" ;;
B*Paste) layer_color="#969696" ;;
B*SilkS) layer_color="#481649" ;;
Cmts*User) layer_color="#7AC0F4" ;;
Dwgs*User) layer_color="#0364D3" ;;
Eco1*User) layer_color="#008500" ;;
Edge_Cuts) layer_color="#C9C83B" ;;
F*Adhes) layer_color="#A74AA8" ;;
F*CrtYd) layer_color="#A7A7A7" ;;
F*Cu) layer_color="#952927" ;;
F*Fab) layer_color="#C2C200" ;;
F*Mask) layer_color="#943197" ;;
F*Paste) layer_color="#3DC9C9" ;;
F*SilkS) layer_color="#339697" ;;
In1*Cu) layer_color="#C2C200" ;;
In2*Cu) layer_color="#C200C2" ;;
In3*Cu) layer_color="#C20000" ;;
In4*Cu) layer_color="#0000C2" ;;
Margin) layer_color="#D357D2" ;;
*) layer_color="" ;;
esac
if [[ "$n" == "0" ]]; then
checked="checked='checked'"
else
checked=""
fi
IFS= read -r -d '' HTML_SNIPPET <<-EOM
<input type="radio" id="${layer}" name="layers" value="${layer}" ${checked} onchange="change_layer()"/>
<label class="rounded text-sm-left list-group-item radio-box" for="${layer}">
<span style="margin-left:0.5em; margin-right:0.1em; color: ${layer_color}" class="iconify" data-icon="teenyicons-square-solid" data-inline="false"></span>
${layer}
</label>
EOM
n=$((n+1))
LAYERS_LIST_HTML="${LAYERS_LIST_HTML}${HTML_SNIPPET}"
done
echo " - Updating html"
HTML_CONTENT=$(<"${html}")
HTML_CONTENT_UPDATED=${HTML_CONTENT//"<!-- FILL_LAYERS_HERE -->"/${LAYERS_LIST_HTML}}
printf '%s' "${HTML_CONTENT_UPDATED}" > "${html}"
}
generate_comparisons()
{
script_path=$1
repo_root_path=$2
project_path=$3
kicad_pcb=$4
# Workaround for windows line endings.
if [[ -f "${kicad_pcb}.bkp" ]]; then
cp "${kicad_pcb}" "${kicad_pcb}.bkp" 2> /dev/null
fi
dos2unix "${kicad_pcb}"
main_html=index.html
filename=$(basename -- "$kicad_pcb")
board_name="${filename%.*}"
if [[ "${project_path}" != "." ]]; then
output_dir="${repo_root_path}/kidiff/${project_path}"
else
output_dir="${repo_root_path}/kidiff/"
fi
web_dir="${output_dir}/web/"
if [[ "$REMOVE_KDIFF_FOLDER" == "1" ]]; then
echo
echo "Removing ${output_dir} folder ..."
if ! rm -rf "${output_dir}" &> /dev/null; then
echo "Warning: folder was not removed"
fi
fi
if [[ "$project_path" == $(basename "$(pwd)") ]]; then
current_path_fix="./"
else
current_path_fix="${project_path}"
fi
sch_files=$(find_ "${current_path_fix}" -name "*.sch" -printf "%p ")
if [[ "${VERBOSE}" == "1" ]]; then
echo
echo " script_dir: ${script_path}"
echo
echo " repo_root: ${repo_root_path}"
echo " project_dir: ${project_path}"
echo
echo " kicad_pcb: ${kicad_pcb}"
echo " sch_files: ${sch_files}"
echo " board_name: ${board_name}"
echo
echo " output_dir: ${output_dir}"
echo " web_dir: ${web_dir}"
echo
fi
mkdir -p "${web_dir}/"
mkdir -p "${output_dir}/diff/"
git_branch=$(git rev-parse --abbrev-ref HEAD)
if [[ "$ALL_COMMITS" == "1" ]]; then
cmd="git-kdiff-log ${git_branch} -- ${project_path}"
if [[ "${VERBOSE}" == "1" ]]; then
echo "$cmd"
echo
fi
commits=$(eval "${cmd}")
else
cmd="git-kdiff-log ${git_branch} -- ${kicad_pcb} ${sch_files}"
if [[ "${VERBOSE}" == "1" ]]; then
echo "$cmd"
echo
fi
commits=$(eval "${cmd}")
fi
if [[ "$OLDER_COMMIT" != "" ]]; then
if [[ "${VERBOSE}" == "1" ]]; then
echo " Older commit: ${OLDER_COMMIT}"
fi
commits=$(echo "${commits}" | sed_ "/^${OLDER_COMMIT}/q")
fi
if [[ "$NEWER_COMMIT" != "" ]]; then
if [[ "${VERBOSE}" == "1" ]]; then
echo " Newer commit: ${NEWER_COMMIT}"
echo
fi
commits=$(echo "${commits}" | sed_ -n "/^${NEWER_COMMIT}/,\$p")
fi
if [[ "${VERBOSE}" == "1" ]]; then
echo "Commits list:"
echo "$commits"
fi
# Commits list has to have 2 commits at least
if [[ $(echo "$commits" | wc -l | sed_ "s/^[ \t]\+//g") -lt "2" ]]; then
echo "Leaving, less than 2 commits found"
exit 0
fi
a=$(echo "$commits" | tac_ | cut -d' ' -f1);
b=$a
na=$(echo "$a" | wc -w | sed_ "s/^[ \t]\+//g")
nb=$(echo "$b" | wc -w | sed_ "s/^[ \t]\+//g")
nab=$(echo "${na} * (${nb} - 1) " | bc)
echo
echo "Generating artifacts for ${na} commits."
echo "Totaling ${nab} possible comparisons."
echo
n=0
sch_page_names=$(get_pages "${board_name}" "${current_path_fix}/${board_name}.sch" | cut -d"|" -f2)
board_layers=$(sed_ -n '/^ (layers/,/ )$/{/^ (layers/!{/ )$/!p}}' "${kicad_pcb}" | sed_ "s/^[ \t]\+//g" | sed_ "s/[()]//g" | cut -d" " -f2 | tr "." "_")
# Remove windows line endings
sch_page_names=$(echo "$sch_page_names" | sed_ "s/\r$//g")
board_layers=$(echo "$board_layers" | sed_ "s/\r$//g")
if [[ "${VERBOSE}" == "1" ]]; then
echo "Schematic Pages"
echo "${sch_page_names}" | nl
echo
echo "Layers"
echo "${board_layers}" | nl
echo
fi
IFS=$'\n'
for i in $a; do
k=0
for j in $b; do
if [[ "$i" != "$j" ]]; then
k=$((k+1))
n=$((n+1))
# Check if diff file exists
if [[ -d "${output_dir}/$j" ]]; then
continue
fi
if [[ -d "${output_dir}/$j" ]]; then
skipping=", skipping..."
else
skipping=""
fi
if [[ $k == 1 ]]; then
printf "%4d. %s - %s%s\n" "$n" "$i" "$j" "$skipping"
else
printf "%4d. %s - %s%s \n" "$n" " " "$j" "$skipping"
fi
mkdir -p "${output_dir}/$i/"
mkdir -p "${output_dir}/$j/"
# ====
if [[ "$DEBUG" == 0 ]]; then
HASHTAG=""
else
HASHTAG="#"
fi
# ====
if [[ ! "${KIDIFF_MISSING}" == 1 ]]; then
cmd="kidiff ${kicad_pcb} --webserver-disable -a $i -b $j ${HASHTAG} &> /dev/null"
if [[ "$VERBOSE" == "1" ]]; then
echo " $cmd"
fi
if [[ "$DRY_RUN" == "0" ]]; then
eval "$cmd"
ret=$?
if [[ "$VERBOSE" == "1" ]]; then
if [[ "${ret}" -gt "0" ]]; then
echo " Status: ${ret}"
fi
fi
fi
if [ -f "${output_dir}/diff.txt" ]; then
cp -f "${output_dir}/diff.txt" "${output_dir}/diff-pcb-$i-$j.txt"
cp -f "${output_dir}/diff.txt" "${output_dir}/diff-pcb-$j-$i.txt"
fi
if [[ "$ret" == "1" ]]; then
if [[ ! $(du -s "${output_dir}/$i/${board_name}.kicad_pcb" | cut -d$'\t' -f1) = 0 ]]; then
cmd="plotpcb -o \"${output_dir}/$i/\" -q \"${output_dir}/$i/${board_name}.kicad_pcb\" ${HASHTAG} &> /dev/null"
echo " ${cmd}"
eval "$cmd"
fi
if [[ ! $(du -s "${output_dir}/$j/${board_name}.kicad_pcb" | cut -d$'\t' -f1) = 0 ]]; then
cmd="plotpcb -o \"${output_dir}/$j/\" -q \"${output_dir}/$j/${board_name}.kicad_pcb\" ${HASHTAG} &> /dev/null"
echo " ${cmd}"
eval "$cmd"
fi
fi
fi
IFS=$'\n'
for layer in $board_layers; do
# Create blank svg when it is missing
if [[ ! -f "${output_dir}/$i/${board_name}-${layer}.svg" ]]; then
ln -sf "${web_dir}/blank.svg" "${output_dir}/$i/${board_name}-${layer}.svg"
fi
if [[ ! -f "${output_dir}/$j/${board_name}-${layer}.svg" ]]; then
ln -sf "${web_dir}/blank.svg" "${output_dir}/$j/${board_name}-${layer}.svg"
fi
done
# ====
removed_color="FF0000"
added_color="008FFF"
unchanged_color="FFFFFF"
background_color="222222"
if [[ ! "$PLOTGITSCH_MISSING" == 1 ]]; then
if [[ "$current_path_fix" == "." ]] || [[ "$current_path_fix" == "./" ]]; then
plotgitsch_path=""
else
plotgitsch_path="-r ${project_path}"
fi
cmd="plotgitsch ${plotgitsch_path} -m -l ${current_path_fix}/${board_name}-cache.lib -c ${removed_color}:${added_color}:${unchanged_color}:${background_color} ${i} ${j} ${HASHTAG} &> /dev/null"
if [[ "${VERBOSE}" == "1" ]]; then
echo " $cmd"
fi
if [[ "$DRY_RUN" == "0" ]]; then
eval "$cmd"
ret=$?
if [[ "$VERBOSE" == "1" ]]; then
if [[ "${ret}" -gt "0" ]]; then
echo " Status: ${ret}"
echo
fi
fi
fi
if [[ "${VERBOSE}" == "1" ]]; then
echo
fi
fi
IFS=$'\n'
for sch_page_name in $sch_page_names; do
# Create blank svg when it is missing
if [[ ! -f "sch1-${sch_page_name}.svg" ]]; then
ln -sf "${web_dir}/blank.svg" "sch1-${sch_page_name}.svg"
fi
if [[ ! -f "sch2-${sch_page_name}.svg" ]]; then
ln -sf "${web_dir}/blank.svg" "sch2-${sch_page_name}.svg"
fi
done
IFS=$'\n'
for sch_page_svg in *.svg; do
# Background transparent
sed_ -i 's/fill="#FFFFFF"/fill="#FFFFFF00"/g' "$sch_page_svg" &> /dev/null
sed_ -i 's/fill="#222222"/fill="#22222200"/g' "$sch_page_svg" &> /dev/null
# Elements in black
sed_ -i 's/fill="#FF0000"/fill="#000000FF"/g' "$sch_page_svg" &> /dev/null
sed_ -i 's/fill="#00FF00"/fill="#000000FF"/g' "$sch_page_svg" &> /dev/null
sed_ -i 's/stroke="#800000"/stroke="#000000FF"/g' "$sch_page_svg" &> /dev/null
case "$sch_page_svg" in
sch1-*)
new_sch_name=${sch_page_svg//sch1/sch}
mv -f "$sch_page_svg" "${output_dir}/$i/${new_sch_name}" 2> /dev/null
;;
sch2-*)
new_sch_name=${sch_page_svg//sch2/sch}
mv -f "$sch_page_svg" "${output_dir}/$j/${new_sch_name}" 2> /dev/null
;;
esac
done
fi
done
# THIS CAN BE USED TO DISABLE PAGES AND LAYERS THAT DONT HAVE DIFFERENCES
# IFS=$'\n'
# for svg_path in ${output_dir}/${j}/*.svg; do
# svg=$(echo $svg_path | sed "s|${output_dir}/${j}/||g" | sed "s/\.svg//g")
# differs=0
# if diff ${output_dir}/${i}/${svg}.svg $svg_path > ${output_dir}/diff2/$i-$j-$svg.diff; then
# differs=1
# fi
# echo "diff ${output_dir}/${i}/$svg $svg_path $differs" >> ${output_dir}/svgs.diff
# echo "$differs" >> ${output_dir}/diff2/$i-$j-$svg.differs
# done
done
echo " Assembling HTML"
# Copy assets
cp -f "${script_path}/${main_html}" "${web_dir}/"
cp -f "${script_path}/kdiff.js" "${web_dir}/"
cp -f "${script_path}/kdiff.css" "${web_dir}/"
cp -f "${script_path}/blank.svg" "${web_dir}/"
cp -f "${script_path}/favicon.ico" "${web_dir}/"
cp -f "${script_path}/redirect.html" "${web_dir}/../index.html"
# Insert commits list
echo " Insert commits list"
insert_commits "${web_dir}/${main_html}" "${commits}" "${current_path_fix}" "${kicad_pcb}" "${sch_files}"
# Insert pages list
echo " Insert pages list"
pages=$(get_pages "${board_name}" "${current_path_fix}/${board_name}.sch")
insert_pages "${web_dir}/${main_html}" "$pages"
# Update board title
echo " Update board title"
title=$(grep -i "(title .*)" "${kicad_pcb}" 2> /dev/null | sed_ "s/^[ \t]\+//g" | cut -d" " -f 2- | sed_ "s/[)\"]//g")
if [[ $title == "" ]]; then
title="{Title}";
fi
sed_ -i "s|\[PROJECT_TITLE\]|${title}|g" "${web_dir}/${main_html}"
# Update board revision
echo " Update board revision"
revision=$(grep "(rev " "${kicad_pcb}" 2> /dev/null | sed_ "s/^[ \t]\+//g" | sed_ "s/[()]//g" | cut -d" " -f2- | sed_ "s/\"//g")
if [[ $revision == "" ]]; then
revision="{Rev}";
fi
sed_ -i "s|\[PROJECT_REVISION\]|${revision}|g" "${web_dir}/${main_html}"
# Update board date
echo " Update board date"
date=$(grep "(date " "${kicad_pcb}" 2> /dev/null | sed_ "s/^[ \t]\+//g" | sed_ "s/[()]//g" | cut -d" " -f2)
if [[ $date == "" ]]; then
date="{Date}";
fi
sed_ -i "s|\[PROJECT_DATE\]|${date}|g" "${web_dir}/${main_html}"
# Update board name
echo " Update board name"
sed_ -i "s|var board_name =.*|var board_name = \"${board_name}\"; // generated by kdiff script |g" "${web_dir}/kdiff.js"
commit1=$(echo "$commits" | head -n1 | tail -n1 | cut -d" " -f1)
commit2=$(echo "$commits" | head -n2 | tail -n1 | cut -d" " -f1)
# Update initial schematic images
echo " Update initial schematic images"
sed_ -i "s|\[INITIAL_COMMIT_1_SCH\]|../${commit1}/sch-${board_name}.svg|g" "${web_dir}/${main_html}"
sed_ -i "s|\[INITIAL_COMMIT_2_SCH\]|../${commit2}/sch-${board_name}.svg|g" "${web_dir}/${main_html}"
# Update initial layout images
echo " Update initial layout images"
initial_layer=$(echo "$board_layers" | head -1)
sed_ -i "s|\[INITIAL_COMMIT_1_PCB\]|../${commit1}/${board_name}-${initial_layer}.svg|g" "${web_dir}/${main_html}"
sed_ -i "s|\[INITIAL_COMMIT_2_PCB\]|../${commit2}/${board_name}-${initial_layer}.svg|g" "${web_dir}/${main_html}"
# Update initial commit legend
echo " Update initial commit legend"
sed_ -i "s|\[COMMIT_1_HASH\]|${commit1}|g" "${web_dir}/${main_html}"
sed_ -i "s|\[COMMIT_2_HASH\]|${commit2}|g" "${web_dir}/${main_html}"
# Insert layers list
echo " Insert layers list"
board_layers=$(sed_ -n '/^ (layers/,/ )$/{/^ (layers/!{/ )$/!p}}' "${kicad_pcb}" | sed_ "s/^[ \t]\+//g" | sed_ "s/[()]//g" | cut -d" " -f2)
insert_layers "${web_dir}/${main_html}" "$board_layers"
# Restore board
if [[ -f "${kicad_pcb}.bkp" ]]; then
cp "${kicad_pcb}.bkp" "${kicad_pcb}" &> /dev/null
fi
}
# ====================================================================
# ====================================================================
script_path=$(readlink_ "$0")
script_dir=$(dirname "$script_path")
trap ctrl_c INT
export ALL_COMMITS=0
export DEBUG=0
export DRY_RUN=0
export LAUNCH_PAGE=1
export NEWER_COMMIT=""
export OLDER_COMMIT=""
export REMOVE_KDIFF_FOLDER=0
export VERBOSE=0
export WEBSERVER_PORT=8000
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-a)
ALL_COMMITS=1
shift
;;
-d)
VERBOSE=1
DRY_RUN=1
shift
;;
-h)
show_help
exit 1
;;
-l)
LAUNCH_PAGE=0
shift
;;
-n)
NEWER_COMMIT="$2"
shift
shift
;;
-o)
OLDER_COMMIT="$2"
shift
shift
;;
-r)
REMOVE_KDIFF_FOLDER=1
shift
;;
-p)
WEBSERVER_PORT="$2"
shift
shift
;;
-V)
VERBOSE=1
shift
;;
-D)
DEBUG=1
shift
;;
*)
kicad_pro="$1"
shift # past argument
;;
esac
done
kicad_pcb=${kicad_pro//.pro/.kicad_pcb}
# Check required tools
if [[ "${DRY_RUN}" == 0 ]]; then
check_tool opam > /dev/null
eval "$(opam config env)"
PLOTGITSCH_MISSING=$(check_tool plotgitsch)
KIDIFF_MISSING=$(check_tool kidiff)
export PLOTGITSCH_MISSING
export KIDIFF_MISSING
fi
if [[ ! "${PLOTGITSCH_MISSING}" = 0 ]]; then
echo "INFO: plotgitsch is missing, schematic diff won't be generated"
fi
if [[ ! "${KIDIFF_MISSING}" = 0 ]]; then
echo "INFO: kidiff is missing, layout diff won't be generated"
fi
if ! check_tool "dos2unix" &> /dev/null; then "dos2unix is missing"; exit; fi
case $OSTYPE in
darwin*)
if ! check_tool "gsed" &> /dev/null; then "gsed is missing"; exit; fi
if ! check_tool "gfind" &> /dev/null; then "gfind is missing"; exit; fi
;;
esac
repo_root=$(git rev-parse --show-toplevel)
kicad_pcb_path=$(readlink_ "${kicad_pcb}" | sed_ "s|${repo_root}/||g")
project_dir=$(dirname "$kicad_pcb_path" | sed_ "s|${repo_root}/||g")
if [[ -f "${kicad_pro}" ]]; then
generate_comparisons "${script_dir}" "${repo_root}" "${project_dir}" "${kicad_pcb}"
else
echo "The board ${kicad_pcb} does not exit"
exit 1
fi
output_dir="${repo_root}/kidiff/${project_dir}"
web_dir="${output_dir}/web/"
echo
if grep -q "Microsoft" "/proc/version" &> /dev/null; then
# Webserver alternative for Windows with WSL
host=127.0.0.1
echo "Starting webserver at http://${host}:${WEBSERVER_PORT}"
cmd.exe /C start "http://${host}:${WEBSERVER_PORT}" &> /dev/null &
python3 -m http.server -d "${web_dir}/../" -b "$host" "$WEBSERVER_PORT" &> /dev/null
echo "(Hit Ctrl+C to exit)"
else
webserver.py -p "$WEBSERVER_PORT" "${kicad_pcb}"
pid=$?
kill -9 $pid > /dev/null
fi