forked from erlangpack/mimetypes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_compile.escript
executable file
·41 lines (33 loc) · 1.34 KB
/
post_compile.escript
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
#!/usr/bin/env escript
%%! -pa ebin
main(_) ->
{ok, [T]} = file:consult("ebin/mimetypes.app"),
{application, mimetypes, PL} = T,
DynMods = [mimetypes:dmodule(),mimetypes:mmodule()],
BaseModules = proplists:get_value(modules, PL) -- DynMods,
Modules = DynMods ++ BaseModules,
Timestamps = last(["priv/mime.types"|files(BaseModules)]),
[DModTimestamp,MModTimestamp] = last(files(DynMods)),
DynMinTimestamp = erlang:min(DModTimestamp, MModTimestamp),
case lists:any(fun(TS) ->
TS > DynMinTimestamp
end, Timestamps) of
true ->
[ file:delete(F) || F <- files(DynMods) ],
ok = application:start(mimetypes),
ok = application:stop(mimetypes),
{ok, F} = file:open("ebin/mimetypes.app",[write]),
NewPL = lists:keyreplace(modules, 1, PL, {modules, Modules}),
NewApp = {application, mimetypes, NewPL},
io:format(F, "~s.\n", [io_lib_pretty:print(NewApp)]),
file:close(F);
false ->
%% everything is up to date
ok
end.
files([]) ->
[];
files([H|T]) ->
[ filename:join(["ebin",atom_to_list(H) ++ ".beam"]) | files(T) ].
last(Files) ->
[ case filelib:last_modified(F) of 0 -> 0; M -> calendar:datetime_to_gregorian_seconds(M) end || F <- Files ].