You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.3 KiB

  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  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. -include("rebar.hrl").
  29. -include_lib("kernel/include/file.hrl").
  30. -export([preprocess/2]).
  31. %% ===================================================================
  32. %% Public API
  33. %% ===================================================================
  34. preprocess(Config, _) ->
  35. %% Get the list of subdirs specified in the config (if any).
  36. Cwd = rebar_utils:get_cwd(),
  37. ListSubdirs = rebar_config:get_local(Config, sub_dirs, []),
  38. Subdirs0 = lists:flatmap(fun filelib:wildcard/1, ListSubdirs),
  39. case {rebar_config:is_skip_dir(Config, Cwd), Subdirs0} of
  40. {true, []} ->
  41. {ok, []};
  42. {true, _} ->
  43. ?WARN("Ignoring sub_dirs for ~s~n", [Cwd]),
  44. {ok, []};
  45. {false, _} ->
  46. Check = check_loop(Cwd),
  47. ok = lists:foreach(Check, Subdirs0),
  48. Subdirs = [filename:join(Cwd, Dir) || Dir <- Subdirs0],
  49. {ok, Subdirs}
  50. end.
  51. %% ===================================================================
  52. %% Internal functions
  53. %% ===================================================================
  54. check_loop(Cwd) ->
  55. RebarConfig = filename:join(Cwd, "rebar.config"),
  56. fun(Dir0) ->
  57. IsSymlink = case file:read_link_info(Dir0) of
  58. {ok, #file_info{type=symlink}} ->
  59. {true, resolve_symlink(Dir0)};
  60. _ ->
  61. {false, Dir0}
  62. end,
  63. case IsSymlink of
  64. {false, Dir="."} ->
  65. ?ERROR("infinite loop detected:~nsub_dirs"
  66. " entry ~p in ~s~n", [Dir, RebarConfig]);
  67. {true, Cwd} ->
  68. ?ERROR("infinite loop detected:~nsub_dirs"
  69. " entry ~p in ~s is a symlink to \".\"~n",
  70. [Dir0, RebarConfig]);
  71. _ ->
  72. ok
  73. end
  74. end.
  75. resolve_symlink(Dir0) ->
  76. {ok, Dir} = file:read_link(Dir0),
  77. Dir.