/
erldocs
executable file
·56 lines (48 loc) · 1.57 KB
/
erldocs
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
#!/usr/bin/env escript
%% -*- erlang -*-
%% @doc Called automatically by escript
-spec main(list()) -> ok.
main(Args) ->
ok = file:set_cwd(filename:dirname(escript:script_name())),
ok = filelib:ensure_dir("ebin/"),
code:add_path("ebin"),
{ok, _} = compile:file("src/erldocs.erl", [{outdir, "ebin"}, verbose]),
Conf = create_conf(Args),
try erldocs:dispatch(Conf)
catch Type:Error ->
log("Error running script:~n~p~n~p~n",
[erlang:get_stacktrace(), {Type, Error}])
end.
-spec parse_options([string()]) -> [{atom(), _}].
parse_options(Args) ->
lists:flatmap(fun (Arg) -> parse_option(Arg) end, Args).
-spec parse_option(string()) -> [{atom(), _}].
parse_option("--sys-conf=" ++ Path) ->
[{sys_conf, Path}];
parse_option("--gen-docsrc") ->
[{gen_docsrc, true}];
parse_option("copystatic") ->
[{copystatic, true}];
parse_option("--name=" ++ Name) ->
[{name, Name}];
parse_option(Unknown) ->
throw({unknown_option, Unknown}).
-spec getopt(atom(), [{atom(), _}], _) -> _.
getopt(Key, Opts, Default) ->
case lists:keyfind(Key, 1, Opts) of
false ->
Default;
{_, V} ->
V
end.
-spec create_conf([string()]) -> [{atom(), _}].
create_conf(Args) ->
Opts = parse_options(Args),
Path = getopt(sys_conf, Opts, "priv/sys.conf"),
{ok, [Config]} = file:consult(Path),
{erldocs, Conf} = lists:keyfind(erldocs, 1, Config),
{ok, Root} = file:get_cwd(),
Opts ++ [{root, Root} | Conf].
-spec log(string(), [_]) -> ok.
log(Str, Args) ->
io:format(Str, Args).