-
Notifications
You must be signed in to change notification settings - Fork 2
/
modd-watch
82 lines (66 loc) · 2.39 KB
/
modd-watch
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
#!/usr/bin/env bash
dk use: tty
dk.watch() {
require-any modd dk use: golang
require-any modd go install github.com/cortesi/modd/cmd/[email protected] # Need modd for this
require-any tput # for pager.watch
# Trap SIGUSR1 to restart the watch with new configuration
event quote "$@"
trap "echo Configuration changed: restarting watch; echo; exec dk watch ""$REPLY" SIGUSR1
export DEVKIT_PAGER=${DEVKIT_PAGER-pager.watch} # Use the watch pager by default
export DEVKIT_ISATTY=1; isatty || export DEVKIT_ISATTY=0 # modd has no pseudo-tty
# Run modd with our current watch configuration, suppressing "killed" msg
# via background job + wait
modd -cf <(event emit modd_conf) "$@" </dev/null & wait $!
}
watch() {
# Output the command line as part of the reflex config
on modd_conf __put-watch 'prep +onchange:' "$@"
}
watch+ () {
# Run the specified command at the start of watch, as well as on changes
on modd_conf __put-watch 'prep:' "$@"
}
RELOAD_PATTERNS=()
UNWATCH_PATTERNS=()
watch-reload() {
# Restart watch if given pattern matches
export WATCH_PID=$$; RELOAD_PATTERNS+=("$@"); on modd_conf __reload-watch
}
unwatch() {
# Globally exclude the given globs from all watch patterns
UNWATCH_PATTERNS+=("${@/#/!}")
}
__reload-watch() {
__put-watch 'prep +onchange:' "${RELOAD_PATTERNS[@]}" \
-- bash -c 'kill -SIGUSR1 '$WATCH_PID'; kill -9 $PPID'
}
__put-watch() {
# Output a reflex with global exclusion(s) added
local m="$1" patterns command
doubledash-split patterns command "${@:2}"
! ((${#UNWATCH_PATTERNS[@]})) || patterns+=("${UNWATCH_PATTERNS[@]}")
! ((${#patterns[@]})) || { pattern-quote "${patterns[@]}"; pattern=$REPLY; }
! ((${#command[@]})) || { event quote "${command[@]}"; command=$REPLY; }
printf '%s {\n\t%s %s\n}\n\n' "${pattern-}" "$m" "${command-}"
}
doubledash-split() {
local __hd=$1 __tl=$2; shift 2; eval "$__hd=(); $__tl=()"
while (($#)) && [[ $1 != -- ]]; do
eval "$__hd"'+=("$1")'; shift
done
! (($#)) || shift
eval "$__tl"'=("$@")'
}
pattern-quote() {
# Quote patterns in such a way that modd will read them correctly
local bs=\\ q='"' qq=\\'"'
set -- "${@//$bs/$bs$bs}" # double backslashes
set -- "${@//$q/$qq}" # backslash quotes
set -- "${@/#/$q}" # enclose in quotes
set -- "${@/%/$q}"
set -- "${@/#$q!/!$q}" # Move pattern negation outside quotes
REPLY="$*"
}
# automatically re-run watch if configuration changes
watch-reload .dkrc