-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpre-commit
executable file
·239 lines (189 loc) · 9.95 KB
/
pre-commit
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
#!/bin/bash
##################################
## START OF USER ADJUSTABLE VALUES
##################################
#Assuming stuff is in the path.
#Probably shouldn't do this.
EAGLE=eagle
GERBV=gerbv
#Determine whether we should generate our own visual diffs
#Otherwise, it will be up to other tools to compare images between revisions
GEN_VISUAL_DIFFS=true
#Ensure we have the tool for our visual diff
IMAGEMAGICK_COMPOSITE=composite
if ! $IMAGEMAGICK_COMPOSITE -version;then
echo "ImageMagick not found, disabling visual diffs"
GEN_VISUAL_DIFFS=false;
fi
#Specify the sub-folders to put images in
IMAGE_FOLDER=gerber-previews
GERBER_FOLDER=gerber-files
#Set colors, must fit gerber "#RRGGBB" or "#RRGGBBAA" format, where AA is the alpha transparency
OUTLINECOLOR="#FFFFFF"
DRILLCOLOR="#000000" #This will not be honored on the individual drill layer, as I'm rendering that as white. Otherwise, it's likely black on black, which is less than useful
SILKSCREENCOLOR="#FFFFFF88"
MASKCOLOR="#e5c100A0"
COPPERCOLOR="#7f6b00"
#Indicate the desired DPI. 500 produces very detailed images at roughly 4x maginfication
#For really large boards, you might want to tone it down to 300-400
DPI=500
#You can set the maximum image size here, and the resulting previews will scale down appropriately
#To disable, just set to arbitrarily large values
#MAXIMAGEWIDTH=100000
#MAXIMAGEHEIGHT=100000
MAXIMAGEWIDTH=1500
MAXIMAGEHEIGHT=1500
#Optionally include outlines on the individual layer renderings. Should be true or false.
INCLUDE_OUTLINES=false
###############################
##END OF USER ADJUSTABLE VALUES
###############################
#Verify that the folders are there, or try to make them if they're just missing
if [ ! -e $IMAGE_FOLDER ];then mkdir $IMAGE_FOLDER || {echo "Could not find images folder" ; exit} ; fi
if [ ! -e $GERBER_FOLDER ];then mkdir $GERBER_FOLDER || {echo "Could not find gerber folder" ; exit} ; fi
#Check with Git for a list of added or modified boards, then strip off git data from the filename
#FIXME Does not handle filenames with spaces very well.
for BOARD in $(git status -s|grep "^[AM].*.brd" |sed 's/^[AM] *//');do
echo Processing $BOARD
BOARD=$(basename $BOARD)
NAME=$(basename $BOARD .brd)
#For generating visual diffs, we need both new and old versions to exist, so generate a folder to put them in
OUTPUTDIR=$(mktemp -d)
echo Operating on $BOARD using name $NAME
{ #Shove into a CAM processor in Eagle
#page 305 of the manual has other commands that may be useful
#Searching for "terminal" gives better results than "command line"
#-X cam processor
#-N Suppress erros
#-d device
#-o output filename
echo "Generating board outline"
$EAGLE -X -N -d GERBER_RS274X -o "$GERBER_FOLDER/$NAME.boardoutline.ger" $BOARD Dimension Milling 2>/dev/null
echo "Generating Drill file"
$EAGLE -X -N -d EXCELLON -o "$GERBER_FOLDER/$NAME.drills.xln" $BOARD Drills Holes 2>/dev/null
echo "Generating top layers"
$EAGLE -X -N -d GERBER_RS274X -o "$GERBER_FOLDER/$NAME.toplayer.ger" $BOARD Top Pads Vias 2>/dev/null
$EAGLE -X -N -d GERBER_RS274X -o "$GERBER_FOLDER/$NAME.topstopmask.ger" $BOARD tStop 2>/dev/null
$EAGLE -X -N -d GERBER_RS274X -o "$GERBER_FOLDER/$NAME.topsilkscreen.ger" $BOARD tPlace tNames 2>/dev/null
echo "Generating bottom layers"
$EAGLE -X -N -d GERBER_RS274X -o "$GERBER_FOLDER/$NAME.bottomlayer.ger" $BOARD Bottom Pads Vias 2>/dev/null
$EAGLE -X -N -d GERBER_RS274X -o "$GERBER_FOLDER/$NAME.bottomstopmask.ger" $BOARD bStop 2>/dev/null
$EAGLE -X -N -d GERBER_RS274X -o "$GERBER_FOLDER/$NAME.bottomsilkscreen.ger" $BOARD bPlace bName 2>/dev/null
}
#Assuming no errors, because hell yeeah bitches.
#Probably should detect those and return 1, which will stop the commit until things get fixed.
{ # Generate board extents
#These all work on the same horribly inefficient principal, because I'm lazy. I'm 100% sure if I did a line-by-line comparison, it'd be orders of magnitude more effient.
# Take the processed gerber output,
#Search for the draw command
#grab the appropriate coordinate type
#Clean up the non-numerical stuff
#sort the data
#Take the top or bottom line
#remove leading zeroes, which will make bash maths angry
#We need to process the board outline, so it's in a known and reliable state
TEMPOUTLINE=$(tempfile -s .boardoutline.ger)
gerbv -x rs274x -o $TEMPOUTLINE "$GERBER_FOLDER/$NAME.boardoutline.ger"
#Get min X
MINX=$(cat $TEMPOUTLINE |grep -e '^G' |grep -Po "X[0-9]+" |sed 's/X//g' |sort -n |head -n1 )
#Get max X
MAXX=$(cat $TEMPOUTLINE |grep -e '^G' |grep -Po "X[0-9]+" |sed 's/X//g' |sort -n |tail -n1)
#get min Y
MINY=$(cat $TEMPOUTLINE |grep -e '^G' |grep -Po "Y[0-9]+" |sed 's/Y//g' |sort -n |head -n1 )
#get max Y
MAXY=$(cat $TEMPOUTLINE |grep -e '^G' |grep -Po "Y[0-9]+" |sed 's/Y//g' |sort -n |tail -n1)
echo "Bounds (mil)= ($MINX,$MINY) to ($MAXX,$MAXY)"
#clean up, we don't need it past here
rm $TEMPOUTLINE
}
{ #Do some maths
#GerbV makes everything 3:4 precision, so we need to break that up into floating point
fMINX=$(echo $MINX|sed -e 's/\(...\)\(....\)/\1.\2/')
fMAXX=$(echo $MAXX|sed -e 's/\(...\)\(....\)/\1.\2/')
fMINY=$(echo $MINY|sed -e 's/\(...\)\(....\)/\1.\2/')
fMAXY=$(echo $MAXY|sed -e 's/\(...\)\(....\)/\1.\2/')
echo "Bounds (in)= ($fMINX,$fMINY) to ($fMAXX,$fMAXY)"
#Generate our DPI and window values
BOARDWIDTH=$(echo $fMAXX-$fMINX |bc -l )
BOARDHEIGHT=$(echo $fMAXY-$fMINY |bc -l)
echo "Board size (in) = $BOARDWIDTH x $BOARDHEIGHT"
#DPI=500 #In user adjustable settings up top
#If we want to cap the image sizes, we'll need to lower the DPI a bit. Let's try for that
#Since this is in pixels, we're dropping the floating point
IMAGEWIDTH=$(echo "$BOARDWIDTH*$DPI" |bc |sed 's/\..*//')
if [ $IMAGEWIDTH -gt $MAXIMAGEWIDTH ] ;then
echo -n "lowering DPI from $DPI to "
DPI=$(echo $DPI*$MAXIMAGEWIDTH/$IMAGEWIDTH |bc )
echo "$DPI"
fi
#Since this calculates with the "new" DPI if we have it, we can assert that the image will be below both maximums
IMAGEHEIGHT=$(echo "$BOARDHEIGHT*$DPI" |bc |sed 's/\..*//' )
if [ $IMAGEHEIGHT -gt $MAXIMAGEHEIGHT ] ;then
echo -n "lowering DPI from $DPI to "
DPI=$(echo $DPI*$MAXIMAGEHEIGHT/$IMAGEHEIGHT |bc)
echo "$DPI"
fi
#Now, provide GerbV with all the data it needs to provide the exact image frame that we want
# In order to crop the image (which we want to do), we need to provide GerbV with both a DPI and the window size
IMAGE_SIZE="--origin=${fMINX}x${fMINY} --dpi=$DPI --window_inch=${BOARDWIDTH}x${BOARDHEIGHT} --antialias"
}
{ echo "Generating outline and drill images"
OUTLINE="-f "$OUTLINECOLOR" $GERBER_FOLDER/$NAME.boardoutline.ger"
#Process requests to include outlines on the individual layers
if [ "$INCLUDE_OUTLINES" = "true" ] ;then
INCLUDE_OUTLINES="$OUTLINE"
else
INCLUDE_OUTLINES=""
fi
$GERBV $IMAGE_SIZE --export=png --output=$OUTPUTDIR/$NAME.boardoutline.png -f "$OUTLINECOLOR" $GERBER_FOLDER/$NAME.boardoutline.ger
$GERBV $IMAGE_SIZE --export=png --output=$OUTPUTDIR/$NAME.drills.png $INCLUDE_OUTLINES -f "#FFFFFF" $GERBER_FOLDER/$NAME.drills.xln
echo Generating top images
$GERBV $IMAGE_SIZE --export=png --output=$OUTPUTDIR/$NAME.toplayer.png $INCLUDE_OUTLINES -f "$COPPERCOLOR" $GERBER_FOLDER/$NAME.toplayer.ger
$GERBV $IMAGE_SIZE --export=png --output=$OUTPUTDIR/$NAME.topstopmask.png $INCLUDE_OUTLINES -f "$MASKCOLOR" $GERBER_FOLDER/$NAME.topstopmask.ger
$GERBV $IMAGE_SIZE --export=png --output=$OUTPUTDIR/$NAME.topsilkscreen.png $INCLUDE_OUTLINES -f "$SILKSCREENCOLOR" $GERBER_FOLDER/$NAME.topsilkscreen.ger
echo Generating bottom images
$GERBV $IMAGE_SIZE --export=png --output=$OUTPUTDIR/$NAME.bottomlayer.png $INCLUDE_OUTLINES -f "$COPPERCOLOR" $GERBER_FOLDER/$NAME.bottomlayer.ger
$GERBV $IMAGE_SIZE --export=png --output=$OUTPUTDIR/$NAME.bottomstopmask.png $INCLUDE_OUTLINES -f "$MASKCOLOR" $GERBER_FOLDER/$NAME.bottomstopmask.ger
$GERBV $IMAGE_SIZE --export=png --output=$OUTPUTDIR/$NAME.bottomsilkscreen.png $INCLUDE_OUTLINES -f "$SILKSCREENCOLOR" $GERBER_FOLDER/$NAME.bottomsilkscreen.ger
echo Generating combined top images
$GERBV $IMAGE_SIZE --export=png --output=$OUTPUTDIR/$NAME.board-top.png \
$OUTLINE \
-f "$DRILLCOLOR" $GERBER_FOLDER/$NAME.drills.xln \
-f "$SILKSCREENCOLOR" $GERBER_FOLDER/$NAME.topsilkscreen.ger \
-f "$MASKCOLOR" $GERBER_FOLDER/$NAME.topstopmask.ger \
-f "$COPPERCOLOR" $GERBER_FOLDER/$NAME.toplayer.ger
echo Generating combined bottom images
$GERBV $IMAGE_SIZE --export=png --output=$OUTPUTDIR/$NAME.board-bottom.png \
$OUTLINE \
-f "$DRILLCOLOR" $GERBER_FOLDER/$NAME.drills.xln \
-f "$SILKSCREENCOLOR" $GERBER_FOLDER/$NAME.bottomsilkscreen.ger \
-f "$MASKCOLOR" $GERBER_FOLDER/$NAME.bottomstopmask.ger \
-f "$COPPERCOLOR" $GERBER_FOLDER/$NAME.bottomlayer.ger
}
{ #Generate visual diffs to the current image folders
#Check to see if we have existing images
#If not, we don't want to try doing any visual diffs
if [ "$GEN_VISUAL_DIFFS"=="true" ]; then
for image in $(ls $OUTPUTDIR);do
if [ -e "$IMAGE_FOLDER/$image" ] && [ -e "$OUTPUTDIR/$image" ] ;then
echo "Generating diff for $image"
#use ImageMagick to generate a visual diff. Old objects will be red, new placements will be blue.
#Thanks to EMSL for finding this!
$IMAGEMAGICK_COMPOSITE -stereo 0 $OUTPUTDIR/$image $IMAGE_FOLDER/$image $OUTPUTDIR/$image.diff
fi
done
fi
#Save a lot of time and effort on making proper names with ugly hacks like this
rename 's/\.png\.diff$/.diff.png/' $OUTPUTDIR/*
}
{ #move the generated images to the output folder
#This will over write our current set of images, which is fine since they're saved in Git
#TODO might possibly need an -f to overwrite in batch mode.
mv $OUTPUTDIR/*.png $IMAGE_FOLDER/
rmdir $OUTPUTDIR
}
done
#Add the new images, which the post-commit will amend to this one. During pre-commit, you're not allowed to add additional files
#git add $IMAGE_FOLDER
#Pass post commit some data. It will add these files in during the post-commit
echo $IMAGE_FOLDER >.git_amend_data