PageRenderTime 148ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/priv/skel/src/skel_deps.erl

http://github.com/basho/mochiweb
Erlang | 92 lines | 47 code | 14 blank | 31 comment | 1 complexity | a41460b4fc48e82b6aa897b83d72f618 MD5 | raw file
Possible License(s): MIT
  1. %% @author author <author@example.com>
  2. %% @copyright YYYY author.
  3. %% @doc Ensure that the relatively-installed dependencies are on the code
  4. %% loading path, and locate resources relative
  5. %% to this application's path.
  6. -module(skel_deps).
  7. -author('author <author@example.com>').
  8. -export([ensure/0, ensure/1]).
  9. -export([get_base_dir/0, get_base_dir/1]).
  10. -export([local_path/1, local_path/2]).
  11. -export([deps_on_path/0, new_siblings/1]).
  12. %% @spec deps_on_path() -> [ProjNameAndVers]
  13. %% @doc List of project dependencies on the path.
  14. deps_on_path() ->
  15. F = fun (X, Acc) ->
  16. ProjDir = filename:dirname(X),
  17. case {filename:basename(X),
  18. filename:basename(filename:dirname(ProjDir))} of
  19. {"ebin", "deps"} ->
  20. [filename:basename(ProjDir) | Acc];
  21. _ ->
  22. Acc
  23. end
  24. end,
  25. ordsets:from_list(lists:foldl(F, [], code:get_path())).
  26. %% @spec new_siblings(Module) -> [Dir]
  27. %% @doc Find new siblings paths relative to Module that aren't already on the
  28. %% code path.
  29. new_siblings(Module) ->
  30. Existing = deps_on_path(),
  31. SiblingEbin = filelib:wildcard(local_path(["deps", "*", "ebin"], Module)),
  32. Siblings = [filename:dirname(X) || X <- SiblingEbin,
  33. ordsets:is_element(
  34. filename:basename(filename:dirname(X)),
  35. Existing) =:= false],
  36. lists:filter(fun filelib:is_dir/1,
  37. lists:append([[filename:join([X, "ebin"]),
  38. filename:join([X, "include"])] ||
  39. X <- Siblings])).
  40. %% @spec ensure(Module) -> ok
  41. %% @doc Ensure that all ebin and include paths for dependencies
  42. %% of the application for Module are on the code path.
  43. ensure(Module) ->
  44. code:add_paths(new_siblings(Module)),
  45. code:clash(),
  46. ok.
  47. %% @spec ensure() -> ok
  48. %% @doc Ensure that the ebin and include paths for dependencies of
  49. %% this application are on the code path. Equivalent to
  50. %% ensure(?Module).
  51. ensure() ->
  52. ensure(?MODULE).
  53. %% @spec get_base_dir(Module) -> string()
  54. %% @doc Return the application directory for Module. It assumes Module is in
  55. %% a standard OTP layout application in the ebin or src directory.
  56. get_base_dir(Module) ->
  57. {file, Here} = code:is_loaded(Module),
  58. filename:dirname(filename:dirname(Here)).
  59. %% @spec get_base_dir() -> string()
  60. %% @doc Return the application directory for this application. Equivalent to
  61. %% get_base_dir(?MODULE).
  62. get_base_dir() ->
  63. get_base_dir(?MODULE).
  64. %% @spec local_path([string()], Module) -> string()
  65. %% @doc Return an application-relative directory from Module's application.
  66. local_path(Components, Module) ->
  67. filename:join([get_base_dir(Module) | Components]).
  68. %% @spec local_path(Components) -> string()
  69. %% @doc Return an application-relative directory for this application.
  70. %% Equivalent to local_path(Components, ?MODULE).
  71. local_path(Components) ->
  72. local_path(Components, ?MODULE).
  73. %%
  74. %% Tests
  75. %%
  76. -include_lib("eunit/include/eunit.hrl").
  77. -ifdef(TEST).
  78. -endif.