-
Notifications
You must be signed in to change notification settings - Fork 3
/
preview.sh
executable file
·88 lines (74 loc) · 2.79 KB
/
preview.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
#!/bin/bash
# script to watch source directory for changes, and re-run build and preview
previewpidfile="preview.pid"
# 4913: for vim users, vim creates a temporary file to test it can write to
# directory
# https://groups.google.com/g/vim_dev/c/sppdpElxY44
# .git: so we don't get rebuilds each time git metadata changes
inotifyignore="\.git.*|4913"
watch_and_preview () {
if ! command -v inotifywait > /dev/null
then
echo "inotifywait command could not be found. Please install inotify-tools."
echo "On Fedora, run: sudo dnf install inotify-tools"
echo "On *buntu, run: sudo apt-get install inotify-tools"
stop_preview_and_exit
else
# check for git
# required to get ignorelist
if ! command -v git > /dev/null
then
echo "git command could not be found. Please install git."
echo "On Fedora, run: sudo dnf install git-core"
stop_preview_and_exit
else
# Get files not being tracked, we don't watch for changes in these.
# Could hard code, but people use different editors that may create
# temporary files that are updated regularly and so on, so better
# to get the list from git. It'll also look at global gitingore
# settings and so on.
inotifyignore="$(git status -s --ignored | grep '^!!' | sed -e 's/^!! //' | tr '\n' '|')${inotifyignore}"
fi
while true
do
echo "Watching current directory (excluding: ${inotifyignore}) for changes and re-building as required. Use Ctrl C to stop."
inotifywait -q --exclude "($inotifyignore)" -e modify,create,delete,move -r . && echo "Change detected, rebuilding.." && start_preview
done
fi
}
start_preview (){
stop_preview
if ! command -v panel > /dev/null
then
echo "panel command was not found. Please see the README file to set up your environment."
stop_preview_and_exit
else
panel serve src/project_browser.py --static-dirs assets=./assets &
echo "$!" > "${previewpidfile}"
fi
}
stop_preview () {
if [ -e "${previewpidfile}" ]
then
PID=$(cat "${previewpidfile}")
kill $PID
echo "Stopping preview server (running with PID ${PID}).."
rm -f "${previewpidfile}"
else
echo "No running preview server found to stop: no ${previewpidfile} file found."
fi
}
stop_preview_and_exit ()
{
# stop and also exit the script
# if stop_preview is trapped, then SIGINT doesn't stop the build loop. So
# we need to make sure we also exit the script.
# stop_preview is called before other functions, so we cannot add exit to
# it.
stop_preview
exit 0
}
trap stop_preview_and_exit INT
start_preview
watch_and_preview
stop_preview