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.

252 line
9.0 KiB

10 年之前
10 年之前
10 年之前
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. -module(rebar3).
  28. -export([main/1,
  29. run/2,
  30. global_option_spec_list/0,
  31. init_config/0,
  32. set_options/2,
  33. parse_args/1,
  34. version/0,
  35. log_level/0]).
  36. -include("rebar.hrl").
  37. %% ====================================================================
  38. %% Public API
  39. %% ====================================================================
  40. %% escript Entry point
  41. main(Args) ->
  42. case catch(run(Args)) of
  43. {ok, _State} ->
  44. ok;
  45. rebar_abort ->
  46. erlang:halt(1);
  47. {error, rebar_abort} ->
  48. erlang:halt(1);
  49. {error, {Module, Reason}} ->
  50. case code:which(Module) of
  51. non_existing ->
  52. ?ERROR("Uncaught error in rebar_core. Run with DEBUG=1 to see stacktrace", []),
  53. ?DEBUG("Uncaught error: ~p ~p", [Module, Reason]),
  54. ?INFO("When submitting a bug report, please include the output of `rebar3 wtf \"your command\"`", []);
  55. _ ->
  56. ?ERROR(Module:format_error(Reason), [])
  57. end,
  58. erlang:halt(1);
  59. {error, Error} when is_list(Error) ->
  60. ?ERROR(Error, []),
  61. erlang:halt(1);
  62. Error ->
  63. %% Nothing should percolate up from rebar_core;
  64. %% Dump this error to console
  65. ?ERROR("Uncaught error in rebar_core. Run with DEBUG=1 to see stacktrace", []),
  66. ?DEBUG("Uncaught error: ~p", [Error]),
  67. ?INFO("When submitting a bug report, please include the output of `rebar3 wtf \"your command\"`", []),
  68. erlang:halt(1)
  69. end.
  70. %% Erlang-API entry point
  71. run(BaseState, Commands) ->
  72. _ = application:load(rebar),
  73. BaseState1 = rebar_state:set(BaseState, task, Commands),
  74. BaseState2 = rebar_state:set(BaseState1, caller, api),
  75. run_aux(BaseState2, [], Commands).
  76. %% ====================================================================
  77. %% Internal functions
  78. %% ====================================================================
  79. run(RawArgs) ->
  80. _ = application:load(rebar),
  81. {GlobalPluginProviders, BaseState} = init_config(),
  82. BaseState1 = rebar_state:set(BaseState, caller, command_line),
  83. case erlang:system_info(version) of
  84. "6.1" ->
  85. ?WARN("Due to a filelib bug in Erlang 17.1 it is recommended"
  86. "you update to a newer release.", []);
  87. _ ->
  88. ok
  89. end,
  90. {BaseState2, _Args1} = set_options(BaseState1, {[], []}),
  91. run_aux(BaseState2, GlobalPluginProviders, RawArgs).
  92. run_aux(State, GlobalPluginProviders, RawArgs) ->
  93. %% Make sure crypto is running
  94. case crypto:start() of
  95. ok -> ok;
  96. {error,{already_started,crypto}} -> ok
  97. end,
  98. application:start(asn1),
  99. application:start(public_key),
  100. application:start(ssl),
  101. inets:start(),
  102. State2 = case os:getenv("REBAR_PROFILE") of
  103. false ->
  104. State;
  105. Profile ->
  106. rebar_state:apply_profiles(State, [list_to_atom(Profile)])
  107. end,
  108. %% Process each command, resetting any state between each one
  109. BaseDir = rebar_state:get(State, base_dir, ?DEFAULT_BASE_DIR),
  110. State3 = rebar_state:set(State2, base_dir,
  111. filename:join(filename:absname(rebar_state:dir(State2)), BaseDir)),
  112. {ok, Providers} = application:get_env(rebar, providers),
  113. {ok, PluginProviders, State4} = rebar_plugins:install(State3),
  114. rebar_core:update_code_path(State4),
  115. %% Providers can modify profiles stored in opts, so set default after initializing providers
  116. AllProviders = Providers++PluginProviders++GlobalPluginProviders,
  117. State5 = rebar_state:create_logic_providers(AllProviders, State4),
  118. State6 = rebar_state:default(State5, rebar_state:opts(State5)),
  119. {Task, Args} = parse_args(RawArgs),
  120. rebar_core:process_command(rebar_state:command_args(State6, Args), Task).
  121. init_config() ->
  122. %% Initialize logging system
  123. Verbosity = log_level(),
  124. ok = rebar_log:init(command_line, Verbosity),
  125. Config = case os:getenv("REBAR_CONFIG") of
  126. false ->
  127. rebar_config:consult_file(?DEFAULT_CONFIG_FILE);
  128. ConfigFile ->
  129. rebar_config:consult_file(ConfigFile)
  130. end,
  131. Config1 = rebar_config:merge_locks(Config, rebar_config:consult_file(?LOCK_FILE)),
  132. %% If $HOME/.config/rebar3/config exists load and use as global config
  133. GlobalConfigFile = rebar_dir:global_config(),
  134. State = case filelib:is_regular(GlobalConfigFile) of
  135. true ->
  136. ?DEBUG("Load global config file ~p",
  137. [GlobalConfigFile]),
  138. GlobalConfig = rebar_state:new(global, rebar_config:consult_file(GlobalConfigFile)),
  139. {ok, PluginProviders, GlobalConfig1} = rebar_plugins:install(GlobalConfig),
  140. rebar_state:new(GlobalConfig1, Config1);
  141. false ->
  142. PluginProviders = [],
  143. rebar_state:new(Config1)
  144. end,
  145. %% Determine the location of the rebar executable; important for pulling
  146. %% resources out of the escript
  147. State1 = try
  148. ScriptName = filename:absname(escript:script_name()),
  149. rebar_state:escript_path(State, ScriptName)
  150. catch
  151. _:_ ->
  152. State
  153. end,
  154. %% TODO: Do we need this still? I think it may still be used.
  155. %% Initialize vsn cache
  156. {PluginProviders, rebar_state:set(State1, vsn_cache, dict:new())}.
  157. parse_args([]) ->
  158. parse_args(["help"]);
  159. parse_args([H | Rest]) when H =:= "-h"
  160. ; H =:= "--help" ->
  161. parse_args(["help" | Rest]);
  162. parse_args([H | Rest]) when H =:= "-v"
  163. ; H =:= "--version" ->
  164. parse_args(["version" | Rest]);
  165. parse_args([Task | RawRest]) ->
  166. {list_to_atom(Task), RawRest}.
  167. set_options(State, {Options, NonOptArgs}) ->
  168. GlobalDefines = proplists:get_all_values(defines, Options),
  169. State1 = rebar_state:set(State, defines, GlobalDefines),
  170. %% Set global variables based on getopt options
  171. State2 = set_global_flag(State1, Options, force),
  172. Task = proplists:get_value(task, Options, "help"),
  173. {rebar_state:set(State2, task, Task), NonOptArgs}.
  174. %%
  175. %% get log level based on getopt option
  176. %%
  177. log_level() ->
  178. case os:getenv("QUIET") of
  179. false ->
  180. DefaultLevel = rebar_log:default_level(),
  181. case os:getenv("DEBUG") of
  182. false ->
  183. DefaultLevel;
  184. _ ->
  185. DefaultLevel + 3
  186. end;
  187. _ ->
  188. rebar_log:error_level()
  189. end.
  190. %%
  191. %% show version information and halt
  192. %%
  193. version() ->
  194. {ok, Vsn} = application:get_key(rebar, vsn),
  195. ?CONSOLE("rebar ~s on Erlang/OTP ~s Erts ~s",
  196. [Vsn, erlang:system_info(otp_release), erlang:system_info(version)]).
  197. %% TODO: Actually make it 'global'
  198. %%
  199. %% set global flag based on getopt option boolean value
  200. %%
  201. set_global_flag(State, Options, Flag) ->
  202. Value = case proplists:get_bool(Flag, Options) of
  203. true ->
  204. "1";
  205. false ->
  206. "0"
  207. end,
  208. rebar_state:set(State, Flag, Value).
  209. %%
  210. %% options accepted via getopt
  211. %%
  212. global_option_spec_list() ->
  213. [
  214. %% {Name, ShortOpt, LongOpt, ArgSpec, HelpMsg}
  215. {help, $h, "help", undefined, "Print this help."},
  216. {version, $V, "version", undefined, "Show version information."},
  217. %{config, $C, "config", string, "Rebar config file to use."},
  218. {task, undefined, undefined, string, "Task to run."}
  219. ].