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.

253 line
8.7 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/1,
  30. run/2,
  31. global_option_spec_list/0,
  32. init_config/0,
  33. set_options/2,
  34. parse_args/1,
  35. version/0,
  36. log_level/0]).
  37. -include("rebar.hrl").
  38. %% ====================================================================
  39. %% Public API
  40. %% ====================================================================
  41. %% escript Entry point
  42. main(Args) ->
  43. case catch(run(Args)) of
  44. {ok, _State} ->
  45. erlang:halt(0);
  46. rebar_abort ->
  47. erlang:halt(1);
  48. {error, rebar_abort} ->
  49. erlang:halt(1);
  50. {error, {Module, Reason}} ->
  51. case code:which(Module) of
  52. non_existing ->
  53. ?ERROR("Uncaught error in rebar_core. Run with DEBUG=1 to see stacktrace", []),
  54. ?DEBUG("Uncaught error: ~p ~p", [Module, Reason]),
  55. ?INFO("When submitting a bug report, please include the output of `rebar3 report \"your command\"`", []);
  56. _ ->
  57. ?ERROR(Module:format_error(Reason), [])
  58. end,
  59. erlang:halt(1);
  60. {error, Error} when is_list(Error) ->
  61. ?ERROR(Error, []),
  62. erlang:halt(1);
  63. Error ->
  64. %% Nothing should percolate up from rebar_core;
  65. %% Dump this error to console
  66. ?ERROR("Uncaught error in rebar_core. Run with DEBUG=1 to see stacktrace", []),
  67. ?DEBUG("Uncaught error: ~p", [Error]),
  68. ?INFO("When submitting a bug report, please include the output of `rebar3 report \"your command\"`", []),
  69. erlang:halt(1)
  70. end.
  71. %% Erlang-API entry point
  72. run(BaseState, Commands) ->
  73. _ = application:load(rebar),
  74. BaseState1 = rebar_state:set(BaseState, task, Commands),
  75. BaseState2 = rebar_state:set(BaseState1, caller, api),
  76. run_aux(BaseState2, Commands).
  77. %% ====================================================================
  78. %% Internal functions
  79. %% ====================================================================
  80. run(RawArgs) ->
  81. _ = application:load(rebar),
  82. BaseState = init_config(),
  83. BaseState1 = rebar_state:set(BaseState, caller, command_line),
  84. case erlang:system_info(version) of
  85. "6.1" ->
  86. ?WARN("Due to a filelib bug in Erlang 17.1 it is recommended"
  87. "you update to a newer release.", []);
  88. _ ->
  89. ok
  90. end,
  91. {BaseState2, _Args1} = set_options(BaseState1, {[], []}),
  92. run_aux(BaseState2, RawArgs).
  93. run_aux(State, RawArgs) ->
  94. %% Make sure crypto is running
  95. case crypto:start() of
  96. ok -> ok;
  97. {error,{already_started,crypto}} -> ok
  98. end,
  99. application:start(asn1),
  100. application:start(public_key),
  101. application:start(ssl),
  102. inets:start(),
  103. State2 = case os:getenv("REBAR_PROFILE") of
  104. false ->
  105. State;
  106. "" ->
  107. State;
  108. Profile ->
  109. rebar_state:apply_profiles(State, [list_to_atom(Profile)])
  110. end,
  111. %% Process each command, resetting any state between each one
  112. BaseDir = rebar_state:get(State, base_dir, ?DEFAULT_BASE_DIR),
  113. State3 = rebar_state:set(State2, base_dir,
  114. filename:join(filename:absname(rebar_state:dir(State2)), BaseDir)),
  115. {ok, Providers} = application:get_env(rebar, providers),
  116. State4 = rebar_plugins:install(State3),
  117. %% Providers can modify profiles stored in opts, so set default after initializing providers
  118. State5 = rebar_state:create_logic_providers(Providers, State4),
  119. State6 = rebar_state:default(State5, rebar_state:opts(State5)),
  120. {Task, Args} = parse_args(RawArgs),
  121. State7 = rebar_state:code_paths(State6, default, code:get_path()),
  122. rebar_core:init_command(rebar_state:command_args(State7, Args), Task).
  123. init_config() ->
  124. %% Initialize logging system
  125. Verbosity = log_level(),
  126. ok = rebar_log:init(command_line, Verbosity),
  127. Config = case os:getenv("REBAR_CONFIG") of
  128. false ->
  129. rebar_config:consult_file(?DEFAULT_CONFIG_FILE);
  130. ConfigFile ->
  131. rebar_config:consult_file(ConfigFile)
  132. end,
  133. Config1 = rebar_config:merge_locks(Config, rebar_config:consult_file(?LOCK_FILE)),
  134. %% If $HOME/.config/rebar3/config exists load and use as global config
  135. GlobalConfigFile = rebar_dir:global_config(),
  136. State = case filelib:is_regular(GlobalConfigFile) of
  137. true ->
  138. ?DEBUG("Load global config file ~p",
  139. [GlobalConfigFile]),
  140. GlobalConfig = rebar_state:new(rebar_config:consult_file(GlobalConfigFile)),
  141. rebar_state:new(GlobalConfig, Config1);
  142. false ->
  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. 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. ].