This repository has been archived by the owner on Nov 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
generator.sh
executable file
·200 lines (163 loc) · 5.5 KB
/
generator.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
#!/bin/bash
case "$(uname | tr '[:upper:]' '[:lower:]')" in
darwin*) sed_regex_option='-E' ;;
*) sed_regex_option='-r' ;;
esac
hash_sum=sha256sum
simple_name='simple-icons'
simple_source_dir="source/$simple_name"
simple_gen_dir="gen/$simple_name"
simple_icons_dir="icons/$simple_name"
mini_name='minimalistic-icons'
mini_source_dir="source/$mini_name"
mini_gen_dir="gen/$mini_name"
mini_icons_dir="icons/$mini_name"
function list_simple_icons() {
files=
for simple_dir in $simple_source_dir $simple_gen_dir
do
files="$files $(ls $simple_dir/* | grep -v '.light.svg')"
done
echo $files | sort -u
}
function get_folder_color() {
grep -Eo '(rect|polygon)[^#]+fill="#[0-9a-f]{6}"' $1 | head -n 1 | grep -Eo '#.{6}'
}
function validate_sums() {
if [[ -f $1 ]] && [[ -f $2 ]]
then
sum="$(tail -1 $2 | grep -Eo '\w+' | head -n 1)"
[[ "$($hash_sum $1 | grep -Eo '\w+' | head -n 1)" = "$sum" ]]
else
false
fi
}
function comment_sum() {
echo "<!-- $($hash_sum $1) -->"
}
npm run compile
mkdir -p {$simple_gen_dir,$simple_icons_dir,$mini_gen_dir,$mini_icons_dir}
for theme_source_dir in $mini_source_dir $simple_source_dir
do
echo "Beautifying icons from $theme_source_dir"
./node_modules/.bin/svgo --config=.svgo.js --multipass -f $theme_source_dir > /dev/null
done
for file in $(ls $simple_gen_dir)
do
if [[ ! -f $simple_source_dir/${file/.expanded/} ]] || [[ -f $simple_source_dir/$file ]]
then
echo "Cleaning up unused file $simple_gen_dir/$file"
rm -f $simple_gen_dir/$file
fi
done
for file in $(ls $simple_icons_dir)
do
if [[ ! -f $simple_source_dir/$file ]] && [[ ! -f $simple_gen_dir/$file ]]
then
echo "Cleaning up unused file $simple_icons_dir/$file"
rm -f $simple_icons_dir/$file
fi
done
for folder in $(ls $simple_source_dir/folder-*.svg | grep -v '.expanded.svg')
do
expanded_folder=${folder/.svg/.expanded.svg}
gen_folder=$simple_gen_dir/$(basename $expanded_folder)
if ! validate_sums $folder $gen_folder && [[ ! -f $expanded_folder ]]
then
echo "Generating simple $(basename ${expanded_folder/.svg/})"
old_color=$(get_folder_color $simple_source_dir/folder.expanded.svg)
new_color=$(get_folder_color $folder)
cp $simple_source_dir/folder.expanded.svg $gen_folder
sed $sed_regex_option -i '' "s/$old_color/$new_color/g" $gen_folder
comment_sum $folder >> $gen_folder
fi
done
icon_sums=
for file in $(list_simple_icons)
do
sum=$(cat $file | sed $sed_regex_option 's/"(#[0-9a-f]{6}|none)"//g' | sed 's/<!\-\-.*\-\->//g' | tr -d '[:space:]' | $hash_sum | grep -Eo '\w+' | head -n 1)
icon_sums="$icon_sums $sum@$(basename $file)"
done
icon_redirects=$(echo $icon_sums | node generator.js redirect)
for mini_dir in $mini_gen_dir $mini_icons_dir
do
for file in $(ls $mini_dir)
do
file=${file/.light/}
if [[ -n $(echo $icon_redirects | grep -E "(^| )${file/.svg/}(.light)?.svg@") ]] \
|| ( [[ ! -f $simple_source_dir/$file ]] && [[ ! -f $simple_gen_dir/$file ]] )
then
file=${file/.svg/}
for useless in $file{,.light}.svg
do
if [[ -f $mini_dir/$useless ]]
then
echo "Cleaning up unused file $mini_dir/$useless"
rm -f $mini_dir/$useless
fi
done
fi
done
done
for file in $(list_simple_icons)
do
file=$(basename $file)
if echo $icon_redirects | grep -E "(^| )$file@" &> /dev/null
then
continue
fi
simple_dir=$simple_source_dir
if [[ -f $simple_gen_dir/$file ]]
then
simple_dir=$simple_gen_dir
fi
if [[ -f $mini_source_dir/$file ]]
then
file_dir=$mini_source_dir
rm -f $mini_gen_dir/$file
else
file_dir=$mini_gen_dir
if ! validate_sums $simple_dir/$file $mini_gen_dir/$file
then
echo "Generating minimalistic $file"
node generator.js gen < $simple_dir/$file > $mini_gen_dir/$file
comment_sum $simple_dir/$file >> $mini_gen_dir/$file
fi
fi
light_file=${file/.svg/.light.svg}
if ! validate_sums $file_dir/$file $mini_gen_dir/$light_file
then
echo "Generating minimalistic $light_file"
node generator.js light < $file_dir/$file > $mini_gen_dir/$light_file
comment_sum $file_dir/$file >> $mini_gen_dir/$light_file
fi
done
for theme_name in $mini_name $simple_name
do
theme_source_dir=source/$theme_name
theme_gen_dir=gen/$theme_name
theme_icon_dir=icons/$theme_name
icon_list="$(ls $theme_source_dir) $(ls $theme_gen_dir)"
svgo_cmd="./node_modules/.bin/svgo --multipass -o $theme_icon_dir"
if [[ $theme_name = $mini_name ]]
then
redirects=$icon_redirects
else
redirects=
fi
mkdir -p $theme_icon_dir
echo "Writing $theme_name.json"
node generator.js json $theme_icon_dir $redirects $(echo $icon_list | sort -u) < icons.json > $theme_name.json
echo "Optimizing icons from $theme_source_dir"
$svgo_cmd -f $theme_source_dir > /dev/null
echo "Optimizing icons from $theme_gen_dir"
$svgo_cmd -f $theme_gen_dir > /dev/null
done
for file in $(ls $simple_source_dir/* | LC_ALL=C sort)
do
markdown_list="$markdown_list$(basename $file)|<img width=\"16\" height=\"16\" src=\"$file\">@"
done
echo "Icon file name|Icon" > ICONS.md
echo "---|---" >> ICONS.md
echo -n $markdown_list | tr @ '\n' >> ICONS.md
echo 'Done'