-
Notifications
You must be signed in to change notification settings - Fork 1
/
user_default.erl
executable file
·337 lines (282 loc) · 8.23 KB
/
user_default.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
-module(user_default).
-export([l/0, nl/0, mm/0]).
-export([p/1, s/1]).
-export([help/0,dbgtc/1, dbgon/1, dbgon/2,
dbgadd/1, dbgadd/2, dbgdel/1, dbgdel/2, dbgoff/0]).
-export(
[load_conf/0,
extend_paths/0]
).
-export([rr/0]).
%-compile(export_all).
-import(io, [format/1]).
%% - should add configurable compiler flags to use by default?
%% * can we sneak that into Makefile somehow?
%% * perhaps per-file? e.g., switch debug on/off ...
load_conf() ->
Conf = "./.econf",
user_default_config:load_config(Conf).
extend_paths() ->
case user_default_config:get(ebin) of
{found, Paths} ->
code:add_pathsa(Paths);
not_found ->
[]
end.
%% @doc make in current dir (see also mk/0 for more flex)
mk0() ->
io:format("~s\n", [os:cmd("make")]).
%% @doc make, then load all changed modules
mkl() ->
mk0(),
l().
%% mk/0 reads the directory to make first, then collects the modified
%% modules, loads them, and runs their EUnit tests (if any).
%%
%% Note: we need to run mm/0 before loading changed modules, since this
%% set is changed by running the load.
%%
%% Note: we could run nl() instead of l(), but this way we confine all
%% changes to the current node. On the other hand, we probably should
%% ALWAYS use nl() when running distributed erlang, because it otherwise
%% gets difficult to summarize where code is up-to-date.
mk() ->
Dirs = config_value(),
mk_dirs(Dirs).
mk_dirs(Dirs) ->
lists:foreach(
fun(Dir) ->
io:format("Make ~s\n", [Dir]),
mk(Dir)
end,
Dirs).
fmt(Str, Xs) ->
case catch io:format(Str, Xs) of
ok ->
ok;
Err ->
io:format("Error ~p\n", [Err])
end.
mk(Dir) ->
fmt("~s\n",
[os:cmd(
lists:flatten(
io_lib:format("(cd ~s && make)", [quote(Dir)])))]),
Ms = mm(),
l(),
%% UNFINISHED
%% - is this useful? not being used
lists:foreach(
fun(M) ->
case catch M:test() of
{'EXIT', {undef, _}} ->
%% skip modules w/o EUnit entry point
ok;
ok ->
%% (EUnit will normally print result)
ok;
Res ->
io:format("EUnit: ~p -> ~p\n", [M, Res])
end
end,
Ms).
%% UNFINISHED
%% - this should
quote(Dir) ->
Dir.
config_value() ->
user_default_config:get(mkdir, []).
%% @doc Print entire value
p(X) ->
io:format("~p\n", [X]).
%% @doc Print string (possibly io-list, so p/1 gets ugly)
s(Str) ->
io:format("~s\n", [Str]).
%% @doc EUnit for module M.
u(M) ->
case catch M:test() of
{'EXIT', {undef, _}} ->
ok;
Res ->
Res
end.
%% @doc Load and unit-test all changed modules.
u() ->
Ms = mm(),
l(),
lists:foreach(
fun(M) ->
io:format("Unit testing ~p -> ~p\n", [M, u(M)])
end,
Ms
).
%% @doc Load all changed modules on current node.
l() ->
lists:foreach(
fun(M) ->
io:format("Loading ~p -> ~p~n", [M, c:l(M)])
end,
mm()
).
%% @doc Load all changed modules on all visible nodes
%%
%% UNFINISHED - does this load new code on slave nodes? if not, how
%% do we do that?
nl() ->
lists:foreach(
fun(M) ->
io:format("Network loading ~p -> ~p~n", [M, c:nl(M)])
end,
mm()
).
%% Vladimir Sekissov's fine code
mm() ->
modified_modules().
%% @doc Check changed modules on node N. We probably want this
%% instead: compare mod date of modules on node N with mod date on
%% current node [then do nl(M) if changed]
mm(Node) ->
rpc:call(Node, ?MODULE, mm, []).
%% List of all modified modules.
modified_modules() ->
code:modified_modules().
%% @doc Here is the actual work horse for OLD OTP. Note that this required
%% compile time as an attribute
%%
%% OBSOLETE
%% - we keep it around for older versions of OTP
old_modified_modules() ->
[M || {M, _} <- code:all_loaded(), module_modified(M) == true].
module_modified(Module) ->
case code:is_loaded(Module) of
{file, preloaded} ->
false;
{file, Path} ->
CompileOpts = proplists:get_value(compile, Module:module_info()),
CompileTime = proplists:get_value(time, CompileOpts),
Src = proplists:get_value(source, CompileOpts),
module_modified(Path, CompileTime, Src);
_ ->
false
end.
module_modified(Path, PrevCompileTime, PrevSrc) ->
case find_module_file(Path) of
false ->
false;
ModPath ->
{ok, {_, [{_, CB}]}} = beam_lib:chunks(ModPath, ["CInf"]),
CompileOpts = binary_to_term(CB),
CompileTime = proplists:get_value(time, CompileOpts),
Src = proplists:get_value(source, CompileOpts),
not (CompileTime == PrevCompileTime) and (Src == PrevSrc)
end.
find_module_file(Path) ->
case file:read_file_info(Path) of
{ok, _} ->
Path;
_ ->
%% maybe the path was changed?
%% NB: the following does not exist ... but the whole works
%% fairly well anyway
case code:where_is_file(filename:basename(Path)) of
non_existing ->
false;
NewPath ->
NewPath
end
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% rr() - load all record definitions in all beam files
%%
%% UNFINISHED
%% - rr/1 is not an exported function from shell.erl
%% * shell:shell_cmd/6 might be abused to do it, but there are many
%% params that need to be passed correctly
%% * perhaps better left internal
rr() ->
lists:foreach(
fun(M) ->
io:format("rr(~p) -> ~p\n", [M, c:rr(M)])
end,
[ M || {M, _Obj} <- code:all_loaded()]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Run M:F(A1,...,An) on all visible nodes (perhaps just this one).
mc(M, F, As) ->
Nodes = nodes(),
case Nodes of
[] ->
apply(M, F, As);
_ ->
rpc:multicall(M, F, As)
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% By "Serge Aleynikov" <serge@hq.idt.net>, erlang-questions Sep 2006
help() ->
shell_default:help(),
io:format("** user extended commands **~n"),
io:format("dbgtc(File) -- use dbg:trace_client() to read data from "
"File\n"),
io:format("dbgon(M) -- enable dbg tracer on all funs in module "
"M\n"),
io:format("dbgon(M,Fun) -- enable dbg tracer for module M and "
"function F\n"),
io:format("dbgon(M,File) -- enable dbg tracer for module M and log to "
"File\n"),
io:format("dbgadd(M) -- enable call tracer for module M\n"),
io:format("dbgadd(M,F) -- enable call tracer for function M:F\n"),
io:format("dbgdel(M) -- disable call tracer for module M\n"),
io:format("dbgdel(M,F) -- disable call tracer for function M:F\n"),
io:format("dbgoff() -- disable dbg tracer (calls "
"dbg:stop/0)\n"),
io:format("mk() -- run make in configured dir, load, unit test\n"),
io:format("mk(D) -- run make in dir D, load, unit test\n"),
io:format("mk0() -- run make in current directory\n"),
io:format("l() -- load all changed modules\n"),
io:format("nl() -- load all changed modules on all known "
"nodes\n"),
io:format("mm() -- list modified modules\n"),
true.
dbgtc(File) ->
Fun = fun({trace,_,call,{M,F,A}}, _) ->
io:format("call: ~w:~w~w~n", [M,F,A]);
({trace,_,return_from,{M,F,A},R}, _) ->
io:format("retn: ~w:~w/~w -> ~w~n", [M,F,A,R]);
(A,B) ->
io:format("~w: ~w~n", [A,B])
end,
dbg:trace_client(file, File, {Fun, []}).
dbgon(Module) ->
case dbg:tracer() of
{ok,_} ->
dbg:p(all,call),
dbg:tpl(Module, [{'_',[],[{return_trace}]}]),
ok;
Else ->
Else
end.
dbgon(Module, Fun) when is_atom(Fun) ->
{ok,_} = dbg:tracer(),
dbg:p(all,call),
dbg:tpl(Module, Fun, [{'_',[],[{return_trace}]}]),
ok;
dbgon(Module, File) when is_list(File) ->
{ok,_} = dbg:tracer(file, dbg:trace_port(file, File)),
dbg:p(all,call),
dbg:tpl(Module, [{'_',[],[{return_trace}]}]),
ok.
dbgadd(Module) ->
dbg:tpl(Module, [{'_',[],[{return_trace}]}]),
ok.
dbgadd(Module, Fun) ->
dbg:tpl(Module, Fun, [{'_',[],[{return_trace}]}]),
ok.
dbgdel(Module) ->
dbg:ctpl(Module),
ok.
dbgdel(Module, Fun) ->
dbg:ctpl(Module, Fun),
ok.
dbgoff() ->
dbg:stop().