Tuncer Ayaz | c466076 | 2011-01-31 17:43:31 +0100 | [diff] [blame] | 1 | %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*- |
Tuncer Ayaz | 9a8015f | 2009-12-31 19:42:53 +0100 | [diff] [blame] | 2 | %% ex: ts=4 sw=4 et |
Dave Smith | 2f64f0e | 2009-12-25 23:19:09 -0700 | [diff] [blame] | 3 | %% ------------------------------------------------------------------- |
| 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 Ayaz | e0a8686 | 2011-06-16 17:38:48 +0200 | [diff] [blame] | 30 | -include_lib("kernel/include/file.hrl"). |
Dave Smith | 2f64f0e | 2009-12-25 23:19:09 -0700 | [diff] [blame] | 31 | |
| 32 | -export([preprocess/2]). |
| 33 | |
| 34 | %% =================================================================== |
| 35 | %% Public API |
| 36 | %% =================================================================== |
| 37 | |
| 38 | preprocess(Config, _) -> |
| 39 | %% Get the list of subdirs specified in the config (if any). |
| 40 | Cwd = rebar_utils:get_cwd(), |
Юрин Вячеслав | 0424d75 | 2012-02-03 21:06:18 +0700 | [diff] [blame] | 41 | ListSubdirs = rebar_config:get_local(Config, sub_dirs, []), |
| 42 | Subdirs0 = lists:flatmap(fun filelib:wildcard/1, ListSubdirs), |
Tuncer Ayaz | e185e86 | 2012-04-22 21:53:32 +0200 | [diff] [blame] | 43 | case {rebar_config:is_skip_dir(Config, Cwd), Subdirs0} of |
Tuncer Ayaz | 0689928 | 2012-01-13 18:20:05 +0100 | [diff] [blame] | 44 | {true, []} -> |
Tuncer Ayaz | 45555eb | 2012-01-09 19:34:27 +0100 | [diff] [blame] | 45 | {ok, []}; |
Tuncer Ayaz | 0689928 | 2012-01-13 18:20:05 +0100 | [diff] [blame] | 46 | {true, _} -> |
| 47 | ?WARN("Ignoring sub_dirs for ~s~n", [Cwd]), |
| 48 | {ok, []}; |
| 49 | {false, _} -> |
Tuncer Ayaz | 45555eb | 2012-01-09 19:34:27 +0100 | [diff] [blame] | 50 | Check = check_loop(Cwd), |
| 51 | ok = lists:foreach(Check, Subdirs0), |
| 52 | Subdirs = [filename:join(Cwd, Dir) || Dir <- Subdirs0], |
| 53 | {ok, Subdirs} |
| 54 | end. |
Tuncer Ayaz | e0a8686 | 2011-06-16 17:38:48 +0200 | [diff] [blame] | 55 | |
| 56 | %% =================================================================== |
| 57 | %% Internal functions |
| 58 | %% =================================================================== |
| 59 | |
| 60 | check_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 Ayaz | cc2447f | 2011-07-07 15:29:53 +0200 | [diff] [blame] | 71 | ?ERROR("infinite loop detected:~nsub_dirs" |
Tuncer Ayaz | e0a8686 | 2011-06-16 17:38:48 +0200 | [diff] [blame] | 72 | " entry ~p in ~s~n", [Dir, RebarConfig]); |
| 73 | {true, Cwd} -> |
Tuncer Ayaz | cc2447f | 2011-07-07 15:29:53 +0200 | [diff] [blame] | 74 | ?ERROR("infinite loop detected:~nsub_dirs" |
Tuncer Ayaz | e0a8686 | 2011-06-16 17:38:48 +0200 | [diff] [blame] | 75 | " entry ~p in ~s is a symlink to \".\"~n", |
| 76 | [Dir0, RebarConfig]); |
| 77 | _ -> |
| 78 | ok |
| 79 | end |
| 80 | end. |
| 81 | |
| 82 | resolve_symlink(Dir0) -> |
| 83 | {ok, Dir} = file:read_link(Dir0), |
| 84 | Dir. |