-
Notifications
You must be signed in to change notification settings - Fork 0
/
lliveWatch
executable file
·71 lines (58 loc) · 1.86 KB
/
lliveWatch
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
#!/usr/bin/env bash
base=$(dirname "$(readlink -f "$0")")
## dont set luce, it has to be local
export LUA_PATH="$base/lib/?.lua;$(lua -e 'print(package.path)')"
export LUA_CPATH="$base/lib/?.so;$(lua -e 'print(package.cpath)')"
inotifywait=$( which inotifywait) || { echo "Please install inotify-tools first"; exit 1; }
ABORT_WITH_SERVER=1
## TODO: watch the classes folder and watch new created classes on the fly
[[ "$1" == -l ]] && LUA="$2" && shift 2
! [[ -f "$1" ]] && echo "Usage: ${0##*/} <main_file_to_watch.lua>" && exit 1
main="$1"; shift
classes=""
while (($#))
do
classes+=" $1"
shift
done
PORT=20087
LUA=${LUA:-$(which luajit || which lua5.1 || which lua || which psm)}
[[ "$LUA" == nil ]] && LUA=""
ME=$(dirname `readlink -f "$0"`)
restartServer() {
if ! lsof -i tcp:${PORT} &>/dev/null
then
${LUA:+"$LUA"} "$ME"/LLive.lua &
fi
echo "D: Waiting for server..."
ok=1
timeout=0
max=5
step=0.5
echo `lua -e "print( ($timeout < $max) and 1 or 0 )"`
while (( `lua -e "print( ($timeout < $max) and 1 or 0 )"` ))
do
lsof -i tcp:${PORT} &>/dev/null && ok=0 && break
sleep $step
timeout=$(bc <<< $timeout+$step)
done
return $ok
}
! restartServer && echo "ERROR: Couldn't start Luce Live Coding server!" && exit 1
echo "D: Loading main class..."
${LUA:+"$LUA"} "$ME"/LLiveClient.lua "$main"
while true
do
## TODO: check PID instead of socket
lsof -i tcp:${PORT} &>/dev/null
notRunning=$?
if ((ABORT_WITH_SERVER))
then
((notRunning)) && echo "ERROR: Lost contact with Luce Live Coding server - abort" && exit 1
else
((notRunning)) && ! restartServer && echo "ERROR: Can't restart server" && exit 1
fi
$inotifywait -t 1 -e modify "$main" $classes &>/dev/null
(($?==2)) && continue
${LUA:+"$LUA"} "$ME"/LLiveClient.lua "$main" "true"
done