-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdateImages.sh
executable file
·101 lines (88 loc) · 2.66 KB
/
updateImages.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
#!/bin/bash
while getopts a option
do
case "${option}"
in
a) updateAllImages=1;;
esac
done
if ! command -v compare &> /dev/null
then
echo "compare (ImageMagick) could not be found!"
exit
fi
mkdir images/wIcons/{android,iphone,hardware,bearer,desktop}/{small,tiny}/ 2> /dev/null
truncate /tmp/revert.txt --size=0
tmpDir=/tmp/resizing/
export tmpDir
rm -rf $tmpDir 2> /dev/null
mkdir $tmpDir 2> /dev/null
git --work-tree=$tmpDir checkout HEAD -- images/wIcons/
logIfUnchanged() {
changed=$1
original=$tmpDir$changed
if [[ ! $changed =~ ^.*\.(jpg|png)$ ]]; then
# if file is not a jpg or png, it is deleted.
echo "Deleting unexpected $changed"
rm $changed
elif [ -f "$original" ] && $( compare -metric AE -fuzz 15% $original $changed NULL: >/dev/null ); then
echo $changed >> /tmp/revert.txt
else
echo "$changed changed"
fi
}
export -f logIfUnchanged
revertImagesThatDidNotChange() {
files=$( git status --porcelain -- images/wIcons/ | sed 's/^...//g' | tr '\n' ' ' )
if [[ $files ]]; then parallel logIfUnchanged {} ::: $files; fi
revertFiles=$( cat /tmp/revert.txt | paste -sd ' ' )
if [[ $revertFiles ]]; then
git checkout HEAD $revertFiles
fi
}
if [ ! "$updateAllImages" ]; then
revertImagesThatDidNotChange
fi
resizeDeterministically() {
filename=$1
source=$2/$filename
target=$3/$filename
size=$4
convert -background none $source -resize ${size}x $target 2> /dev/null
}
resizeMany() {
source=$1
target=$2
size=$3
if [ "$updateAllImages" ]; then
files=$( ls $source/*.* )
else
files=$( git status --porcelain -- $source/*.* | sed 's/^...//g' | tr '\n' ' ' )
fi
if [[ $files ]]; then parallel resizeDeterministically {/} $source $target $size ::: $files; fi
}
export -f resizeDeterministically
for platform in android iphone hardware bearer desktop; do
resizeMany images/wIcons/$platform images/wIcons/$platform/small 100
resizeMany images/wIcons/$platform images/wIcons/$platform/tiny 25
done
wait
if [ "$updateAllImages" ]; then
revertImagesThatDidNotChange
fi
# delete images that are not referenced anymore
tmpFolder=/tmp/migrateImages
rm -rf $tmpFolder 2> /dev/null
mkdir --parents $tmpFolder/
mv images/wIcons $tmpFolder
mkdir --parents images/wIcons/{android,iphone,hardware,bearer,desktop}/{small,tiny}/ 2> /dev/null
folder='bearer'
for folder in bearer android iphone hardware desktop; do
icons=$(grep "^icon: \(.*\)" _$folder/* --only-matching --no-filename | sed 's/^icon: //g')
for i in $icons; do
mv $tmpFolder/wIcons/${folder}/$i images/wIcons/${folder}/$i
for s in small tiny; do
mv $tmpFolder/wIcons/${folder}/${s}/$i images/wIcons/${folder}/${s}/$i
done
done
done