-
Notifications
You must be signed in to change notification settings - Fork 0
/
builddots
executable file
·186 lines (158 loc) · 3.94 KB
/
builddots
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
#!/bin/bash
root_dir=/home/tony/git/dotfiles
cache_file="$root_dir/.dotbuild_cache"
refresh_cache=false
is_default_verbosity=true
default_verbosity=1
verbosity=0
rebuild_gtk=false
restow=false
now=$(date +%s)
shvar=$(mktemp /tmp/builddotsXXX.tmp)
export cache_file
export verbosity
export now
export shvar
export root_dir
. /home/tony/.scripts/lib/logv
progname=${0##*/}
help_text() {
while IFS= read -r line; do
printf "%s\n" "$line"
done <<-EOF
Usage:
${progname} [OPTIONS...]
Options:
-r, --refresh Force refresh cache
-c, --cache=CACHE Custom cache path
-v, --verbosity=N Verbosity level. Defaults to 1. 0 is silent.
--gtk Also rebuild gtk theme
-s, --restow Restow after build
-h, --help Prints this help message
EOF
}
init_shvar() {
printf "%s\n%s\n%s" 0 0 0 > "${shvar}"
}
get_shvar_line() {
case "$1" in
num_built) line=1 ;;
num_skipped) line=2 ;;
num_new) line=3 ;;
*) exit 1 ;;
esac
echo "$line"
}
export -f get_shvar_line
get_shvar_val() {
line=$(get_shvar_line "$1")
curr=$(sed -n "${line}p" "${shvar}")
echo "$curr"
}
export -f get_shvar_val
increment_shvar() {
line=$(get_shvar_line "$1")
curr=$(get_shvar_val "$1")
((curr++))
sed -i "${line}s/.*/${curr}/" "${shvar}"
}
export -f increment_shvar
close_shvar() {
logv debug "Removing $shvar"
rm "$shvar"
}
build() {
directory=$(dirname "$1")
filename=$(basename "$1")
file_ext=${filename##*.}
[ "$file_ext" != 'dot' ] && exit # ignore non .dot files
search=$(grep "$1" $cache_file)
if [ "$?" -eq 1 ]; then
echo "$1 $now" >> $cache_file
logv debug "$1 added to cache"
increment_shvar num_new
else
last_built=$(echo "$search" | awk '{print $2}')
last_modified=$(stat -c "%Y" "$1")
if [ $((last_modified-last_built)) -lt 0 ]; then
logv debug "Skipping $1"
increment_shvar num_skipped
exit
else
logv debug "Updating $1"
sed -i "s@$search@$1 $now@" $cache_file
fi
fi
built="$directory/${filename::-4}"
dotpp -s "${root_dir}/lookup.json" -t "$1" -o "$built"
logv info "built $1 -> $built"
increment_shvar num_built
# Warn if there's still something unmatched
if grep '$$.*$$' "$built"; then
logv warn "$built has unmatched expressions."
fi
}
export -f build
[ ! -f $cache_file ] && touch $cache_file
eval set -- $(
getopt \
-an "$progname" \
-o 'hrc:vs' \
--long 'help,refresh,cache:,verbosity:,gtk,restow' \
-- "$@"
)
while :;do
case $1 in
-r | --refresh)
refresh_cache=true
shift
;;
-c | --cache)
cache_file=$2
shift 2
;;
--verbosity)
verbosity=$2
is_default_verbosity=false
shift 2
;;
-v)
verbosity=$((verbosity+1))
is_default_verbosity=false
shift
;;
--gtk)
rebuild_gtk=true
shift
;;
-s | --restow)
restow=true
shift
;;
--)
shift
break
;;
*)
help_text
exit 1
;;
esac
done
init_shvar
[ "$refresh_cache" = true ] &&\
echo "" > "$cache_file"
[ "$is_default_verbosity" = true ] && verbosity="$default_verbosity"
cd $root_dir || exit 1
find . -type f -iname '*.dot' -exec bash -c 'build "{}"' \;
if [ "$rebuild_gtk" = true ]; then
cd "$root_dir/glass-theme" || exit 1
meson install -C build
fi
logv info "built: $(get_shvar_val num_built), skipped: $(get_shvar_val num_skipped), new: $(get_shvar_val num_new)"
close_shvar
if [ "$restow" = true ]; then
ship sail >/dev/null 2>&1
success=$?
[ "$success" -eq 0 ] && logv info "Restowed successfully"
fi