-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_sourcer.sh
74 lines (66 loc) · 2.15 KB
/
auto_sourcer.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
#!/usr/bin/env bash
# TODO: tune; currently, check every prompt while debugging
export AUTO_SOURCER_CHECK_INTERVAL="10"
export AUTO_SOURCING_FILE_CHANGED=""
export AUTO_SOURCED_FILES=""
# NB: Auto-Sourcing this file will clobber auto-sourcing data this prevents the
# file from being sourced more than once in a given shell
if [ -z "$AUTO_SOURCED_FILES" ] ; then
# NB: these cannot be an `export` because we don't want it propagating to
# child shells
AUTO_SOURCER_LAST_CHECK=""
AUTO_SOURCED_FILES=" "
auto_source() {
# TODO make sure to re-source only if it is changed
if [ -z "$1" ]; then
echo "ERROR: specify a file to source."
else
# use appropriate `source` command
if type source >/dev/null 2>&1; then
source "$1"
else
. "$1"
fi
# only record if not already present
if [[ "$AUTO_SOURCED_FILES" != *" $1 "* ]]; then
AUTO_SOURCED_FILES+="$1 "
fi
fi
}
last_mtime() {
if [ -z "$1" ]; then
echo "ERROR: specify a file to source."
else
# NB: first try `gstat` which is installed by `brew` for `coreutils` to
# not clobber existing `macOS` utilities
if type gstat >/dev/null 2>&1; then
gstat -c %Y "$1"
else
stat -c %Y "$1"
fi
fi
}
auto_source_check_and_reload_sources() {
NOW_UTC="$(date +%s)"
NEXT_CHECK_TIME="$(echo "$AUTO_SOURCER_LAST_CHECK + $AUTO_SOURCER_CHECK_INTERVAL" | bc)"
# only check after interval
if [ "$NOW_UTC" -gt "$NEXT_CHECK_TIME" ]; then
AUTO_SOURCING_FILE_CHANGED="YES"
for SOURCED_FILE in `echo $AUTO_SOURCED_FILES`; do
if [[ "$SOURCED_FILE" == "$HOME"* ]]; then
DISPLAY_PATH="~${SOURCED_FILE#"$HOME"}"
fi
if [ -e "$SOURCED_FILE" ] && [ "$(last_mtime "$SOURCED_FILE")" -gt "$AUTO_SOURCER_LAST_CHECK" ]; then
# disrupts output from other commands
# echo "\`$DISPLAY_PATH\` changed, resourcing..."
auto_source "$SOURCED_FILE"
fi
done
AUTO_SOURCING_FILE_CHANGED=""
AUTO_SOURCER_LAST_CHECK="$NOW_UTC"
fi
}
auto_source_initialize() {
AUTO_SOURCER_LAST_CHECK="$(date +%s)"
}
fi