blob: f444a5943a25b6b04792fab8fdae828ef43e4ed4 [file] [log] [blame]
Tuncer Ayazc4660762011-01-31 17:43:31 +01001%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
Tuncer Ayaz9a8015f2009-12-31 19:42:53 +01002%% ex: ts=4 sw=4 et
Dave Smith2f64f0e2009-12-25 23:19:09 -07003%% -------------------------------------------------------------------
4%%
5%% rebar: Erlang Build Tools
6%%
7%% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
8%%
9%% Permission is hereby granted, free of charge, to any person obtaining a copy
10%% of this software and associated documentation files (the "Software"), to deal
11%% in the Software without restriction, including without limitation the rights
12%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13%% copies of the Software, and to permit persons to whom the Software is
14%% furnished to do so, subject to the following conditions:
15%%
16%% The above copyright notice and this permission notice shall be included in
17%% all copies or substantial portions of the Software.
18%%
19%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25%% THE SOFTWARE.
26%% -------------------------------------------------------------------
27-module(rebar_subdirs).
28
29-include("rebar.hrl").
Tuncer Ayaze0a86862011-06-16 17:38:48 +020030-include_lib("kernel/include/file.hrl").
Dave Smith2f64f0e2009-12-25 23:19:09 -070031
32-export([preprocess/2]).
33
34%% ===================================================================
35%% Public API
36%% ===================================================================
37
38preprocess(Config, _) ->
39 %% Get the list of subdirs specified in the config (if any).
40 Cwd = rebar_utils:get_cwd(),
Юрин Вячеслав0424d752012-02-03 21:06:18 +070041 ListSubdirs = rebar_config:get_local(Config, sub_dirs, []),
42 Subdirs0 = lists:flatmap(fun filelib:wildcard/1, ListSubdirs),
Tuncer Ayaze185e862012-04-22 21:53:32 +020043 case {rebar_config:is_skip_dir(Config, Cwd), Subdirs0} of
Tuncer Ayaz06899282012-01-13 18:20:05 +010044 {true, []} ->
Tuncer Ayaz45555eb2012-01-09 19:34:27 +010045 {ok, []};
Tuncer Ayaz06899282012-01-13 18:20:05 +010046 {true, _} ->
47 ?WARN("Ignoring sub_dirs for ~s~n", [Cwd]),
48 {ok, []};
49 {false, _} ->
Tuncer Ayaz45555eb2012-01-09 19:34:27 +010050 Check = check_loop(Cwd),
51 ok = lists:foreach(Check, Subdirs0),
52 Subdirs = [filename:join(Cwd, Dir) || Dir <- Subdirs0],
53 {ok, Subdirs}
54 end.
Tuncer Ayaze0a86862011-06-16 17:38:48 +020055
56%% ===================================================================
57%% Internal functions
58%% ===================================================================
59
60check_loop(Cwd) ->
61 RebarConfig = filename:join(Cwd, "rebar.config"),
62 fun(Dir0) ->
63 IsSymlink = case file:read_link_info(Dir0) of
64 {ok, #file_info{type=symlink}} ->
65 {true, resolve_symlink(Dir0)};
66 _ ->
67 {false, Dir0}
68 end,
69 case IsSymlink of
70 {false, Dir="."} ->
Tuncer Ayazcc2447f2011-07-07 15:29:53 +020071 ?ERROR("infinite loop detected:~nsub_dirs"
Tuncer Ayaze0a86862011-06-16 17:38:48 +020072 " entry ~p in ~s~n", [Dir, RebarConfig]);
73 {true, Cwd} ->
Tuncer Ayazcc2447f2011-07-07 15:29:53 +020074 ?ERROR("infinite loop detected:~nsub_dirs"
Tuncer Ayaze0a86862011-06-16 17:38:48 +020075 " entry ~p in ~s is a symlink to \".\"~n",
76 [Dir0, RebarConfig]);
77 _ ->
78 ok
79 end
80 end.
81
82resolve_symlink(Dir0) ->
83 {ok, Dir} = file:read_link(Dir0),
84 Dir.