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.

132 rivejä
6.0 KiB

10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
  1. -module(rebar_hooks).
  2. -export([run_all_hooks/5
  3. ,run_all_hooks/6
  4. ,format_error/1]).
  5. -include("rebar.hrl").
  6. -include_lib("providers/include/providers.hrl").
  7. -spec run_all_hooks(file:filename_all(), pre | post,
  8. atom() | {atom(), atom()} | string(),
  9. [providers:t()], rebar_app_info:t(), rebar_state:t()) -> ok.
  10. run_all_hooks(Dir, Type, Command, Providers, AppInfo, State) ->
  11. run_provider_hooks(Dir, Type, Command, Providers, rebar_app_info:opts(AppInfo), State),
  12. run_hooks(Dir, Type, Command, rebar_app_info:opts(AppInfo), State).
  13. run_all_hooks(Dir, Type, Command, Providers, State) ->
  14. run_provider_hooks(Dir, Type, Command, Providers, rebar_state:opts(State), State),
  15. run_hooks(Dir, Type, Command, rebar_state:opts(State), State).
  16. run_provider_hooks(Dir, Type, Command, Providers, Opts, State) ->
  17. case rebar_opts:get(Opts, provider_hooks, []) of
  18. [] ->
  19. ok;
  20. AllHooks ->
  21. TypeHooks = proplists:get_value(Type, AllHooks, []),
  22. run_provider_hooks_(Dir, Type, Command, Providers, TypeHooks, rebar_state:opts(State, Opts))
  23. end.
  24. run_provider_hooks_(_Dir, _Type, _Command, _Providers, [], _State) ->
  25. ok;
  26. run_provider_hooks_(Dir, Type, Command, Providers, TypeHooks, State) ->
  27. PluginDepsPaths = rebar_state:code_paths(State, all_plugin_deps),
  28. code:add_pathsa(PluginDepsPaths),
  29. Providers1 = rebar_state:providers(State),
  30. State1 = rebar_state:providers(rebar_state:dir(State, Dir), Providers++Providers1),
  31. HookProviders = proplists:get_all_values(Command, TypeHooks),
  32. case rebar_core:do(HookProviders, State1) of
  33. {error, ProviderName} ->
  34. ?DEBUG(format_error({bad_provider, Type, Command, ProviderName}), []),
  35. throw(?PRV_ERROR({bad_provider, Type, Command, ProviderName}));
  36. {ok, _} ->
  37. rebar_utils:remove_from_code_path(PluginDepsPaths)
  38. end.
  39. format_error({bad_provider, Type, Command, {Name, Namespace}}) ->
  40. io_lib:format("Unable to run ~s hooks for '~p', command '~p' in namespace '~p' not found.", [Type, Command, Namespace, Name]);
  41. format_error({bad_provider, Type, Command, Name}) ->
  42. io_lib:format("Unable to run ~s hooks for '~p', command '~p' not found.", [Type, Command, Name]).
  43. %% @doc The following environment variables are exported when running
  44. %% a hook (absolute paths):
  45. %%
  46. %% REBAR_DEPS_DIR = rebar_dir:deps_dir/1
  47. %% REBAR_BUILD_DIR = rebar_dir:base_dir/1
  48. %% REBAR_ROOT_DIR = rebar_dir:root_dir/1
  49. %% REBAR_CHECKOUTS_DIR = rebar_dir:checkouts_dir/1
  50. %% REBAR_PLUGINS_DIR = rebar_dir:plugins_dir/1
  51. %% REBAR_GLOBAL_CONFIG_DIR = rebar_dir:global_config_dir/1
  52. %% REBAR_GLOBAL_CACHE_DIR = rebar_dir:global_cache_dir/1
  53. %% REBAR_TEMPLATE_DIR = rebar_dir:template_dir/1
  54. %% REBAR_APP_DIRS = rebar_dir:lib_dirs/1
  55. %% REBAR_SRC_DIRS = rebar_dir:src_dirs/1
  56. %%
  57. %% autoconf compatible variables
  58. %% (see: http://www.gnu.org/software/autoconf/manual/autoconf.html#Erlang-Libraries):
  59. %% ERLANG_ERTS_VER = erlang:system_info(version)
  60. %% ERLANG_ROOT_DIR = code:root_dir/0
  61. %% ERLANG_LIB_DIR_erl_interface = code:lib_dir(erl_interface)
  62. %% ERLANG_LIB_VER_erl_interface = version part of path returned by code:lib_dir(erl_interface)
  63. %% ERL = ERLANG_ROOT_DIR/bin/erl
  64. %% ERLC = ERLANG_ROOT_DIR/bin/erl
  65. %%
  66. run_hooks(Dir, pre, Command, Opts, State) ->
  67. run_hooks(Dir, pre_hooks, Command, Opts, State);
  68. run_hooks(Dir, post, Command, Opts, State) ->
  69. run_hooks(Dir, post_hooks, Command, Opts, State);
  70. run_hooks(Dir, Type, Command, Opts, State) ->
  71. case rebar_opts:get(Opts, Type, []) of
  72. [] ->
  73. ok;
  74. Hooks ->
  75. Env = create_env(State, Opts),
  76. lists:foreach(fun({_, C, _}=Hook) when C =:= Command ->
  77. apply_hook(Dir, Env, Hook);
  78. ({C, _}=Hook) when C =:= Command ->
  79. apply_hook(Dir, Env, Hook);
  80. (_) ->
  81. continue
  82. end, Hooks)
  83. end.
  84. apply_hook(Dir, Env, {Arch, Command, Hook}) ->
  85. case rebar_utils:is_arch(Arch) of
  86. true ->
  87. apply_hook(Dir, Env, {Command, Hook});
  88. false ->
  89. ok
  90. end;
  91. apply_hook(Dir, Env, {Command, Hook}) ->
  92. Msg = lists:flatten(io_lib:format("Hook for ~p failed!~n", [Command])),
  93. rebar_utils:sh(Hook, [use_stdout, {cd, Dir}, {env, Env}, {abort_on_error, Msg}]).
  94. create_env(State, Opts) ->
  95. BaseDir = rebar_dir:base_dir(State),
  96. [
  97. {"REBAR_DEPS_DIR", filename:absname(rebar_dir:deps_dir(State))},
  98. {"REBAR_BUILD_DIR", filename:absname(rebar_dir:base_dir(State))},
  99. {"REBAR_ROOT_DIR", filename:absname(rebar_dir:root_dir(State))},
  100. {"REBAR_CHECKOUTS_DIR", filename:absname(rebar_dir:checkouts_dir(State))},
  101. {"REBAR_PLUGINS_DIR", filename:absname(rebar_dir:plugins_dir(State))},
  102. {"REBAR_GLOBAL_CONFIG_DIR", filename:absname(rebar_dir:global_config_dir(State))},
  103. {"REBAR_GLOBAL_CACHE_DIR", filename:absname(rebar_dir:global_cache_dir(State))},
  104. {"REBAR_TEMPLATE_DIR", filename:absname(rebar_dir:template_dir(State))},
  105. {"REBAR_APP_DIRS", join_dirs(BaseDir, rebar_dir:lib_dirs(State))},
  106. {"REBAR_SRC_DIRS", join_dirs(BaseDir, rebar_dir:all_src_dirs(Opts))},
  107. {"ERLANG_ERTS_VER", erlang:system_info(version)},
  108. {"ERLANG_ROOT_DIR", code:root_dir()},
  109. {"ERLANG_LIB_DIR_erl_interface", code:lib_dir(erl_interface)},
  110. {"ERLANG_LIB_VER_erl_interface", re_version(code:lib_dir(erl_interface))},
  111. {"ERL", filename:join([code:root_dir(), "bin", "erl"])},
  112. {"ERLC", filename:join([code:root_dir(), "bin", "erlc"])}
  113. ].
  114. join_dirs(BaseDir, Dirs) ->
  115. string:join([ filename:join(BaseDir, Dir) || Dir <- Dirs ], ":").
  116. re_version(Path) ->
  117. case re:run(Path, "^.*-(?<VER>[^/-]*)$", [{capture, [1], list}]) of
  118. nomatch -> "";
  119. {match, [Ver]} -> Ver
  120. end.