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.

89 lines
3.2 KiB

10 years ago
10 years ago
  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. -module(rebar_plugins).
  4. -export([install/1, handle_plugins/2, handle_plugins/3]).
  5. -include("rebar.hrl").
  6. %% ===================================================================
  7. %% Public API
  8. %% ===================================================================
  9. -spec install(rebar_state:t()) -> rebar_state:t().
  10. install(State) ->
  11. Plugins = rebar_state:get(State, plugins, []),
  12. ProjectApps = rebar_state:project_apps(State),
  13. OtherPlugins = lists:flatmap(fun(App) ->
  14. AppDir = rebar_app_info:dir(App),
  15. C = rebar_config:consult(AppDir),
  16. S = rebar_state:new(rebar_state:new(), C, AppDir),
  17. rebar_state:get(S, plugins, [])
  18. end, ProjectApps),
  19. handle_plugins(Plugins++OtherPlugins, State).
  20. -spec handle_plugins([rebar_prv_install_deps:dep()], rebar_state:t()) -> rebar_state:t().
  21. handle_plugins(Plugins, State) ->
  22. handle_plugins(default, Plugins, State).
  23. handle_plugins(Profile, Plugins, State) ->
  24. %% Set deps dir to plugins dir so apps are installed there
  25. State1 = rebar_state:set(State, deps_dir, ?DEFAULT_PLUGINS_DIR),
  26. PluginProviders = lists:flatmap(fun(Plugin) ->
  27. handle_plugin(Profile, Plugin, State1)
  28. end, Plugins),
  29. %% reset deps dir
  30. State2 = rebar_state:set(State1, deps_dir, ?DEFAULT_DEPS_DIR),
  31. rebar_state:create_logic_providers(PluginProviders, State2).
  32. handle_plugin(Profile, Plugin, State) ->
  33. try
  34. {ok, _, State2} = rebar_prv_install_deps:handle_deps(Profile, State, [Plugin]),
  35. Apps = rebar_state:all_deps(State2),
  36. ToBuild = lists:dropwhile(fun rebar_app_info:valid/1, Apps),
  37. [build_plugin(AppInfo) || AppInfo <- ToBuild],
  38. [true = code:add_patha(filename:join(rebar_app_info:dir(AppInfo), "ebin")) || AppInfo <- Apps],
  39. plugin_providers(Plugin)
  40. catch
  41. C:T ->
  42. ?DEBUG("~p ~p ~p", [C, T, erlang:get_stacktrace()]),
  43. ?WARN("Plugin ~p not available. It will not be used.", [Plugin]),
  44. []
  45. end.
  46. build_plugin(AppInfo) ->
  47. AppDir = rebar_app_info:dir(AppInfo),
  48. C = rebar_config:consult(AppDir),
  49. S = rebar_state:new(rebar_state:new(), C, AppDir),
  50. rebar_prv_compile:compile(S, [], AppInfo).
  51. plugin_providers({Plugin, _, _}) when is_atom(Plugin) ->
  52. validate_plugin(Plugin);
  53. plugin_providers({Plugin, _}) when is_atom(Plugin) ->
  54. validate_plugin(Plugin);
  55. plugin_providers(Plugin) when is_atom(Plugin) ->
  56. validate_plugin(Plugin).
  57. validate_plugin(Plugin) ->
  58. _ = application:load(Plugin),
  59. case application:get_env(Plugin, providers) of
  60. {ok, Providers} ->
  61. Providers;
  62. undefined ->
  63. Exports = Plugin:module_info(exports),
  64. case lists:member({init,1}, Exports) of
  65. false ->
  66. ?WARN("Plugin ~p does not export init/1. It will not be used.", [Plugin]),
  67. [];
  68. true ->
  69. [Plugin]
  70. end
  71. end.