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.

71 line
2.6 KiB

10 年之前
10 年之前
10 年之前
  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. -module(rebar_plugins).
  4. -export([install/1]).
  5. -include("rebar.hrl").
  6. %% ===================================================================
  7. %% Public API
  8. %% ===================================================================
  9. install(State) ->
  10. %% Set deps_dir to a different dir for plugin so they don't collide
  11. OldDepsDir = rebar_state:get(State, deps_dir, ?DEFAULT_DEPS_DIR),
  12. State1 = rebar_state:set(State, deps_dir, ?DEFAULT_PLUGINS_DIR),
  13. DepsDir = rebar_utils:deps_dir(State1),
  14. expand_plugins(DepsDir),
  15. Plugins = rebar_state:get(State1, plugins, []),
  16. PluginProviders = rebar_utils:filtermap(fun(Plugin) ->
  17. handle_plugin(Plugin, State1)
  18. end, Plugins),
  19. State2 = rebar_state:set(State1, deps_dir, OldDepsDir),
  20. {ok, PluginProviders, State2}.
  21. handle_plugin(Plugin, State) ->
  22. try
  23. {ok, _, State1} = rebar_prv_install_deps:handle_deps(State, [Plugin]),
  24. Apps = rebar_state:all_deps(State1),
  25. ToBuild = lists:dropwhile(fun rebar_app_info:valid/1, Apps),
  26. lists:foreach(fun(AppInfo) ->
  27. AppDir = rebar_app_info:dir(AppInfo),
  28. C = rebar_config:consult(AppDir),
  29. S = rebar_state:new(rebar_state:new(), C, AppDir),
  30. rebar_prv_compile:build(S, AppInfo),
  31. true = code:add_patha(filename:join(AppDir, "ebin"))
  32. end, ToBuild),
  33. plugin_providers(Plugin)
  34. catch
  35. C:T ->
  36. ?DEBUG("~p ~p", [C, T]),
  37. ?WARN("Plugin ~p not available. It will not be used.~n", [Plugin]),
  38. false
  39. end.
  40. plugin_providers({Plugin, _, _}) when is_atom(Plugin) ->
  41. validate_plugin(Plugin);
  42. plugin_providers({Plugin, _}) when is_atom(Plugin) ->
  43. validate_plugin(Plugin);
  44. plugin_providers(Plugin) when is_atom(Plugin) ->
  45. validate_plugin(Plugin).
  46. validate_plugin(Plugin) ->
  47. Exports = sets:from_list(Plugin:module_info(exports)),
  48. Required = sets:from_list([{init,1},
  49. {do,1},
  50. {format_error,1}]),
  51. case sets:is_subset(Required, Exports) of
  52. false ->
  53. ?WARN("Plugin ~p is not a provider. It will not be used.~n", [Plugin]),
  54. false;
  55. true ->
  56. {true, Plugin}
  57. end.
  58. expand_plugins(Dir) ->
  59. Apps = filelib:wildcard(filename:join([Dir, "*", "ebin"])),
  60. ok = code:add_pathsa(Apps).