Skip to content

Commit 1b9e643

Browse files
committed
Re'orged compiler code.
1 parent 9d7ecfe commit 1b9e643

File tree

3 files changed

+110
-101
lines changed

3 files changed

+110
-101
lines changed

src/docs-compiler.lfe

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
(defmodule docs-compiler
2+
(export all))
3+
4+
(defun lfe-compile (path file)
5+
(let ((outdir (get-ebin-dir file))
6+
(mod-name (get-module-name file))
7+
(filename (filename:join path file)))
8+
(code:purge mod-name)
9+
(logjam:info "Recompiling LFE file ~p ..." `(,filename))
10+
(case (lfe_comp:file filename `(#(outdir ,outdir)))
11+
(`#(ok ,_)
12+
(logjam:debug "Success.")
13+
(logjam:debug "Reloading module ~p ..." `(,mod-name))
14+
(case (code:load_file mod-name)
15+
(`#(module ,_) (logjam:debug "Success."))
16+
(err (logjam:debug "Couldn't reload module: ~p" `(,err)))))
17+
(err (logjam:error "Couldn't compile file: ~p" `(,err))))))
18+
19+
(defun lfe
20+
((_ "docs.app.src" _)
21+
'skipping)
22+
((path file `#(,source-dir ,watcher))
23+
(inotify:unwatch source-dir)
24+
(lfe-compile path file)
25+
(inotify:watch source-dir watcher)))
26+
27+
(defun erlydtl-compile (path file)
28+
(let* ((opts (get-erlydtl-opts))
29+
(outdir (get-ebin-dir file opts))
30+
(all-opts (++ opts `(#(out_dir ,outdir))))
31+
(suffix (get-tmpl-suffix opts))
32+
(mod-name (get-module-name file opts))
33+
(filename (filename:join path file)))
34+
(code:purge mod-name)
35+
(logjam:debug "Recompiling ErlyDTL file: ~p" `(,filename))
36+
(logjam:debug "All compile opts: ~p~n" `(,all-opts))
37+
(case (erlydtl:compile_file filename mod-name all-opts)
38+
(`#(ok ,_mod ,_msgs)
39+
(logjam:debug "Success.")
40+
(logjam:debug "Reloading module ~p ..." `(,mod-name))
41+
;; Reloads are noisy with logs, so let's shutdown the logger until
42+
;; we've finished
43+
(logjam:set-level 'critical)
44+
(case (code:load_file mod-name)
45+
(`#(module ,_) (logjam:debug "Success."))
46+
(err (logjam:debug "Couldn't reload module: ~p" `(,err))))
47+
(logjam:set-level 'info))
48+
(err (logjam:error "Couldn't compile file: ~p" `(,err))))))
49+
50+
(defun erlydtl
51+
((path file `#(,template-dir ,watcher))
52+
(let ((filename (filename:join path file))
53+
(wildcard (filename:join path "*.html")))
54+
(inotify:unwatch template-dir)
55+
(erlydtl-compile path file)
56+
;; Now let's compile all the othter templates, since they might
57+
;; depend upon the once that's just changed
58+
(lists:map
59+
(lambda (filename)
60+
(erlydtl-compile path (filename:basename filename)))
61+
(lists:subtract (filelib:wildcard wildcard) `(,filename)))
62+
;; Finally, re-render the pages with the recompiled templates
63+
(logjam:info "Re-rendering pages ...")
64+
(docs-gen:run-dev)
65+
(inotify:watch template-dir watcher)
66+
(docs-httpd:restart))))
67+
68+
(defun get-module-name (filename)
69+
(clj:-> filename
70+
(filename:rootname)
71+
(list_to_atom)))
72+
73+
(defun get-module-name (filename opts)
74+
(clj:->> opts
75+
(get-tmpl-suffix)
76+
(++ (filename:rootname filename))
77+
(list_to_atom)))
78+
79+
(defun get-ebin-dir (filename)
80+
;; XXX if the result of this is "." we should instead use
81+
;; (get-ebin-dir "docs.lfe") instead
82+
(clj:-> filename
83+
(get-module-name)
84+
(code:which)
85+
(filename:dirname)))
86+
87+
(defun get-ebin-dir (filename opts)
88+
;; XXX if the result of this is "." we should instead use
89+
;; (get-ebin-dir "docs.lfe") instead
90+
(clj:->> opts
91+
(get-module-name filename)
92+
(code:get_object_code)
93+
(element 3)
94+
(filename:dirname)))
95+
96+
(defun get-erlydtl-opts ()
97+
(case (file:consult "rebar.config")
98+
(`#(ok ,data)
99+
(proplists:get_value 'erlydtl_opts data))))
100+
101+
(defun get-tmpl-suffix (opts)
102+
(proplists:get_value 'module_ext opts))

src/docs-watcher.lfe

+7-101
Original file line numberDiff line numberDiff line change
@@ -2,86 +2,24 @@
22
(export all))
33

44
(defun source-data ()
5-
`#("src" ,#'lfe-watcher/1))
5+
`#("src" ,#'docs-watcher:lfe-watcher/1))
66

77
(defun template-data ()
8-
`#("priv/templates" ,#'erlydtl-watcher/1))
8+
`#("priv/templates" ,#'docs-watcher:erlydtl-watcher/1))
99

1010
(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)))
3413

3514
(defun lfe-watcher
3615
((`#(,path file close_write ,fd ,file))
37-
(lfe-compiler path file))
16+
(docs-compiler:lfe path file (source-data)))
3817
((args)
3918
(logjam:debug "Unhandled LFE watcher event: ~p" `(,args))))
4019

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-
8220
(defun erlydtl-watcher
8321
((`#(,path file close_write ,fd ,file))
84-
(erlydtl-compiler path file))
22+
(docs-compiler:erlydtl path file (template-data)))
8523
((args)
8624
(logjam:debug "Unhandled ErlyDTL watcher event: ~p" `(,args))))
8725

@@ -96,7 +34,7 @@
9634
(application:ensure_all_started 'docs)
9735
(docs:start)
9836
(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"
10038
`(,path ,func))
10139
(inotify:watch path func)))
10240
watch-data)
@@ -117,35 +55,3 @@
11755
(stop)
11856
(timer:sleep 500)
11957
(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))

src/docs.app.src

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
docs,
1212
'docs-cfg',
1313
'docs-cli',
14+
'docs-compiler',
1415
'docs-data',
1516
'docs-httpd',
1617
'docs-gen',

0 commit comments

Comments
 (0)