File: app_deps.erl

package info (click to toggle)
rabbitmq-server 4.0.5-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,972 kB
  • sloc: erlang: 257,835; javascript: 22,466; sh: 3,037; makefile: 2,517; python: 1,966; xml: 646; cs: 335; java: 244; ruby: 212; php: 100; perl: 63; awk: 13
file content (71 lines) | stat: -rwxr-xr-x 2,425 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/env escript
%% -*- erlang -*-
%%% Run with 'escript app_deps.erl'
%%% Change the path in filelib:wildcard/1 as required to capture
%%% all your dependencies.
%%%
%%% Rectangular nodes will represent library apps (no processes
%%% involved) and the circular nodes will represent regular apps.
%%% An arrow going from 'A -> B' means 'A depends on B'.
%%%
%%% This script depends on graphviz being present on the system.
-module(app_deps).
-export([main/1]).

main(_) ->
    AppFiles = filelib:wildcard("deps/*/ebin/*.app")
               ++
               filelib:wildcard("apps/*/ebin/*.app")
               ++
               filelib:wildcard("ebin/*.app")
               ++
               filelib:wildcard("_build/default/lib/*/ebin/*.app"),
    to_graphviz(read_deps(AppFiles)).

read_deps(AppFiles) ->
    [{App,
      proplists:get_value(applications, Props, []),
      apptype(Props)}
     || {ok, [{_,App,Props}]} <-
        [file:consult(AppFile) || AppFile <- AppFiles]].

apptype(Props) ->
    case proplists:get_value(mod, Props) of
        undefined -> library;
        _ -> regular
    end.

to_graphviz(Deps) ->
    AllApps = lists:usort(lists:flatten(
        [[{App,Type},DepList] || {App,DepList,Type} <- Deps]
    )),
    Bytes = ["digraph G { ",
             "K=0.25; ratio=0.75; overlap=\"9:prism\"; ",
             [io_lib:format("~p [shape=box] ", [App])
              || App <- libapps(AllApps -- [kernel,stdlib])],
             [[io_lib:format("~p->~p ", [App,Dep])
               || Dep <- DepList -- [kernel, stdlib]]
              || {App, DepList, _} <- Deps],
            "}"],
    file:write_file("app-deps.dot", Bytes),
    os:cmd("dot app-deps.dot -Tpng -o app-deps.png").

libapps([]) -> [];
libapps([{App,library}|Apps]) -> [App|libapps(Apps)];
libapps([{_,_}|Apps]) -> libapps(Apps);
libapps([App|Apps]) ->
    Dir = case code:lib_dir(App) of
        {error, _} -> ""; % not an OTP app
        DirPath -> DirPath
    end,
    Path = filename:join([Dir, "ebin", atom_to_list(App)++".app"]),
    case lists:prefix(code:lib_dir(), Path) of
        false ->
            [App|libapps(Apps)]; % not OTP app, we don't care
        true -> % deps of OTP deps: we don't care either.
            {ok, [{_,App,Props}]} = file:consult(Path),
            case apptype(Props) of
                library -> [App | libapps(Apps)];
                regular -> libapps(Apps)
            end
    end.