|
2 | 2 | (export all))
|
3 | 3 |
|
4 | 4 | (defun source-data ()
|
5 |
| - `#("src" ,#'lfe-watcher/1)) |
| 5 | + `#("src" ,#'docs-watcher:lfe-watcher/1)) |
6 | 6 |
|
7 | 7 | (defun template-data ()
|
8 |
| - `#("priv/templates" ,#'erlydtl-watcher/1)) |
| 8 | + `#("priv/templates" ,#'docs-watcher:erlydtl-watcher/1)) |
9 | 9 |
|
10 | 10 | (defun default-watch-data ()
|
11 |
| - `(,(template-data) |
12 |
| - ,(source-data))) |
13 |
| - |
14 |
| -(defun lfe-compile (path file) |
15 |
| - (let ((outdir (get-ebin-dir file)) |
16 |
| - (mod-name (get-module-name file)) |
17 |
| - (filename (filename:join path file))) |
18 |
| - (code:purge mod-name) |
19 |
| - (logjam:info "Recompiling LFE file ~p ..." `(,filename)) |
20 |
| - (case (lfe_comp:file filename `(#(outdir ,outdir))) |
21 |
| - (`#(ok ,_) |
22 |
| - (logjam:debug "Success.") |
23 |
| - (logjam:debug "Reloading module ~p ..." `(,mod-name)) |
24 |
| - (case (code:load_file mod-name) |
25 |
| - (`#(module ,_) (logjam:debug "Success.")) |
26 |
| - (err (logjam:debug "Couldn't reload module: ~p" `(,err))))) |
27 |
| - (err (logjam:error "Couldn't compile file: ~p" `(,err)))))) |
28 |
| - |
29 |
| -(defun lfe-compiler (path file) |
30 |
| - (let ((`#(,source-dir ,watcher) (source-data))) |
31 |
| - (inotify:unwatch source-dir) |
32 |
| - (lfe-compile path file) |
33 |
| - (inotify:watch source-dir watcher))) |
| 11 | + `(,(source-data) |
| 12 | + ,(template-data))) |
34 | 13 |
|
35 | 14 | (defun lfe-watcher
|
36 | 15 | ((`#(,path file close_write ,fd ,file))
|
37 |
| - (lfe-compiler path file)) |
| 16 | + (docs-compiler:lfe path file (source-data))) |
38 | 17 | ((args)
|
39 | 18 | (logjam:debug "Unhandled LFE watcher event: ~p" `(,args))))
|
40 | 19 |
|
41 |
| -(defun erlydtl-compile (path file) |
42 |
| - (let* ((opts (get-erlydtl-opts)) |
43 |
| - (outdir (get-ebin-dir file opts)) |
44 |
| - (all-opts (++ opts `(#(out_dir ,outdir)))) |
45 |
| - (suffix (get-tmpl-suffix opts)) |
46 |
| - (mod-name (get-module-name file opts)) |
47 |
| - (filename (filename:join path file))) |
48 |
| - (code:purge mod-name) |
49 |
| - (logjam:debug "Recompiling ErlyDTL file: ~p" `(,filename)) |
50 |
| - (logjam:debug "All compile opts: ~p~n" `(,all-opts)) |
51 |
| - (case (erlydtl:compile_file filename mod-name all-opts) |
52 |
| - (`#(ok ,_mod ,_msgs) |
53 |
| - (logjam:debug "Success.") |
54 |
| - (logjam:debug "Reloading module ~p ..." `(,mod-name)) |
55 |
| - ;; Reloads are noisy with logs, so let's shutdown the logger until |
56 |
| - ;; we've finished |
57 |
| - (logjam:set-level 'critical) |
58 |
| - (case (code:load_file mod-name) |
59 |
| - (`#(module ,_) (logjam:debug "Success.")) |
60 |
| - (err (logjam:debug "Couldn't reload module: ~p" `(,err)))) |
61 |
| - (logjam:set-level 'info)) |
62 |
| - (err (logjam:error "Couldn't compile file: ~p" `(,err)))))) |
63 |
| - |
64 |
| -(defun erlydtl-compiler (path file) |
65 |
| - (let ((`#(,template-dir ,watcher) (template-data)) |
66 |
| - (filename (filename:join path file)) |
67 |
| - (wildcard (filename:join path "*.html"))) |
68 |
| - (inotify:unwatch template-dir) |
69 |
| - (erlydtl-compile path file) |
70 |
| - ;; Now let's compile all the othter templates, since they might |
71 |
| - ;; depend upon the once that's just changed |
72 |
| - (lists:map |
73 |
| - (lambda (filename) |
74 |
| - (erlydtl-compile path (filename:basename filename))) |
75 |
| - (lists:subtract (filelib:wildcard wildcard) `(,filename))) |
76 |
| - ;; Finally, re-render the pages with the recompiled templates |
77 |
| - (logjam:info "Re-rendering pages ...") |
78 |
| - (docs-gen:run-dev) |
79 |
| - (inotify:watch template-dir watcher) |
80 |
| - (docs-httpd:restart))) |
81 |
| - |
82 | 20 | (defun erlydtl-watcher
|
83 | 21 | ((`#(,path file close_write ,fd ,file))
|
84 |
| - (erlydtl-compiler path file)) |
| 22 | + (docs-compiler:erlydtl path file (template-data))) |
85 | 23 | ((args)
|
86 | 24 | (logjam:debug "Unhandled ErlyDTL watcher event: ~p" `(,args))))
|
87 | 25 |
|
|
96 | 34 | (application:ensure_all_started 'docs)
|
97 | 35 | (docs:start)
|
98 | 36 | (case (lists:map (match-lambda ((`#(,path ,func))
|
99 |
| - (logjam:debug "Watching path: ~p with function: ~p" |
| 37 | + (logjam:info "Watching path: ~p with function: ~p" |
100 | 38 | `(,path ,func))
|
101 | 39 | (inotify:watch path func)))
|
102 | 40 | watch-data)
|
|
117 | 55 | (stop)
|
118 | 56 | (timer:sleep 500)
|
119 | 57 | (start))
|
120 |
| - |
121 |
| -(defun get-module-name (filename) |
122 |
| - (clj:-> filename |
123 |
| - (filename:rootname) |
124 |
| - (list_to_atom))) |
125 |
| - |
126 |
| -(defun get-module-name (filename opts) |
127 |
| - (clj:->> opts |
128 |
| - (get-tmpl-suffix) |
129 |
| - (++ (filename:rootname filename)) |
130 |
| - (list_to_atom))) |
131 |
| - |
132 |
| -(defun get-ebin-dir (filename) |
133 |
| - (clj:-> filename |
134 |
| - (get-module-name) |
135 |
| - (code:which) |
136 |
| - (filename:dirname))) |
137 |
| - |
138 |
| -(defun get-ebin-dir (filename opts) |
139 |
| - (clj:->> opts |
140 |
| - (get-module-name filename) |
141 |
| - (code:get_object_code) |
142 |
| - (element 3) |
143 |
| - (filename:dirname))) |
144 |
| - |
145 |
| -(defun get-erlydtl-opts () |
146 |
| - (case (file:consult "rebar.config") |
147 |
| - (`#(ok ,data) |
148 |
| - (proplists:get_value 'erlydtl_opts data)))) |
149 |
| - |
150 |
| -(defun get-tmpl-suffix (opts) |
151 |
| - (proplists:get_value 'module_ext opts)) |
0 commit comments