summaryrefslogtreecommitdiff
path: root/src/rebar_prv_install_deps.erl
blob: 8b56ac31264528e5f7fa5156f92a04dcb01683e6 (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
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
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 et
%% -------------------------------------------------------------------
%%
%% rebar: Erlang Build Tools
%%
%% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
%%
%% Permission is hereby granted, free of charge, to any person obtaining a copy
%% of this software and associated documentation files (the "Software"), to deal
%% in the Software without restriction, including without limitation the rights
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%% copies of the Software, and to permit persons to whom the Software is
%% furnished to do so, subject to the following conditions:
%%
%% The above copyright notice and this permission notice shall be included in
%% all copies or substantial portions of the Software.
%%
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%% THE SOFTWARE.
%% -------------------------------------------------------------------
-module(rebar_prv_install_deps).

-behaviour(provider).

-export([init/1,
         do/1]).

-include("rebar.hrl").

-export([handle_deps/2,
         handle_deps/3]).

%% for internal use only
-export([get_deps_dir/1]).
-export([get_deps_dir/2]).

-define(PROVIDER, install_deps).
-define(DEPS, [app_discovery]).

-type src_dep() :: {atom(), string(), {atom(), string(), string()}}.
-type pkg_dep() :: {atom(), binary()} | atom().

-type dep() :: src_dep() | pkg_dep().

%% ===================================================================
%% Public API
%% ===================================================================

-spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
init(State) ->
    State1 = rebar_state:add_provider(State, providers:create([{name, ?PROVIDER},
                                                               {module, ?MODULE},
                                                               {bare, true},
                                                               {deps, ?DEPS},
                                                               {example, undefined},
                                                               {short_desc, "Install dependencies"},
                                                               {desc, info("Install dependencies")},
                                                               {opts, []}])),
    {ok, State1}.

-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
do(State) ->
    ProjectApps = rebar_state:project_apps(State),
    {ok, State1} = case rebar_state:get(State, locks, []) of
                       [] ->
                           handle_deps(State, rebar_state:get(State, deps, []));
                       Locks ->
                           handle_deps(State, Locks)
                   end,

    Source = ProjectApps ++ rebar_state:src_apps(State1),
    {ok, Sort} = rebar_topo:sort_apps(Source),
    {ok, rebar_state:set(State1, deps_to_build, lists:dropwhile(fun is_valid/1, Sort -- ProjectApps))}.

-spec get_deps_dir(rebar_state:t()) -> file:filename_all().
get_deps_dir(State) ->
    BaseDir = rebar_state:get(State, base_dir, ""),
    DepsDir = rebar_state:get(State, deps_dir, ?DEFAULT_DEPS_DIR),
    get_deps_dir(BaseDir, DepsDir).

-spec get_deps_dir(file:filename_all(), file:filename_all()) -> file:filename_all().
get_deps_dir(DepsDir, App) ->
    filename:join(DepsDir, App).

-spec handle_deps(rebar_state:t(), [dep()]) -> {ok, rebar_state:t()}.
handle_deps(State, Deps) ->
    handle_deps(State, Deps, false).

-spec handle_deps(rebar_state:t(), [dep()], false | {true, integer()}) -> {ok, rebar_state:t()}.
handle_deps(State, [], _) ->
    {ok, State};
handle_deps(State, Deps, Update) ->
    %% Read in package index and dep graph
    {Packages, Graph} = rebar_packages:get_packages(State),

    %% Split source deps from pkg deps, needed to keep backwards compatibility
    DepsDir = get_deps_dir(State),
    {SrcDeps, PkgDeps} = parse_deps(DepsDir, Deps),
    State1 = rebar_state:src_deps(rebar_state:pkg_deps(State, PkgDeps),
                                  SrcDeps),

    %% Fetch transitive src deps
    State2 = update_src_deps(0, State1, Update),
    Solved = case rebar_state:pkg_deps(State2) of
                 [] -> %% No pkg deps
                     [];
                 PkgDeps1 ->
                     %% Find pkg deps needed
                     {ok, S} = rlx_depsolver:solve(Graph, PkgDeps1),
                     %% Create app_info record for each pkg dep
                     lists:map(fun(Pkg) ->
                                       AppInfo = package_to_app(DepsDir
                                                               ,Packages
                                                               ,Pkg),
                                       maybe_fetch(AppInfo, Update),
                                       AppInfo
                               end, S)
             end,

    AllDeps = lists:ukeymerge(2
                            ,lists:ukeysort(2, rebar_state:src_apps(State2))
                            ,lists:ukeysort(2, Solved)),

    %% Sort all apps to build order
    State3 = rebar_state:set(State2, all_deps, AllDeps),
    {ok, State3}.

%% ===================================================================
%% Internal functions
%% ===================================================================

-spec is_valid(rebar_app_info:t()) -> boolean().
is_valid(App) ->
    rebar_app_info:valid(App).

-spec package_to_app(file:filename_all(), rebar_dict(),
                    rlx_depsolver:pkg()) -> rebar_app_info:t().
package_to_app(DepsDir, Packages, Pkg={_, Vsn}) ->
    Name = ec_cnv:to_binary(rlx_depsolver:dep_pkg(Pkg)),
    FmtVsn = iolist_to_binary(rlx_depsolver:format_version(Vsn)),
    {ok, P} = dict:find({Name, FmtVsn}, Packages),
    PkgDeps = proplists:get_value(<<"deps">>, P),
    Link = proplists:get_value(<<"link">>, P),

    {ok, AppInfo} = rebar_app_info:new(Name, FmtVsn),
    AppInfo1 = rebar_app_info:deps(AppInfo, PkgDeps),
    AppInfo2 =
        rebar_app_info:dir(AppInfo1, get_deps_dir(DepsDir, Name)),
    rebar_app_info:source(AppInfo2, Link).

-spec update_src_deps(integer(), rebar_state:t(), boolean()) -> rebar_state:t().
update_src_deps(Level, State, Update) ->
    SrcDeps = rebar_state:src_deps(State),
    DepsDir = get_deps_dir(State),
    case lists:foldl(fun(AppInfo, {SrcDepsAcc, PkgDepsAcc, StateAcc}) ->
                             Name = rebar_app_info:name(AppInfo),
                             Locks = rebar_state:get(State, locks, []),
                             case Update of
                                 {true, UpdateName, UpdateLevel} ->
                                     {_, _, _, DepLevel} = lists:keyfind(Name, 1, Locks),
                                     case UpdateLevel < DepLevel
                                         orelse Name =:= UpdateName of
                                         true ->
                                             case maybe_fetch(AppInfo, true) of
                                                 true ->
                                                     {AppInfo1, NewSrcDeps, NewPkgDeps} =
                                                         handle_dep(DepsDir, AppInfo),
                                                     AppInfo2 = rebar_app_info:dep_level(AppInfo1, Level),
                                                     {NewSrcDeps ++ SrcDepsAcc
                                                     ,NewPkgDeps++PkgDepsAcc
                                                     ,rebar_state:src_apps(StateAcc, AppInfo2)};
                                                 false ->
                                                     {SrcDepsAcc, PkgDepsAcc, State}
                                             end;
                                         false ->
                                             {SrcDepsAcc, PkgDepsAcc, State}
                                     end;
                                 _ ->
                                     case maybe_fetch(AppInfo, false) of
                                         true ->
                                             {AppInfo1, NewSrcDeps, NewPkgDeps} =
                                                 handle_dep(DepsDir, AppInfo),
                                             AppInfo2 = rebar_app_info:dep_level(AppInfo1, Level),
                                             {NewSrcDeps ++ SrcDepsAcc
                                             ,NewPkgDeps++PkgDepsAcc
                                             ,rebar_state:src_apps(StateAcc, AppInfo2)};
                                         false ->
                                             {AppInfo1, NewSrcDeps, NewPkgDeps} =
                                                 handle_dep(DepsDir, AppInfo),
                                             AppInfo2 = rebar_app_info:dep_level(AppInfo1, Level),
                                             {NewSrcDeps ++ SrcDepsAcc
                                             ,NewPkgDeps++PkgDepsAcc
                                             ,rebar_state:src_apps(StateAcc, AppInfo2)}
                                     end
                             end
                     end, {[], rebar_state:pkg_deps(State), State}, SrcDeps) of
        {[], NewPkgDeps, State1} ->
            rebar_state:pkg_deps(State1, NewPkgDeps);
        {NewSrcDeps, NewPkgDeps, State1} ->
            State2 = rebar_state:pkg_deps(State1, NewPkgDeps),
            State3 = rebar_state:src_deps(State2, NewSrcDeps),
            update_src_deps(Level+1, State3, Update)
    end.

-spec handle_dep(file:filename_all(), rebar_app_info:t()) ->
                        {rebar_app_info:t(), [rebar_app_info:t()], [pkg_dep()]}.
handle_dep(DepsDir, AppInfo) ->
    C = rebar_config:consult(rebar_app_info:dir(AppInfo)),
    S = rebar_state:new(rebar_state:new(), C, rebar_app_info:dir(AppInfo)),
    Deps = rebar_state:get(S, deps, []),
    AppInfo1 = rebar_app_info:deps(AppInfo, rebar_state:deps_names(S)),
    {SrcDeps, PkgDeps} = parse_deps(DepsDir, Deps),
    {AppInfo1, SrcDeps, PkgDeps}.

-spec maybe_fetch(rebar_app_info:t(), boolean()) -> boolean().
maybe_fetch(AppInfo, Update) ->
    AppDir = ec_cnv:to_list(rebar_app_info:dir(AppInfo)),
    %Apps = rebar_app_discover:find_apps([get_deps_dir(State)], all),
    %case rebar_app_utils:find(rebar_app_info:name(AppInfo), Apps) of
    Exists = case rebar_app_utils:is_app_dir(filename:absname(AppDir)++"-*") of
                 {true, _} ->
                     true;
                 _ ->
                     case rebar_app_utils:is_app_dir(filename:absname(AppDir)) of
                         {true, _} ->
                             true;
                         _ ->
                             false
                     end
             end,

    case not Exists orelse Update of
        true ->
            ?INFO("Fetching ~s~n", [rebar_app_info:name(AppInfo)]),
            Source = rebar_app_info:source(AppInfo),
            rebar_fetch:download_source(AppDir, Source),
            true;
        _ ->
            false
    end.

-spec parse_deps(binary(), [dep()]) -> {[rebar_app_info:t()], [pkg_dep()]}.
parse_deps(DepsDir, Deps) ->
    lists:foldl(fun({Name, Vsn}, {SrcDepsAcc, PkgDepsAcc}) ->
                        {SrcDepsAcc, [parse_goal(ec_cnv:to_binary(Name)
                                                ,ec_cnv:to_binary(Vsn)) | PkgDepsAcc]};
                   (Name, {SrcDepsAcc, PkgDepsAcc}) when is_atom(Name) ->
                        {SrcDepsAcc, [ec_cnv:to_binary(Name) | PkgDepsAcc]};
                   ({Name, Vsn, Source}, {SrcDepsAcc, PkgDepsAcc}) when is_tuple (Source) ->
                        Dir = ec_cnv:to_list(get_deps_dir(DepsDir, Name)),
                        {ok, Dep} = case rebar_app_info:discover(Dir) of
                                        {ok, App} ->
                                            {ok, App};
                                        not_found ->
                                            rebar_app_info:new(Name, Vsn, Dir)
                                    end,
                        Dep1 = rebar_app_info:source(Dep, Source),
                        {[Dep1 | SrcDepsAcc], PkgDepsAcc};
                   ({Name, Vsn, Source, _Level}, {SrcDepsAcc, PkgDepsAcc}) when is_tuple (Source) ->
                        Dir = ec_cnv:to_list(get_deps_dir(DepsDir, Name)),
                        {ok, Dep} = case rebar_app_info:discover(Dir) of
                                        {ok, App} ->
                                            {ok, App};
                                        not_found ->
                                            rebar_app_info:new(Name, Vsn, Dir)
                                    end,
                        Dep1 = rebar_app_info:source(Dep, Source),
                        {[Dep1 | SrcDepsAcc], PkgDepsAcc}
                end, {[], []}, Deps).

-spec parse_goal(binary(), binary()) -> pkg_dep().
parse_goal(Name, Constraint) ->
    case re:run(Constraint, "([^\\d]*)(\\d.*)", [{capture, [1,2], binary}]) of
        {match, [<<>>, Vsn]} ->
            {Name, Vsn};
        {match, [Op, Vsn]} ->
            {Name, Vsn, binary_to_atom(Op, utf8)};
        nomatch ->
            fail
    end.

info(Description) ->
    io_lib:format("~s.~n"
                 "~n"
                 "Valid rebar.config options:~n"
                 "  ~p~n"
                 "  ~p~n"
                 "Valid command line options:~n"
                 "  deps_dir=\"deps\" (override default or rebar.config deps_dir)~n",
                 [
                 Description,
                 {deps_dir, "deps"},
                 {deps,
                  [app_name,
                   {rebar, "1.0.*"},
                   {rebar, ".*",
                    {git, "git://github.com/rebar/rebar.git"}},
                   {rebar, ".*",
                    {git, "git://github.com/rebar/rebar.git", "Rev"}},
                   {rebar, "1.0.*",
                    {git, "git://github.com/rebar/rebar.git", {branch, "master"}}},
                   {rebar, "1.0.0",
                    {git, "git://github.com/rebar/rebar.git", {tag, "1.0.0"}}},
                   {rebar, "",
                    {git, "git://github.com/rebar/rebar.git", {branch, "master"}},
                    [raw]},
                   {app_name, ".*", {hg, "https://www.example.org/url"}},
                   {app_name, ".*", {rsync, "Url"}},
                   {app_name, ".*", {svn, "https://www.example.org/url"}},
                   {app_name, ".*", {svn, "svn://svn.example.org/url"}},
                   {app_name, ".*", {bzr, "https://www.example.org/url", "Rev"}},
                   {app_name, ".*", {fossil, "https://www.example.org/url"}},
                   {app_name, ".*", {fossil, "https://www.example.org/url", "Vsn"}},
                   {app_name, ".*", {p4, "//depot/subdir/app_dir"}}]}
                 ]).