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
|
%% -*- tab-width: 4;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_deps).
-include("rebar.hrl").
-export([preprocess/2,
compile/2,
'check-deps'/2,
'get-deps'/2,
'delete-deps'/2]).
%% ===================================================================
%% Public API
%% ===================================================================
preprocess(Config, _) ->
DepsDir = get_deps_dir(Config),
Config2 = rebar_config:set(Config, deps_dir, DepsDir),
%% Check for available deps, using the list of deps specified in our config.
%% We use the directory from the list of tuples for deps with source information to
%% update our list of directories to process.
case catch(check_deps(rebar_config:get_local(Config, deps, []), [], DepsDir)) of
Deps when is_list(Deps) ->
%% Walk all the deps and make sure they are available on the code path,
%% if the application we're interested in actually exists there.
ok = update_deps_code_path(Deps),
{ok, Config2, [Dir || {Dir, _, _, _} <- Deps]};
{'EXIT', Reason} ->
?ABORT("Error while processing dependencies: ~p\n", [Reason])
end.
compile(Config, AppFile) ->
'check-deps'(Config, AppFile).
'check-deps'(Config, _) ->
%% Get a list of deps that need to be downloaded and display them only
DepsDir = get_deps_dir(Config),
case catch(check_deps(rebar_config:get_local(Config, deps, []), [], DepsDir)) of
[] ->
ok;
Deps when is_list(Deps) ->
[?CONSOLE("Dependency not available: ~p-~p (~p)\n", [App, VsnRegex, Source]) ||
{_Dir, App, VsnRegex, Source} <- Deps],
?FAIL;
{'EXIT', Reason} ->
?ABORT("Error while processing dependencies: ~p\n", [Reason])
end.
'get-deps'(Config, _) ->
DepsDir = get_deps_dir(Config),
%% Get a list of deps that need to be downloaded
case catch(check_deps(rebar_config:get_local(Config, deps, []), [], DepsDir)) of
Deps when is_list(Deps) ->
%% Now for each dependency tuple, pull it
[use_source(Dir, App, VsnRegex, Source) || {Dir, App, VsnRegex, Source} <- Deps],
ok;
{'EXIT', Reason} ->
?ABORT("Error while processing dependencies: ~p\n", [Reason])
end.
'delete-deps'(Config, _) ->
%% Delete all the deps which we downloaded (or would have caused to be
%% downloaded).
DepsDir = rebar_config:get(Config, deps_dir, rebar_utils:get_cwd()),
?DEBUG("Delete deps: ~p\n", [rebar_config:get(Config, deps, [])]),
delete_deps(rebar_config:get_local(Config, deps, []), DepsDir).
%% ===================================================================
%% Internal functions
%% ===================================================================
get_deps_dir(Config) ->
%% Get the directory where we will place downloaded deps. Take steps
%% to ensure that if we're doing a multi-level build, all the deps will
%% wind up in a single directory; avoiding potential pain from having
%% multiple copies of the same dep scattered throughout the source tree.
%%
%% The first definition of deps_dir is the one we use; we also fully
%% qualify it to ensure everyone sees it properly.
case rebar_config:get_all(Config, deps_dir) of
[] ->
DepsDir = filename:absname("deps");
AllDirs ->
DepsDir = filename:absname(hd(lists:reverse(AllDirs)))
end,
?DEBUG("~s: Using deps dir: ~s\n", [rebar_utils:get_cwd(), DepsDir]),
DepsDir.
update_deps_code_path([]) ->
ok;
update_deps_code_path([{AppDir, App, VsnRegex, _Source} | Rest]) ->
case is_app_available(App, VsnRegex, AppDir) of
true ->
code:add_patha(filename:join(AppDir, ebin));
false ->
ok
end,
update_deps_code_path(Rest).
check_deps([], Acc, _Dir) ->
Acc;
check_deps([App | Rest], Acc, Dir) when is_atom(App) ->
require_app(App, ".*"),
check_deps(Rest, Acc, Dir);
check_deps([{App, VsnRegex} | Rest], Acc, Dir) when is_atom(App) ->
require_app(App, VsnRegex),
check_deps(Rest, Acc, Dir);
check_deps([{App, VsnRegex, Source} | Rest], Acc, Dir) ->
case is_app_available(App, VsnRegex) of
true ->
check_deps(Rest, Acc, Dir);
false ->
%% App is not on our code path OR the version that is available
%% doesn't match our regex. Return a tuple containing the target dir
%% and source information.
AppDir = filename:join(Dir, App),
check_deps(Rest, [{AppDir, App, VsnRegex, Source} | Acc], Dir)
end;
check_deps([Other | _Rest], _Acc, _Dir) ->
?ABORT("Invalid dependency specification ~p in ~s\n",
[Other, rebar_utils:get_cwd()]).
delete_deps([], _DepsDir) ->
ok;
delete_deps([{App, _VsnRegex, _Source} | Rest], DepsDir) ->
AppDir = filename:join(DepsDir, App),
case filelib:is_dir(AppDir) of
true ->
?INFO("Delete dependency dir ~s\n", [AppDir]),
rebar_file_utils:rm_rf(AppDir);
false ->
ok
end,
delete_deps(Rest, DepsDir);
delete_deps([_Other | Rest], DepsDir) ->
delete_deps(Rest, DepsDir).
require_app(App, VsnRegex) ->
case is_app_available(App, VsnRegex) of
true ->
ok;
false ->
%% The requested app is not available on the code path
?ABORT("~s: Dependency ~s-~s not available.\n",
[rebar_utils:get_cwd(), App, VsnRegex])
end.
require_source_engine(Source) ->
case source_engine_avail(Source) of
true ->
ok;
false ->
?ABORT("No command line interface available to process ~p\n", [Source])
end.
is_app_available(App, VsnRegex) ->
case code:lib_dir(App) of
{error, bad_name} ->
false;
Path ->
is_app_available(App, VsnRegex, Path)
end.
is_app_available(App, VsnRegex, Path) ->
case rebar_app_utils:is_app_dir(Path) of
{true, AppFile} ->
case rebar_app_utils:load_app_file(AppFile) of
{ok, App, AppData} ->
{vsn, Vsn} = lists:keyfind(vsn, 1, AppData),
?INFO("Looking for ~s-~s ; found ~s-~s at ~s\n",
[App, VsnRegex, App, Vsn, Path]),
case re:run(Vsn, VsnRegex, [{capture, none}]) of
match ->
true;
nomatch ->
?WARN("~s has version ~p; requested regex was ~s\n",
[AppFile, Vsn, VsnRegex]),
false
end;
{ok, OtherApp, _} ->
?WARN("~s has application id ~p; expected ~p\n", [AppFile, OtherApp, App]),
false;
{error, Reason} ->
?ABORT("Failed to parse ~s: ~p\n", [AppFile, Reason])
end;
false ->
?WARN("Expected ~s to be an app dir (containing ebin/*.app), but no .app found.\n",
[Path]),
false
end.
use_source(AppDir, App, VsnRegex, Source) ->
?CONSOLE("Pulling ~p from ~p\n", [App, Source]),
use_source(AppDir, App, VsnRegex, Source, 3).
use_source(_AppDir, _App, _VsnRegex, Source, 0) ->
?ABORT("Failed to acquire source from ~p after 3 tries.\n", [Source]);
use_source(AppDir, App, VsnRegex, Source, Count) ->
case filelib:is_dir(AppDir) of
true ->
%% Already downloaded -- verify the versioning matches up with our regex
case is_app_available(App, VsnRegex, AppDir) of
true ->
%% Available version matches up -- we're good to go; add the
%% app dir to our code path
code:add_patha(filename:join(AppDir, ebin)),
ok;
false ->
%% The app that was downloaded doesn't match up (or had
%% errors or something). For the time being, abort.
?ABORT("Dependency dir ~s does not satisfy version regex ~s.\n",
[AppDir, VsnRegex])
end;
false ->
require_source_engine(Source),
download_source(AppDir, Source),
use_source(AppDir, App, VsnRegex, Source, Count-1)
end.
download_source(AppDir, {hg, Url, Rev}) ->
ok = filelib:ensure_dir(AppDir),
Cmd = ?FMT("hg clone -u ~s ~s", [Rev, Url]),
rebar_utils:sh(Cmd, [], filename:dirname(AppDir));
download_source(AppDir, {git, Url, Rev}) ->
ok = filelib:ensure_dir(AppDir),
rebar_utils:sh(?FMT("git clone -n ~s", [Url]), [], filename:dirname(AppDir)),
rebar_utils:sh(?FMT("git checkout ~s", [Rev]), [], AppDir);
download_source(AppDir, {bzr, Url, Rev}) ->
ok = filelib:ensure_dir(AppDir),
rebar_utils:sh(?FMT("bzr branch -r ~s ~s ~s",
[Rev, Url, filename:basename(AppDir)]), [],
filename:dirname(AppDir));
download_source(AppDir, {svn, Url, Rev}) ->
ok = filelib:ensure_dir(AppDir),
rebar_utils:sh(?FMT("svn checkout -r ~s ~s ~s",
[Rev, Url, filename:basename(AppDir)]), [],
filename:dirname(AppDir)).
%% ===================================================================
%% Source helper functions
%% ===================================================================
source_engine_avail({Name, _, _})
when Name == hg; Name == git; Name == svn; Name == bzr ->
case scm_client_vsn(Name) >= required_scm_client_vsn(Name) of
true ->
true;
false ->
?ABORT("Rebar requires version ~p or higher of ~s\n",
[required_scm_client_vsn(Name), Name])
end.
scm_client_vsn(false, _VsnArg, _VsnRegex) ->
false;
scm_client_vsn(Path, VsnArg, VsnRegex) ->
Info = os:cmd(Path ++ VsnArg),
case re:run(Info, VsnRegex, [{capture, all_but_first, list}]) of
{match, Match} ->
list_to_tuple([list_to_integer(S) || S <- Match]);
_ ->
false
end.
required_scm_client_vsn(hg) -> {1, 4};
required_scm_client_vsn(git) -> {1, 6};
required_scm_client_vsn(bzr) -> {2, 0};
required_scm_client_vsn(svn) -> {1, 6}.
scm_client_vsn(hg) ->
scm_client_vsn(os:find_executable(hg), " --version", "version (\\d+).(\\d+)");
scm_client_vsn(git) ->
scm_client_vsn(os:find_executable(git), " --version", "git version (\\d+).(\\d+)");
scm_client_vsn(bzr) ->
scm_client_vsn(os:find_executable(bzr), " --version", "Bazaar \\(bzr\\) (\\d+).(\\d+)");
scm_client_vsn(svn) ->
scm_client_vsn(os:find_executable(svn), " --version", "svn, version (\\d+).(\\d+)");
scm_client_vsn(_) ->
undefined.
|