summaryrefslogtreecommitdiff
path: root/src/rebar_app_discover.erl
blob: 19be6102f7b48d73459c1cd9b6e7482fbc8d2fbf (plain)
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
-module(rebar_app_discover).

-export([do/2,
         find_unbuilt_apps/1,
         find_apps/1,
         find_apps/2,
         find_app/2,
         validate_application_info/1]).

do(State, LibDirs) ->
    BaseDir = rebar_state:dir(State),
    Dirs = [filename:join(BaseDir, LibDir) || LibDir <- LibDirs],
    Apps = find_apps(Dirs, all),
    ProjectDeps = rebar_state:deps_names(State),
    lists:foldl(fun(AppInfo, StateAcc) ->
                        rebar_state:project_apps(StateAcc, rebar_app_info:deps(AppInfo, ProjectDeps))
            end, State, Apps).

-spec all_app_dirs(list(file:name())) -> list(file:name()).
all_app_dirs(LibDirs) ->
    lists:flatmap(fun(LibDir) ->
                          app_dirs(LibDir)
                  end, LibDirs).

app_dirs(LibDir) ->
    Path1 = filename:join([LibDir,
                           "*",
                           "src",
                           "*.app.src"]),
    Path2 = filename:join([LibDir,
                           "src",
                           "*.app.src"]),

    Path3 = filename:join([LibDir,
                           "*",
                           "ebin",
                           "*.app"]),
    Path4 = filename:join([LibDir,
                           "ebin",
                           "*.app"]),

    lists:usort(lists:foldl(fun(Path, Acc) ->
                                    Files = filelib:wildcard(ec_cnv:to_list(Path)),
                                    [app_dir(File) || File <- Files] ++ Acc
                            end, [], [Path1, Path2, Path3, Path4])).

find_unbuilt_apps(LibDirs) ->
    find_apps(LibDirs, invalid).

find_apps(LibDirs) ->
    find_apps(LibDirs, valid).

find_apps(LibDirs, Validate) ->
    lists:filtermap(fun(AppDir) ->
                            find_app(AppDir, Validate)
                    end, all_app_dirs(LibDirs)).

find_app(AppDir, Validate) ->
    AppFile = filelib:wildcard(filename:join([AppDir, "ebin", "*.app"])),
    AppSrcFile = filelib:wildcard(filename:join([AppDir, "src", "*.app.src"])),
    case AppFile of
        [File] ->
            AppInfo = create_app_info(AppDir, File),
            AppInfo1 = rebar_app_info:app_file(AppInfo, File),
            AppInfo2 = case AppSrcFile of
                           [F] ->
                               rebar_app_info:app_file_src(AppInfo1, F);
                           [] ->
                               AppInfo1
                       end,
            case Validate of
                valid ->
                    case validate_application_info(AppInfo2) of
                        true ->
                            {true, AppInfo2};
                        false ->
                            false
                    end;
                invalid ->
                    case validate_application_info(AppInfo2) of
                        false ->
                            {true, AppInfo2};
                        true ->
                            false
                    end;
                all ->
                    {true, AppInfo2}
            end;
        [] ->
            case AppSrcFile of
                [File] ->
                    case Validate of
                        V when V =:= invalid ; V =:= all ->
                            AppInfo = create_app_info(AppDir, File),
                            {true, rebar_app_info:app_file_src(AppInfo, File)};
                        valid ->
                            false
                    end;
                [] ->
                    false
            end
    end.

app_dir(AppFile) ->
    filename:join(lists:droplast(filename:split(filename:dirname(AppFile)))).

create_app_info(AppDir, AppFile) ->
    case file:consult(AppFile) of
        {ok, [{application, AppName, AppDetails}]} ->
            AppVsn = proplists:get_value(vsn, AppDetails),
            AbsCwd = filename:absname(rebar_utils:get_cwd()),
            {ok, AppInfo} = rebar_app_info:new(AppName, AppVsn, AppDir),
            RebarConfig = filename:join(AppDir, "rebar.config"),
            AppState = case filelib:is_file(RebarConfig) of
                            true ->
                                Terms = rebar_config:consult_file(RebarConfig),
                                rebar_state:new(Terms);
                            false ->
                                rebar_state:new()
                        end,
            AppState1 = rebar_state:set(AppState, base_dir, AbsCwd),
            AppInfo1 = rebar_app_info:config(
                         rebar_app_info:app_details(AppInfo, AppDetails), AppState1),
            rebar_app_info:dir(AppInfo1, AppDir)
    end.

-spec validate_application_info(rebar_app_info:t()) -> boolean().
validate_application_info(AppInfo) ->
    EbinDir = rebar_app_info:ebin_dir(AppInfo),
    AppFile = rebar_app_info:app_file(AppInfo),
    AppDetail = rebar_app_info:app_details(AppInfo),
    case get_modules_list(AppFile, AppDetail) of
        {ok, List} ->
            has_all_beams(EbinDir, List);
        _Error ->
            false
    end.

-spec get_modules_list(file:name(), proplists:proplist()) ->
                              {ok, list()} |
                              {warning, Reason::term()} |
                              {error, Reason::term()}.
get_modules_list(AppFile, AppDetail) ->
    case proplists:get_value(modules, AppDetail) of
        undefined ->
            {warning, {invalid_app_file, AppFile}};
        ModulesList ->
            {ok, ModulesList}
    end.

-spec has_all_beams(file:name(), list()) ->
                           ok | {error, Reason::term()}.
has_all_beams(EbinDir, [Module | ModuleList]) ->
    BeamFile = filename:join([EbinDir,
                              ec_cnv:to_list(Module) ++ ".beam"]),
    case filelib:is_file(BeamFile) of
        true ->
            has_all_beams(EbinDir, ModuleList);
        false ->
            false
    end;
has_all_beams(_, []) ->
    true.