Skip to content
Newer
Older
100644 162 lines (136 sloc) 4.71 KB
1c63c5f @mdempsky import reloader.erl for automatically reloading modules during develo…
mdempsky authored Dec 6, 2007
1 %% @copyright 2007 Mochi Media, Inc.
2 %% @author Matthew Dempsky <matthew@mochimedia.com>
3 %%
4 %% @doc Erlang module for automatically reloading modified modules
5 %% during development.
6
7 -module(reloader).
8 -author("Matthew Dempsky <matthew@mochimedia.com>").
9
10 -include_lib("kernel/include/file.hrl").
11
12 -behaviour(gen_server).
13 -export([start/0, start_link/0]).
3f36ec4 provide stop() function
Emad El-Haraty authored Dec 9, 2007
14 -export([stop/0]).
1c63c5f @mdempsky import reloader.erl for automatically reloading modules during develo…
mdempsky authored Dec 6, 2007
15 -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
48c9c1d @etrepum reloader:all_changed/0 reloader:is_changed/1
etrepum authored May 10, 2010
16 -export([all_changed/0]).
17 -export([is_changed/1]).
c1a1842 @etrepum reload_modules/1
etrepum authored May 10, 2010
18 -export([reload_modules/1]).
3f36ec4 provide stop() function
Emad El-Haraty authored Dec 9, 2007
19 -record(state, {last, tref}).
1c63c5f @mdempsky import reloader.erl for automatically reloading modules during develo…
mdempsky authored Dec 6, 2007
20
7fd4a1c @etrepum docs for reloader
etrepum authored Dec 26, 2007
21 %% External API
22
23 %% @spec start() -> ServerRet
24 %% @doc Start the reloader.
1c63c5f @mdempsky import reloader.erl for automatically reloading modules during develo…
mdempsky authored Dec 6, 2007
25 start() ->
26 gen_server:start({local, ?MODULE}, ?MODULE, [], []).
27
7fd4a1c @etrepum docs for reloader
etrepum authored Dec 26, 2007
28 %% @spec start_link() -> ServerRet
29 %% @doc Start the reloader.
1c63c5f @mdempsky import reloader.erl for automatically reloading modules during develo…
mdempsky authored Dec 6, 2007
30 start_link() ->
31 gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
32
7fd4a1c @etrepum docs for reloader
etrepum authored Dec 26, 2007
33 %% @spec stop() -> ok
34 %% @doc Stop the reloader.
35 stop() ->
36 gen_server:call(?MODULE, stop).
37
38 %% gen_server callbacks
39
40 %% @spec init([]) -> {ok, State}
41 %% @doc gen_server init, opens the server in an initial state.
1c63c5f @mdempsky import reloader.erl for automatically reloading modules during develo…
mdempsky authored Dec 6, 2007
42 init([]) ->
3f36ec4 provide stop() function
Emad El-Haraty authored Dec 9, 2007
43 {ok, TRef} = timer:send_interval(timer:seconds(1), doit),
44 {ok, #state{last = stamp(), tref = TRef}}.
1c63c5f @mdempsky import reloader.erl for automatically reloading modules during develo…
mdempsky authored Dec 6, 2007
45
7fd4a1c @etrepum docs for reloader
etrepum authored Dec 26, 2007
46 %% @spec handle_call(Args, From, State) -> tuple()
47 %% @doc gen_server callback.
3f36ec4 provide stop() function
Emad El-Haraty authored Dec 9, 2007
48 handle_call(stop, _From, State) ->
49 {stop, shutdown, stopped, State};
1c63c5f @mdempsky import reloader.erl for automatically reloading modules during develo…
mdempsky authored Dec 6, 2007
50 handle_call(_Req, _From, State) ->
51 {reply, {error, badrequest}, State}.
52
7fd4a1c @etrepum docs for reloader
etrepum authored Dec 26, 2007
53 %% @spec handle_cast(Cast, State) -> tuple()
54 %% @doc gen_server callback.
1c63c5f @mdempsky import reloader.erl for automatically reloading modules during develo…
mdempsky authored Dec 6, 2007
55 handle_cast(_Req, State) ->
56 {noreply, State}.
57
7fd4a1c @etrepum docs for reloader
etrepum authored Dec 26, 2007
58 %% @spec handle_info(Info, State) -> tuple()
59 %% @doc gen_server callback.
1c63c5f @mdempsky import reloader.erl for automatically reloading modules during develo…
mdempsky authored Dec 6, 2007
60 handle_info(doit, State) ->
61 Now = stamp(),
62 doit(State#state.last, Now),
63 {noreply, State#state{last = Now}};
64 handle_info(_Info, State) ->
65 {noreply, State}.
66
7fd4a1c @etrepum docs for reloader
etrepum authored Dec 26, 2007
67 %% @spec terminate(Reason, State) -> ok
68 %% @doc gen_server termination callback.
3f36ec4 provide stop() function
Emad El-Haraty authored Dec 9, 2007
69 terminate(_Reason, State) ->
70 {ok, cancel} = timer:cancel(State#state.tref),
1c63c5f @mdempsky import reloader.erl for automatically reloading modules during develo…
mdempsky authored Dec 6, 2007
71 ok.
72
3f36ec4 provide stop() function
Emad El-Haraty authored Dec 9, 2007
73
7fd4a1c @etrepum docs for reloader
etrepum authored Dec 26, 2007
74 %% @spec code_change(_OldVsn, State, _Extra) -> State
75 %% @doc gen_server code_change callback (trivial).
1c63c5f @mdempsky import reloader.erl for automatically reloading modules during develo…
mdempsky authored Dec 6, 2007
76 code_change(_Vsn, State, _Extra) ->
77 {ok, State}.
78
c1a1842 @etrepum reload_modules/1
etrepum authored May 10, 2010
79 %% @spec reload_modules([atom()]) -> [{module, atom()} | {error, term()}]
80 %% @doc code:purge/1 and code:load_file/1 the given list of modules in order,
81 %% return the results of code:load_file/1.
82 reload_modules(Modules) ->
83 [begin code:purge(M), code:load_file(M) end || M <- Modules].
84
48c9c1d @etrepum reloader:all_changed/0 reloader:is_changed/1
etrepum authored May 10, 2010
85 %% @spec all_changed() -> [atom()]
86 %% @doc Return a list of beam modules that have changed.
87 all_changed() ->
88 [M || {M, Fn} <- code:all_loaded(), is_list(Fn), is_changed(M)].
89
90 %% @spec is_changed(atom()) -> boolean()
91 %% @doc true if the loaded module is a beam with a vsn attribute
92 %% and does not match the on-disk beam file, returns false otherwise.
93 is_changed(M) ->
94 try
95 module_vsn(M:module_info()) =/= module_vsn(code:get_object_code(M))
96 catch _:_ ->
97 false
98 end.
99
7fd4a1c @etrepum docs for reloader
etrepum authored Dec 26, 2007
100 %% Internal API
1c63c5f @mdempsky import reloader.erl for automatically reloading modules during develo…
mdempsky authored Dec 6, 2007
101
48c9c1d @etrepum reloader:all_changed/0 reloader:is_changed/1
etrepum authored May 10, 2010
102 module_vsn({M, Beam, _Fn}) ->
103 {ok, {M, Vsn}} = beam_lib:version(Beam),
104 Vsn;
105 module_vsn(L) when is_list(L) ->
106 {_, Attrs} = lists:keyfind(attributes, 1, L),
107 {_, Vsn} = lists:keyfind(vsn, 1, Attrs),
108 Vsn.
109
1c63c5f @mdempsky import reloader.erl for automatically reloading modules during develo…
mdempsky authored Dec 6, 2007
110 doit(From, To) ->
111 [case file:read_file_info(Filename) of
6d300db @etrepum patch from Kostis Sagonas, obtained by using tidier, a tool that tidi…
etrepum authored Nov 5, 2009
112 {ok, #file_info{mtime = Mtime}} when Mtime >= From, Mtime < To ->
3f7ef81 @etrepum detab source, chunked-like responses for HTTP/1.0 clients
etrepum authored Apr 29, 2008
113 reload(Module);
1c63c5f @mdempsky import reloader.erl for automatically reloading modules during develo…
mdempsky authored Dec 6, 2007
114 {ok, _} ->
115 unmodified;
116 {error, enoent} ->
117 %% The Erlang compiler deletes existing .beam files if
118 %% recompiling fails. Maybe it's worth spitting out a
119 %% warning here, but I'd want to limit it to just once.
120 gone;
121 {error, Reason} ->
122 io:format("Error reading ~s's file info: ~p~n",
123 [Filename, Reason]),
124 error
125 end || {Module, Filename} <- code:all_loaded(), is_list(Filename)].
126
3f7ef81 @etrepum detab source, chunked-like responses for HTTP/1.0 clients
etrepum authored Apr 29, 2008
127 reload(Module) ->
128 io:format("Reloading ~p ...", [Module]),
129 code:purge(Module),
130 case code:load_file(Module) of
131 {module, Module} ->
132 io:format(" ok.~n"),
133 case erlang:function_exported(Module, test, 0) of
134 true ->
135 io:format(" - Calling ~p:test() ...", [Module]),
136 case catch Module:test() of
137 ok ->
138 io:format(" ok.~n"),
139 reload;
140 Reason ->
141 io:format(" fail: ~p.~n", [Reason]),
142 reload_but_test_failed
143 end;
144 false ->
145 reload
146 end;
147 {error, Reason} ->
148 io:format(" fail: ~p.~n", [Reason]),
149 error
150 end.
151
152
1c63c5f @mdempsky import reloader.erl for automatically reloading modules during develo…
mdempsky authored Dec 6, 2007
153 stamp() ->
154 erlang:localtime().
ca962ce @etrepum refactor to use eunit, generate code coverage, update Makefiles and s…
etrepum authored Jan 2, 2010
155
156 %%
157 %% Tests
158 %%
159 -include_lib("eunit/include/eunit.hrl").
160 -ifdef(TEST).
161 -endif.
Something went wrong with that request. Please try again.