Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

93 rader
4.1 KiB

  1. -module(rebar_hooks).
  2. -export([run_all_hooks/5
  3. ,run_all_hooks/6
  4. ,run_project_and_app_hooks/5
  5. ,format_error/1]).
  6. -include("rebar.hrl").
  7. -include_lib("providers/include/providers.hrl").
  8. -spec run_all_hooks(file:filename_all(), pre | post,
  9. atom() | {atom(), atom()} | string(),
  10. [providers:t()], rebar_app_info:t(), rebar_state:t()) -> rebar_app_info:t().
  11. run_all_hooks(Dir, Type, Command, Providers, AppInfo, State) ->
  12. State1 = rebar_state:current_app(State, AppInfo),
  13. State2 = run_provider_hooks(Dir, Type, Command, Providers, rebar_app_info:opts(AppInfo), State1),
  14. run_hooks(Dir, Type, Command, rebar_app_info:opts(AppInfo), State1),
  15. rebar_state:current_app(State2).
  16. run_all_hooks(Dir, Type, Command, Providers, State) ->
  17. run_provider_hooks(Dir, Type, Command, Providers, rebar_state:opts(State), State),
  18. run_hooks(Dir, Type, Command, rebar_state:opts(State), State).
  19. run_project_and_app_hooks(Dir, Type, Command, Providers, State) ->
  20. ProjectApps = rebar_state:project_apps(State),
  21. [rebar_hooks:run_all_hooks(Dir, Type, Command, Providers, AppInfo, State) || AppInfo <- ProjectApps],
  22. run_all_hooks(Dir, Type, Command, Providers, State).
  23. run_provider_hooks(Dir, Type, Command, Providers, Opts, State) ->
  24. case rebar_opts:get(Opts, provider_hooks, []) of
  25. [] ->
  26. State;
  27. AllHooks ->
  28. TypeHooks = proplists:get_value(Type, AllHooks, []),
  29. run_provider_hooks_(Dir, Type, Command, Providers, TypeHooks, rebar_state:opts(State, Opts))
  30. end.
  31. run_provider_hooks_(_Dir, _Type, _Command, _Providers, [], State) ->
  32. State;
  33. run_provider_hooks_(Dir, Type, Command, Providers, TypeHooks, State) ->
  34. case proplists:get_all_values(Command, TypeHooks) of
  35. [] ->
  36. State;
  37. HookProviders ->
  38. PluginDepsPaths = lists:usort(rebar_state:code_paths(State, all_plugin_deps)),
  39. code:add_pathsa(PluginDepsPaths),
  40. Providers1 = rebar_state:providers(State),
  41. State1 = rebar_state:providers(rebar_state:dir(State, Dir), Providers++Providers1),
  42. case rebar_core:do(HookProviders, State1) of
  43. {error, ProviderName} ->
  44. ?DEBUG(format_error({bad_provider, Type, Command, ProviderName}), []),
  45. throw(?PRV_ERROR({bad_provider, Type, Command, ProviderName}));
  46. {ok, State2} ->
  47. rebar_utils:remove_from_code_path(PluginDepsPaths),
  48. State2
  49. end
  50. end.
  51. format_error({bad_provider, Type, Command, {Name, Namespace}}) ->
  52. io_lib:format("Unable to run ~ts hooks for '~p', command '~p' in namespace '~p' not found.", [Type, Command, Namespace, Name]);
  53. format_error({bad_provider, Type, Command, Name}) ->
  54. io_lib:format("Unable to run ~ts hooks for '~p', command '~p' not found.", [Type, Command, Name]).
  55. run_hooks(Dir, pre, Command, Opts, State) ->
  56. run_hooks(Dir, pre_hooks, Command, Opts, State);
  57. run_hooks(Dir, post, Command, Opts, State) ->
  58. run_hooks(Dir, post_hooks, Command, Opts, State);
  59. run_hooks(Dir, Type, Command, Opts, State) ->
  60. case rebar_opts:get(Opts, Type, []) of
  61. [] ->
  62. ?DEBUG("run_hooks(~p, ~p, ~p) -> no hooks defined\n", [Dir, Type, Command]),
  63. ok;
  64. Hooks ->
  65. Env = rebar_env:create_env(State, Opts),
  66. lists:foreach(fun({_, C, _}=Hook) when C =:= Command ->
  67. apply_hook(Dir, Env, Hook);
  68. ({C, _}=Hook) when C =:= Command ->
  69. apply_hook(Dir, Env, Hook);
  70. (_) ->
  71. continue
  72. end, Hooks)
  73. end.
  74. apply_hook(Dir, Env, {Arch, Command, Hook}) ->
  75. case rebar_utils:is_arch(Arch) of
  76. true ->
  77. apply_hook(Dir, Env, {Command, Hook});
  78. false ->
  79. ok
  80. end;
  81. apply_hook(Dir, Env, {Command, Hook}) ->
  82. Msg = lists:flatten(io_lib:format("Hook for ~p failed!~n", [Command])),
  83. rebar_utils:sh(Hook, [use_stdout, {cd, Dir}, {env, Env}, {abort_on_error, Msg}]).