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.

178 line
8.1 KiB

10 年之前
  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. %% -------------------------------------------------------------------
  4. %%
  5. %% rebar: Erlang Build Tools
  6. %%
  7. %% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
  8. %%
  9. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  10. %% of this software and associated documentation files (the "Software"), to deal
  11. %% in the Software without restriction, including without limitation the rights
  12. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. %% copies of the Software, and to permit persons to whom the Software is
  14. %% furnished to do so, subject to the following conditions:
  15. %%
  16. %% The above copyright notice and this permission notice shall be included in
  17. %% all copies or substantial portions of the Software.
  18. %%
  19. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. %% THE SOFTWARE.
  26. %% -------------------------------------------------------------------
  27. %% @doc Module providing core functionality about command dispatch, namespacing,
  28. %% and chaining for rebar3.
  29. -module(rebar_core).
  30. -export([init_command/2, process_namespace/2, process_command/2, do/2, format_error/1]).
  31. -include("rebar.hrl").
  32. -include_lib("providers/include/providers.hrl").
  33. %% @doc initial command set up; based on the first fragment of the
  34. %% command, dispatch to special environments. The keywords for
  35. %% `do' and `as' are implicitly reserved here, barring them from
  36. %% being used as other commands or namespaces.
  37. -spec init_command(rebar_state:t(), atom()) ->
  38. {ok, rebar_state:t()} | {error, term()}.
  39. init_command(State, do) ->
  40. process_command(rebar_state:namespace(State, default), do);
  41. init_command(State, as) ->
  42. process_command(rebar_state:namespace(State, default), as);
  43. init_command(State, Command) ->
  44. case process_namespace(State, Command) of
  45. {ok, State1, Command1} ->
  46. process_command(State1, Command1);
  47. {error, Reason} ->
  48. {error, Reason}
  49. end.
  50. %% @doc parse the commands starting at the namespace level;
  51. %% a namespace is found if the first keyword to match is not
  52. %% belonging to an existing provider, and iff the keyword also
  53. %% matches a registered namespace.
  54. %% The command to run is returned last; for namespaces, some
  55. %% magic is done implicitly calling `do' as an indirect dispatcher.
  56. -spec process_namespace(rebar_state:t(), atom()) ->
  57. {error, term()} | {ok, rebar_state:t(), atom()}.
  58. process_namespace(_State, as) ->
  59. {error, "Namespace 'as' is forbidden"};
  60. process_namespace(State, Command) ->
  61. Providers = rebar_state:providers(State),
  62. CommandProvider = providers:get_provider(Command, Providers, default),
  63. case CommandProvider of
  64. not_found ->
  65. case providers:get_providers_by_namespace(Command, Providers) of
  66. [] ->
  67. {error, io_lib:format("Command ~p not found", [Command])};
  68. _ ->
  69. %% Replay 'do' as a command of that namespace
  70. {ok, rebar_state:namespace(State, Command), do}
  71. end;
  72. _ ->
  73. {ok, rebar_state:namespace(State, default), Command}
  74. end.
  75. %% @doc Dispatches a given command based on the current state.
  76. %% This requires mapping a command name to a specific provider.
  77. %% `as' and `do' are still treated as special providers here.
  78. %% Basic profile application may also be run.
  79. %%
  80. %% The function also takes care of expanding a provider to its
  81. %% dependencies in the proper order.
  82. -spec process_command(rebar_state:t(), atom()) ->
  83. {ok, rebar_state:t()} | {error, string()} | {error, {module(), any()}}.
  84. process_command(State, Command) ->
  85. %% ? rebar_prv_install_deps:setup_env(State),
  86. Providers = rebar_state:providers(State),
  87. Namespace = rebar_state:namespace(State),
  88. TargetProviders = providers:get_target_providers(Command, Providers, Namespace),
  89. CommandProvider = providers:get_provider(Command, Providers, Namespace),
  90. ?DEBUG("Expanded command sequence to be run: ~p", [TargetProviders]),
  91. case CommandProvider of
  92. not_found when Command =/= do ->
  93. case Namespace of
  94. default ->
  95. {error, io_lib:format("Command ~p not found", [Command])};
  96. _ ->
  97. {error, io_lib:format("Command ~p not found in namespace ~p",
  98. [Command, Namespace])}
  99. end;
  100. not_found when Command =:= do, Namespace =/= default ->
  101. do([{default, do} | TargetProviders], State);
  102. CommandProvider ->
  103. case Command of
  104. do ->
  105. do(TargetProviders, State);
  106. as ->
  107. do(TargetProviders, State);
  108. _ ->
  109. Profiles = providers:profiles(CommandProvider),
  110. State1 = rebar_state:apply_profiles(State, Profiles),
  111. Opts = providers:opts(CommandProvider)++rebar3:global_option_spec_list(),
  112. case getopt:parse(Opts, rebar_state:command_args(State1)) of
  113. {ok, Args} ->
  114. State2 = rebar_state:command_parsed_args(State1, Args),
  115. do(TargetProviders, State2);
  116. {error, {invalid_option, Option}} ->
  117. {error, io_lib:format("Invalid option ~ts on task ~p", [Option, Command])};
  118. {error, {invalid_option_arg, {Option, Arg}}} ->
  119. {error, io_lib:format("Invalid argument ~ts to option ~ts", [Arg, Option])};
  120. {error, {missing_option_arg, Option}} ->
  121. {error, io_lib:format("Missing argument to option ~ts", [Option])}
  122. end
  123. end
  124. end.
  125. %% @doc execute the selected providers. If a chain of providers
  126. %% has been returned, run them one after the other, while piping
  127. %% the state from the first into the next one.
  128. -spec do([{atom(), atom()}], rebar_state:t()) ->
  129. {ok, rebar_state:t()} | {error, string()} | {error, {module(), any()}}.
  130. do([], State) ->
  131. {ok, State};
  132. do([ProviderName | Rest], State) ->
  133. ?DEBUG("Provider: ~p", [ProviderName]),
  134. %% Special providers like 'as', 'do' or some hooks may be passed
  135. %% as a tuple {Namespace, Name}, otherwise not. Handle them
  136. %% on a per-need basis.
  137. Provider = case ProviderName of
  138. {Namespace, Name} ->
  139. providers:get_provider(Name
  140. ,rebar_state:providers(State)
  141. ,Namespace);
  142. _ ->
  143. providers:get_provider(ProviderName
  144. ,rebar_state:providers(State)
  145. ,rebar_state:namespace(State))
  146. end,
  147. try providers:do(Provider, State) of
  148. {ok, State1} ->
  149. do(Rest, State1);
  150. {error, Error} ->
  151. {error, Error}
  152. catch
  153. ?WITH_STACKTRACE(error,undef,Stack)
  154. case Stack of
  155. [{ProviderName, do, [_], _}|_] ->
  156. %% This should really only happen if a plugin provider doesn't export do/1
  157. ?DEBUG("Undefined call to provider's do/1 function:~n~p", [Stack]),
  158. ?PRV_ERROR({bad_provider_namespace, ProviderName});
  159. _ -> % re-raise
  160. erlang:raise(error, undef, Stack)
  161. end;
  162. error:{badrecord,provider} ->
  163. {error, ProviderName}
  164. end.
  165. %% @doc convert a given exception's payload into an io description.
  166. -spec format_error(any()) -> iolist().
  167. format_error({bad_provider_namespace, {Namespace, Name}}) ->
  168. io_lib:format("Undefined command ~ts in namespace ~ts", [Name, Namespace]);
  169. format_error({bad_provider_namespace, Name}) ->
  170. io_lib:format("Undefined command ~ts", [Name]).