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.

75 lines
3.0 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. Subdirs0 = rebar_config:get_local(Config, sub_dirs, []),
  38. Check = check_loop(Cwd),
  39. ok = lists:foreach(Check, Subdirs0),
  40. Subdirs = [filename:join(Cwd, Dir) || Dir <- Subdirs0],
  41. {ok, Subdirs}.
  42. %% ===================================================================
  43. %% Internal functions
  44. %% ===================================================================
  45. check_loop(Cwd) ->
  46. RebarConfig = filename:join(Cwd, "rebar.config"),
  47. fun(Dir0) ->
  48. IsSymlink = case file:read_link_info(Dir0) of
  49. {ok, #file_info{type=symlink}} ->
  50. {true, resolve_symlink(Dir0)};
  51. _ ->
  52. {false, Dir0}
  53. end,
  54. case IsSymlink of
  55. {false, Dir="."} ->
  56. ?ERROR("infinite loop detected:~nsub_dirs"
  57. " entry ~p in ~s~n", [Dir, RebarConfig]);
  58. {true, Cwd} ->
  59. ?ERROR("infinite loop detected:~nsub_dirs"
  60. " entry ~p in ~s is a symlink to \".\"~n",
  61. [Dir0, RebarConfig]);
  62. _ ->
  63. ok
  64. end
  65. end.
  66. resolve_symlink(Dir0) ->
  67. {ok, Dir} = file:read_link(Dir0),
  68. Dir.