-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtbr
More file actions
297 lines (234 loc) · 5.7 KB
/
Copy pathtbr
File metadata and controls
297 lines (234 loc) · 5.7 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
#!/bin/bash
_tbr_source_file_fast() {
local file="$1"
source "$file"
}
_tbr_is_sourceable_file() {
local file="$1"
local filename="${file##*/}"
[ -f "$file" ] || return 1
case "$file" in
"$TBDIR/init/"*) return 1 ;;
esac
[[ "$filename" == *.* ]] && return 1
[[ "$filename" == .* ]] && return 1
[[ "$file" == "$TBDIR/tbr" ]] && return 1
_tbr_is_live_source_file "$file" && return 1
return 0
}
_tbr_is_live_source_file() {
local file="$1"
case "$file" in
"$TBDIR/xgit") return 0 ;;
esac
return 1
}
_tbr_iter_core_files() {
local file
[[ -d "$TBDIR/core" ]] || return 0
while IFS= read -r -d '' file; do
if _tbr_is_sourceable_file "$file"; then
printf '%s\0' "$file"
fi
done < <(find "$TBDIR/core" -type f -print0 | sort -z)
}
_tbr_iter_root_files() {
local file
while IFS= read -r -d '' file; do
if _tbr_is_sourceable_file "$file"; then
printf '%s\0' "$file"
fi
done < <(
find "$TBDIR" \
\( -type d \( -name "bin" -o -name "core" -o -name ".*" \) -prune \) -o \
\( -type f -print0 \) | sort -z
)
}
_tbr_source_files() {
local file
while IFS= read -r -d '' file; do
_tbr_source_file_fast "$file"
done < <(_tbr_iter_core_files)
while IFS= read -r -d '' file; do
_tbr_source_file_fast "$file"
done < <(_tbr_iter_root_files)
}
_tbr_source_live_files() {
local file
for file in "$TBDIR/xgit"; do
if [ -f "$file" ]; then
_tbr_source_file_fast "$file"
fi
done
}
_tbr_cache_ttl() {
echo "${TBR_CACHE_TTL:-604800}"
}
_tbr_cache_path() {
local path
if [ -n "$TBR_CACHE_PATH" ]; then
path="$TBR_CACHE_PATH"
elif [ -n "$XDG_CACHE_HOME" ]; then
path="$XDG_CACHE_HOME/tbr.cache"
else
path="$TBDIR/.tbr.cache"
fi
if [[ "$path" != *.cache ]]; then
path="${path}.cache"
fi
echo "$path"
}
_tbr_ensure_cache_dir() {
local cache_path="$1"
local cache_dir
cache_dir="${cache_path%/*}"
if [ -n "$cache_dir" ]; then
mkdir -p "$cache_dir"
fi
}
_tbr_mtime() {
stat -c %Y "$1" 2>/dev/null || echo 0
}
_tbr_cache_signature() {
local max=0
local file
local ts
while IFS= read -r -d '' file; do
ts=$(_tbr_mtime "$file")
if [ "$ts" -gt "$max" ]; then
max="$ts"
fi
done < <(_tbr_iter_core_files)
while IFS= read -r -d '' file; do
ts=$(_tbr_mtime "$file")
if [ "$ts" -gt "$max" ]; then
max="$ts"
fi
done < <(_tbr_iter_root_files)
echo "$max"
}
_tbr_git_head() {
command -v git >/dev/null 2>&1 || return 0
git -C "$TBDIR" rev-parse HEAD 2>/dev/null
}
_tbr_read_cache_header() {
local cache_path="$1"
local line
TBR_CACHE_HEADER_TIMESTAMP=""
TBR_CACHE_HEADER_SIGNATURE=""
TBR_CACHE_HEADER_TTL=""
TBR_CACHE_HEADER_GIT_HEAD=""
while IFS= read -r line; do
[ -z "$line" ] && break
case "$line" in
timestamp=*) TBR_CACHE_HEADER_TIMESTAMP="${line#timestamp=}" ;;
signature=*) TBR_CACHE_HEADER_SIGNATURE="${line#signature=}" ;;
ttl=*) TBR_CACHE_HEADER_TTL="${line#ttl=}" ;;
git_head=*) TBR_CACHE_HEADER_GIT_HEAD="${line#git_head=}" ;;
esac
done < "$cache_path"
}
_tbr_cache_valid() {
local cache_path="$1"
local ttl="$2"
local signature="$3"
local git_head="$4"
local now
[ -f "$cache_path" ] || return 1
_tbr_read_cache_header "$cache_path"
[ -n "$TBR_CACHE_HEADER_TIMESTAMP" ] || return 1
if [ "$ttl" -gt 0 ] 2>/dev/null; then
now=$(date +%s)
if [ $(( now - TBR_CACHE_HEADER_TIMESTAMP )) -gt "$ttl" ]; then
return 1
fi
fi
[ "$TBR_CACHE_HEADER_SIGNATURE" = "$signature" ] || return 1
if [ -n "$git_head" ]; then
[ "$TBR_CACHE_HEADER_GIT_HEAD" = "$git_head" ] || return 1
fi
return 0
}
_tbr_append_file_to_cache() {
local cache_path="$1"
local file="$2"
cat "$file" >> "$cache_path"
printf '\n' >> "$cache_path"
}
_tbr_write_cache() {
local cache_path="$1"
local ttl="$2"
local signature="$3"
local git_head="$4"
local file
local tmp
_tbr_ensure_cache_dir "$cache_path"
tmp="$(mktemp "${cache_path}.XXXXXX")" || return 1
{
echo "timestamp=$(date +%s)"
echo "signature=$signature"
echo "ttl=$ttl"
if [ -n "$git_head" ]; then
echo "git_head=$git_head"
fi
echo ""
} > "$tmp"
while IFS= read -r -d '' file; do
_tbr_append_file_to_cache "$tmp" "$file"
done < <(_tbr_iter_core_files)
while IFS= read -r -d '' file; do
_tbr_append_file_to_cache "$tmp" "$file"
done < <(_tbr_iter_root_files)
mv "$tmp" "$cache_path"
}
_tbr_refresh_cache() {
local force_refresh="$1"
local cache_path
local ttl
local signature
local git_head=""
cache_path="$(_tbr_cache_path)"
ttl="$(_tbr_cache_ttl)"
signature="$(_tbr_cache_signature)"
if [ "${TBR_CACHE_GIT_CHECK:-0}" = "1" ]; then
git_head="$(_tbr_git_head)"
fi
if [ "$force_refresh" != "1" ] && _tbr_cache_valid "$cache_path" "$ttl" "$signature" "$git_head"; then
return 0
fi
_tbr_write_cache "$cache_path" "$ttl" "$signature" "$git_head"
}
_tbr_should_force_rebuild() {
local arg
for arg in "$@"; do
case "$arg" in
-f|--force) return 0 ;;
esac
done
return 1
}
tbr() {
local cache_path
local force_refresh="0"
cache_path="$(_tbr_cache_path)"
if _tbr_should_force_rebuild "$@"; then
force_refresh="1"
fi
if [ "$force_refresh" = "1" ]; then
_tbr_refresh_cache "1"
fi
if [ -f "$cache_path" ]; then
_tbr_source_file_fast "$cache_path"
else
_tbr_refresh_cache "1" >/dev/null 2>&1 || true
if [ -f "$cache_path" ]; then
_tbr_source_file_fast "$cache_path"
else
_tbr_source_files
fi
fi
_tbr_source_live_files
if [[ $- == *i* ]] && [[ "${TOOLBOX_SKIP_BACKGROUND_INIT:-0}" != "1" ]] && typeset -f background_init >/dev/null 2>&1; then
background_init
fi
}