-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwatchdog.sh
executable file
·75 lines (65 loc) · 1.29 KB
/
watchdog.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
#!/bin/zsh
SELF=${0:A}
CURRENT_DIR=${0:A:h}
declare -ga triggers
sort-triggers() {
echo $(printf '%s\n' "$@" | sort -u)
}
watch-file-changes() {
fswatch \
--event Created \
--event MovedFrom \
--event MovedTo \
--event Removed \
--event Renamed \
--event Updated \
--exclude $CURRENT_DIR/\.git \
--exclude $CURRENT_DIR/\.tox \
--exclude \.isorted \
--exclude __pycache__ \
--exclude egg-info \
--exclude tests/reports \
--recursive \
$CURRENT_DIR
}
on-file-change() {
while true; do
read file
filter-change $file
while read -t 2 file; do
filter-change $file
done
run-triggers
done
}
filter-change() {
case $1 in
$SELF)
triggers+=(0-update-self)
;;
$CURRENT_DIR/(pyproject\.toml|poetry\.lock|tox\.ini))
triggers+=(1-run-tests)
;;
$CURRENT_DIR/*.py)
triggers+=(1-run-tests)
;;
esac
}
run-triggers() {
triggers=($(sort-triggers ${triggers[@]}))
for trigger in $triggers; do
$trigger
done
triggers=()
}
0-update-self() {
triggers=("${triggers[@]:1}")
echo "Running $0 ${triggers[@]} ..."
exec $SELF "${triggers[@]}"
}
1-run-tests() {
echo "Running tests ..."
tox
}
echo "Watching file changes in $CURRENT_DIR ..."
watch-file-changes | on-file-change