/
mod_development.erl
403 lines (343 loc) · 14.3 KB
/
mod_development.erl
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
%% @author Marc Worrell <marc@worrell.nl>
%% @author Bryan Fink <bryan@basho.com>
%% @copyright 2009 Marc Worrell
%% Date: 2009-11-01
%% @doc Development server. Periodically performs a "make" and loads new files.
%% When new files are loaded the caches are emptied.
%% Copyright 2010 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
-module(mod_development).
-author("Marc Worrell <marc@worrell.nl>").
-behaviour(gen_server).
-mod_title("Development").
-mod_description("Development support, periodically builds and loads changed files.").
-mod_prio(1000).
%% gen_server exports
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-export([start_link/1]).
%% interface functions
-export([
reload/1,
make/1,
debug_stream/3,
observe_debug_stream/2,
pid_observe_development_reload/3,
pid_observe_development_make/3,
file_changed/2,
observe_admin_menu/3,
observe_module_activate/2,
% internal (for spawn)
page_debug_stream/3,
page_debug_stream_loop/3
]).
-include_lib("zotonic.hrl").
-include_lib("modules/mod_admin/include/admin_menu.hrl").
-record(state, {host}).
% Interval for checking for new and/or changed files.
-define(DEV_POLL_INTERVAL, 10000).
%% Which files do we not consider at all in the file_changed handler
-define(FILENAME_BLACKLIST_RE, "_flymake|\.#").
%%====================================================================
%% API
%%====================================================================
%% @spec start_link(Args) -> {ok,Pid} | ignore | {error,Error}
%% @doc Starts the server
start_link(Args) when is_list(Args) ->
gen_server:start_link(?MODULE, Args, []).
reload(Context) ->
z_notifier:notify(development_reload, Context).
make(Context) ->
z_notifier:notify(development_make, Context).
%% @doc Stream specific debug information to an area on the current page.
debug_stream(TargetId, What, Context) ->
z_notifier:notify1(#debug_stream{target=TargetId, what=What}, Context).
%% @doc Stream all debug information of a certain kind to the target id on the user agent.
observe_debug_stream(#debug_stream{target=TargetId, what=What}, Context) ->
start_debug_stream(TargetId, What, Context).
%% @doc When activating a module while developing, call Module:manage_schema(install, Context).
observe_module_activate(#module_activate{module=Module}, Context) ->
case z_module_manager:reinstall(Module, Context) of
ok ->
lager:info("[~p] Reinstalled module: ~p", [z_context:site(Context), Module]);
nop ->
nop
end.
pid_observe_development_reload(Pid, development_reload, _Context) ->
gen_server:cast(Pid, development_reload).
pid_observe_development_make(Pid, development_make, _Context) ->
gen_server:cast(Pid, development_make).
%% @doc Called when a file is changed on disk. Decides what to do.
%% @spec file_changed(modify | create, string()) -> ok
file_changed(Verb, F) ->
case file_blacklisted(F) of
true -> nop;
false ->
case handle_file(Verb, filename:extension(F), F) of
undefined -> ok;
Message -> send_message(os:type(), z_string:trim(Message))
end
end,
ok.
file_blacklisted(F) ->
case re:run(F, ?FILENAME_BLACKLIST_RE) of
{match, _} ->
true;
nomatch ->
false
end.
%% @doc Recompile Erlang files on the fly
handle_file(_Verb, ".erl", F) ->
spawn(fun() -> recompile_file(F) end),
Libdir = z_utils:lib_dir(),
L = length(Libdir),
F2 = case string:substr(F, 1, L) of
Libdir ->
string:substr(F, L+2);
_ -> F
end,
"Recompile " ++ F2;
%% @doc SCSS / SASS files from lib/scss -> lib/css
handle_file(_Verb, ".sass", F) ->
handle_file(_Verb, ".scss", F);
handle_file(_Verb, ".scss", F) ->
InPath = filename:dirname(F),
OutPath = filename:join(filename:dirname(InPath), "css"),
case filelib:is_dir(OutPath) of
true ->
os:cmd("sass -C --sourcemap=none --update " ++ z_utils:os_escape(InPath) ++ ":" ++ z_utils:os_escape(OutPath));
false ->
undefined
end;
%% @doc LESS from lib/less -> lib/css
handle_file(_Verb, ".less", F) ->
InPath = filename:dirname(F),
OutPath = filename:join(filename:dirname(InPath), "css"),
case filelib:is_dir(OutPath) of
true ->
OutFile = filename:join(OutPath, filename:basename(F, ".less")++".css"),
os:cmd("lessc " ++ z_utils:os_escape(F) ++ " > " ++ z_utils:os_escape(OutFile)),
"Compiled " ++ OutFile;
false ->
undefined
end;
%% @doc Coffeescript from lib/coffee -> lib/js
handle_file(_Verb, ".coffee", F) ->
InPath = filename:dirname(F),
OutPath = filename:join(filename:dirname(InPath), "js"),
case filelib:is_dir(OutPath) of
true ->
os:cmd("coffee -o " ++ z_utils:os_escape(OutPath) ++ " -c " ++ z_utils:os_escape(InPath)),
"Compiled " ++ OutPath;
false ->
undefined
end;
%% @doc Flush
handle_file(_Verb, ".tpl", F) ->
case re:run(F, "/sites/([^/]+).*?/templates/(.*)", [{capture, all_but_first, list}]) of
nomatch ->
%% Flush the cache when a new zotonic-wide .tpl file is used
case re:run(F, ".*?/templates/(.*)", [{capture, all_but_first, list}]) of
nomatch ->
undefined;
{match, [TemplateFile]} ->
case z_template:find_template(TemplateFile, z_context:new(hd(z_sites_manager:get_sites()))) of
{ok, _} ->
undefined;
{error, _} ->
z:flush(),
"Flushed global cache due to new template: " ++ TemplateFile
end
end;
{match, [Site, TemplateFile]} ->
%% Flush the cache when a new .tpl file is used, within a site
C = z_context:new(list_to_atom(Site)),
case z_template:find_template(TemplateFile, C) of
{ok, _} -> undefined;
{error, _} ->
z_notifier:notify(module_ready, C),
z_depcache:flush(C),
"Flushed cache of " ++ Site ++ " due to new template: " ++ TemplateFile
end
end;
%% @doc Flush
handle_file(_Verb, ".po", F) ->
case re:run(F, "/sites/([^/]+).*?/translations/(.*)", [{capture, all_but_first, list}]) of
nomatch ->
%% Flush the cache when a new zotonic-wide .po file is used
case re:run(F, ".*?/translations/(.*)", [{capture, all_but_first, list}]) of
nomatch ->
undefined;
{match, [TranslationFile]} ->
z:flush(),
"Flushed global cache due to updated translation: " ++ TranslationFile
end;
{match, [Site, TranslationFile]} ->
%% Flush the cache when a new .po file is used, within a site
C = z_context:new(list_to_atom(Site)),
Mods = z_module_indexer:translations(C),
InMods = lists:any(fun({_Module, Tls}) ->
lists:any(fun({_Key, T}) ->
string:equal(T, F)
end, Tls)
end, Mods),
case InMods of
false -> undefined;
true ->
z_notifier:notify(module_ready, C),
z_depcache:flush(C),
"Flushed cache of " ++ Site ++ " due to updated translation: " ++ TranslationFile
end
end;
%% @doc Unknown files
handle_file(_, _, F) -> %% unknown filename
case re:run(F, "^.*/dispatch/(.*)") of
nomatch ->
undefined;
{match, _} ->
z:flush(),
"Flushed cache due to dispatch rule change."
end.
%% @doc send message to the user
send_message(_, []) ->
undefined;
send_message({unix, linux}, Msg) ->
os:cmd("which notify-send && notify-send \"Zotonic\" " ++ z_utils:os_escape(Msg));
send_message({unix, darwin}, Msg) ->
os:cmd("which terminal-notifier && terminal-notifier -title Zotonic -message " ++ z_utils:os_escape(Msg));
send_message(_, _) ->
undefined.
%%====================================================================
%% gen_server callbacks
%%====================================================================
%% @spec init(Args) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% @doc Initiates the server.
init(Args) ->
{context, Context} = proplists:lookup(context, Args),
NeedPeriodic = case os:type() of
{unix, linux} ->
case z_filewatcher_inotify:start_link(Context) of
{ok, _} -> false;
{error, _} -> true
end;
{unix, darwin} ->
case z_filewatcher_fswatch:start_link(Context) of
{ok, _} -> false;
{error, _} -> true
end;
_ ->
true
end,
z_code_reloader:start_link(NeedPeriodic),
Host = z_context:site(Context),
lager:md([
{site, Host},
{module, ?MODULE}
]),
{ok, #state{host=Host}}.
%% @spec handle_call(Request, From, State) -> {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% @doc Trap unknown calls
handle_call(Message, _From, State) ->
{stop, {unknown_call, Message}, State}.
%% @spec handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
handle_cast(development_reload, State) ->
z_code_reloader:reload(),
{noreply, State};
handle_cast(development_make, State) ->
z_code_reloader:make(),
{noreply, State};
%% @doc Trap unknown casts
handle_cast(Message, State) ->
{stop, {unknown_cast, Message}, State}.
%% @spec handle_info(Info, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @doc Handling all non call/cast messages
handle_info(_Info, State) ->
{noreply, State}.
%% @spec terminate(Reason, State) -> void()
%% @doc This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any necessary
%% cleaning up. When it returns, the gen_server terminates with Reason.
%% The return value is ignored.
terminate(_Reason, _State) ->
ok.
%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
%% @doc Convert process state when code is changed
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%====================================================================
%% support functions
%%====================================================================
%% @doc Start a listener for a certain kind of debug information, echo it to the target id on the current page.
start_debug_stream(TargetId, What, Context) ->
Context1 = z_context:prune_for_async(Context),
z_session_page:spawn_link(?MODULE, page_debug_stream, [TargetId, What, Context1], Context1).
%% @doc Process started and linked to the current page, subscribes to debug notifications
page_debug_stream(TargetId, What, Context) ->
process_flag(trap_exit, true),
z_notifier:observe(debug, self(), Context),
?MODULE:page_debug_stream_loop(TargetId, What, Context).
page_debug_stream_loop(TargetId, What, Context) ->
receive
{'EXIT', _} ->
z_notifier:detach(debug, self(), Context),
done;
{'$gen_cast', {#debug{what=What, arg=Arg}, _Context}} ->
%% Update the target id with a dump of this debug message
S = io_lib:format("~p: ~p~n", [What, Arg]),
z_session_page:add_script(z_render:insert_top(TargetId, S, Context)),
?MODULE:page_debug_stream_loop(TargetId, What, Context);
{'$gen_cast', {#debug{what=_Other}, _Context}} ->
?MODULE:page_debug_stream_loop(TargetId, What, Context)
end.
observe_admin_menu(admin_menu, Acc, Context) ->
[
#menu_item{id=admin_development,
parent=admin_system,
label=?__("Development", Context),
url={admin_development},
visiblecheck={acl, use, mod_development}}
|Acc].
%% @doc Recompile and reload an Erlang file.
recompile_file(File) ->
Module = list_to_atom(filename:basename(File, ".erl")),
do_observe_fun(Module, fun z_module_manager:remove_observers/3),
make:files([File], [load|zotonic_compile:compile_options()]),
do_observe_fun(Module, fun z_module_manager:add_observers/3).
%% @doc Given an Erlang module that is being recompiled, adds or
%% remove the 'magic' module observers if the erlang module is also a
%% Zotonic module.
do_observe_fun(Module, F) ->
Contexts = [z:c(Site) || [Site, State |_] <- z_sites_manager:get_sites_status(), State =:= running],
lists:foreach(
fun(C) ->
case z_module_manager:whereis(Module, C) of
{ok, Pid} ->
F(Module, Pid, C),
z_depcache:flush(C);
{error, _} ->
nop
end
end,
Contexts).