-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebuild-daemon.sh
105 lines (87 loc) · 2.49 KB
/
rebuild-daemon.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
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
#!/usr/bin/env bash
MAKE=make DEVELOPMENT_MODE=1
COMMUNICATE_PIPE=$1
ALIVE_FILE=$2
# if the daemon is already alive, exit
if [ -f $ALIVE_FILE ]; then
echo "! Daemon already alive, exiting"
exit 0
fi
shopt -s globstar
SCRIPT_NAME=$($MAKE -s print_DAEMON_SCRIPT)
# if the command is not found
if ! command -v inotifywait &> /dev/null; then
# check for nix-shell
if ! command -v nix-shell &> /dev/null; then
echo "! inotifywait not found, please install inotify-tools."
exit 1
fi
# if we're already in a re-run and it's still not found, fail
if [ -n "$__DAEMON_RERUN" ]; then
echo "! inotifywait not found after nix-shell rerun, exiting"
exit 1
fi
echo "@ inotifywait not found, trying to run with nix-shell"
# Try to run with no command to prefetch it and check if the package is found.
# If the system's nix channels are not setup properly, this will fail.
#TODO: Maybe add a fallback using the new nix commands? `nix develop` or `nix run`
nix-shell -p inotify-tools --run "exit"
if [ $? -eq 0 ]; then
echo "@ nix-shell succeeded, re-running script"
export __DAEMON_RERUN=1
nix-shell -p inotify-tools --run "bash $SCRIPT_NAME $COMMUNICATE_PIPE $ALIVE_FILE"
exit $?
else
echo "! nix-shell failed, exiting"
fi
exit 1
fi
clear
echo
echo "> Hi, I'm the rebuild daemon."
echo
function cleanup {
rm -rf $COMMUNICATE_PIPE
rm -rf $ALIVE_FILE
}
cleanup
touch $ALIVE_FILE
trap cleanup EXIT INT TERM QUIT
sleep 1
echo "> Initial setup, running \`remake\`"
$MAKE remake || true
echo ""
while true; do
if [ ! -f $ALIVE_FILE ]; then
echo "! Daemon not alive, exiting"
exit 0
fi
echo "[*] Watching for changes..."
# wait for inotify events (any file changes)
FILE=$(inotifywait Makefile ./submodules/submodule.mk ./submodules/*/Makefile ./submodules/**/*.{c,h} -r -e modify,move,create,delete,attrib,close_write,moved_to,moved_from 2>/dev/null)
if [ $? -ne 0 ]; then
break
fi
clear
# get the first before " "
FILE=${FILE%% *}
echo "> File changed: $FILE"
FILENAME=${FILE##*/}
if [[ $FILENAME == "Makefile" ]]; then
echo "> Makefile changed, running \`remake\`"
$MAKE remake || true
else
echo "> Source file changed, running \`make\`"
$MAKE MAKE_MULTITHREAD=0 || true
fi
# is rt/miniRT running?
ps -aux | grep -v grep | grep -E "\./rt|miniRT" > /dev/null
if [ $? -eq 0 ]; then
touch $COMMUNICATE_PIPE
# wait for the program to acknowledge the change
echo "> Waiting for acknowledge..."
inotifywait $COMMUNICATE_PIPE -t 5
rm -rf $COMMUNICATE_PIPE
echo "> ACK'd"
fi
done