/
hotreload.sh
executable file
·223 lines (200 loc) · 8.07 KB
/
hotreload.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env bash
# ##################################################
# Functions to drive browser hot reload.
#
# USAGE
#
# Source these files and call the shite_hot_build_reload function:
#
# source ./bin/utils.sh
# source ./bin/events.sh
# source ./bin/hotreload.sh
#
# shite_hot_build_reload [WATCH DIR] [BROWSER TAB NAME] \
# [OPTIONAL BROWSER NAME] \
# [OPTIONAL BASE URL]
#
# It assumes Mozilla Firefox and file:// based navigation by default.
#
#
# EXAMPLES
#
# shite_hot_build_reload "public" 'A site'
#
# shite_hot_build_reload "public" 'A site' "Google Chrome"
#
# shite_hot_build_reload "public" 'A site' "Google Chrome" "http://localhost:8080"
#
#
# LOGIC
#
# Given a stream of event records, in shite's "canonical" CSV record structure:
# - INTERPRET events and generate commands to feed to a command executor
# - "Content" (html) file changes require special handling
# - "Static" (css,js,img) file changes only ever trigger page reload
# - EXECUTE the incoming commands in a streaming fashion
#
#
# WARNINGS
#
# Beware of stdio buffering. Use stdbuf to control it.
#
# Some tools like grep and awk buffer their output and thus block downstream
# actions. But we want instant reaction time. So those buffers are bad for us.
#
# Luckily stdbuf is a general way to tune buffering behaviour. We can work
# with line-buffered output streaming. e.g.:
#
# stdbuf -oL grep something
#
# c.f https://www.perkin.org.uk/posts/how-to-fix-stdio-buffering.html
# ##################################################
# ##################################################
# BROWSER COMMAND INTERPRETER FOR FILE EVENTS
# ##################################################
__shite_hot_cmd_browser_refresh() {
local window_id=${1:?"Fail. We expect window ID to be set in scope."}
printf "%s\n" \
"key --window ${window_id} --clearmodifiers 'F5'"
}
__shite_hot_cmd_goto_url() {
local window_id=${1}
local url=${2}
printf "%s\n" \
"key --window ${window_id} --clearmodifiers 'ctrl+l'" \
"type --window ${window_id} --clearmodifiers --delay 1 ${url}" \
"key --window ${window_id} --clearmodifiers 'Return'"
}
__shite_hot_cmd_public_events() {
# Consume the shite event stream and generate xdotool commands to drive
# browser navigation, based on information like event type, file type,
# and how the public file has changed (updated, deleted, moved etc.).
#
# The browser should react only to changes in the `public` directory,
# so we select only those events.
#
# This interpreter:
#
# - Ignores events for non-public files
# - Generates special commands depending on type of modifications to HTML
# "content" pages
# - Emits a catch-all page reload command for events for non-"content"
# files. i.e. "Reload page" is the only sane thing to do when, say,
# some CSS or JS or image changes.
#
local window_id=${1:?"Fail. We expect a window ID."}
local base_url=${2:?"Fail. We expect a base url."}
local url_status
local prev_url_slug
# Process events only for relevant `public` files.
__shite_events_select_public |
__shite_events_drop_public_noisy_events |
# Generate commands for browser hot reload / navigate.
while IFS=',' read -r timestamp event_type watch_dir sub_dir url_slug file_type content_type
do
# TODO: Find a cleaner alternative. This stateful logic messes up
# occasionally, especially when I also click about the site manually.
# It's not annoying UX-wise, just annoying that the bug exists.
url_status=$(
if [[ "${url_slug}" == "${prev_url_slug}" ]]
then printf "%s" "SAMEURL"
else printf "%s" "NEWURL"
fi
)
case "${event_type}:${file_type}:${url_status}" in
# RELOAD
# - When any content file is modified
# - When any non-current content file is deleted
# (because that may affect the current page)
MODIFY:html:SAMEURL ) ;& # catch vim edits
CLOSE_WRITE:CLOSE:html:SAMEURL ) ;& # catch emacs edits
DELETE:html:NEWURL )
__shite_hot_cmd_browser_refresh ${window_id}
;;
# GOTO - NAVIGATE
# - Newly-created content file, or
# - Moved/renamed content file
MODIFY:html:NEWURL ) ;& # vim new file
CLOSE_WRITE:CLOSE:html:NEWURL ) ;& # emacs new file
CREATE:html:* ) ;&
MOVED_TO:html:* )
__shite_hot_cmd_goto_url \
${window_id} \
"${base_url}/${url_slug}"
;;
# GOTO - FALLBACK
# - home page when the current content file is deleted
DELETE:html:SAMEURL )
__shite_hot_cmd_goto_url \
${window_id} \
"${base_url}/index.html"
;;
# RELOAD page for any action on non-content pages,
# presumably static assets.
* )
__shite_hot_cmd_browser_refresh ${window_id}
;;
esac
# Remember the file for the next cycle
prev_url_slug=${url_slug}
done
}
# ##################################################
# COMMAND EXECUTOR
# ##################################################
__shite_hot_cmd_exec() {
# In debug mode, only show the actions, don't do them.
if [[ ${SHITE_BUILD} == "hot" ]]
then stdbuf -oL grep -v '^$' | __tap_stream | xdotool -
else cat -
fi
}
shite_hot_browser_reload() {
local browser_window_id=${1:?"Fail. Window ID not set."}
local base_url=${base_url:?"Fail. Base URL not set."}
__shite_hot_cmd_public_events ${browser_window_id} ${base_url} |
__shite_hot_cmd_exec
}
shite_hot_watch_file_events() {
# Watch all files we care about, across sources (org, md, CSS, JS etc.), and
# public (published HTML, CSS, JS etc.), for events of interest, viz.:
# 'create,modify,close_write,moved_to,delete'
local watch_dir=${1:?"Fail. Please specify a directory to watch"}
local events=${2:-'create,modify,close_write,moved_to,delete'}
shite_events_stream ${watch_dir} ${events}
}
shite_hot_build() {
local base_url=${1:?"Fail. We expect a base URL like 'file://'"}
# React to source events and CRUD public files
shite_templating_publish_sources ${base_url} > /dev/null
}
shite_hot_build_reload() {
# React to various file events, hot-build-and-publish `sources` to `public`,
# and then hot-reload (navigate) the browser.
#
# Browser navigation will work only when the site's tab is currently active.
# Hot build / publish works whether or not the tab is active.
# Maybe improve with getopts later
local watch_dir=${1:?"Fail. Please specify a directory to watch"}
local browser_name=${2:?"Fail. We expect a browser name like \"Mozilla Firefox\"."}
local base_url=${3:?"Fail. We expect a base URL like 'file://'"}
# LOOKUP WINDOW ID
local window_id
window_id=$(xdotool search --onlyvisible --name ".*${browser_name}$")
__log_info "$(printf "%s" "Hotreloadin' your shite now! " \
"'{" \
"\"watch_dir\": \"$(realpath ${watch_dir})\", "\
"\"browser_name\": \"${browser_name}\", " \
"\"base_url\": \"${base_url}\", " \
"\"window_id\": \"${window_id}\"" \
"}'")"
# RUN PIPELINE
shite_hot_watch_file_events ${watch_dir} |
__shite_events_dedupe |
__tap_stream |
tee >(shite_hot_build ${base_url}) |
# Perform hot-reload actions only against changes to public files
tee >(shite_hot_browser_reload ${window_id} ${base_url}) |
# Trigger rebuilds of metadata indices
tee >(shite_metadata_rebuild_indices)
}