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.

250 lines
8.6 KiB

пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 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. rebar_utils:delayed_halt(1);
  47. {error, rebar_abort} ->
  48. rebar_utils:delayed_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. _ ->
  55. ?ERROR(Module:format_error(Reason), [])
  56. end,
  57. rebar_utils:delayed_halt(1);
  58. {error, Error} when is_list(Error) ->
  59. ?ERROR(Error, []),
  60. rebar_utils:delayed_halt(1);
  61. Error ->
  62. %% Nothing should percolate up from rebar_core;
  63. %% Dump this error to console
  64. ?ERROR("Uncaught error in rebar_core. Run with DEBUG=1 to see stacktrace", []),
  65. ?DEBUG("Uncaught error: ~p", [Error]),
  66. rebar_utils:delayed_halt(1)
  67. end.
  68. %% Erlang-API entry point
  69. run(BaseState, Command) ->
  70. _ = application:load(rebar),
  71. BaseState1 = rebar_state:set(BaseState, task, Command),
  72. run_aux(BaseState1, [], [Command]).
  73. %% ====================================================================
  74. %% Internal functions
  75. %% ====================================================================
  76. run(RawArgs) ->
  77. _ = application:load(rebar),
  78. {GlobalPluginProviders, BaseConfig} = init_config(),
  79. case erlang:system_info(version) of
  80. "6.1" ->
  81. ?WARN("Due to a filelib bug in Erlang 17.1 it is recommended"
  82. "you update to a newer release.", []);
  83. _ ->
  84. ok
  85. end,
  86. {BaseConfig1, _Args1} = set_options(BaseConfig, {[], []}),
  87. run_aux(BaseConfig1, GlobalPluginProviders, RawArgs).
  88. run_aux(State, GlobalPluginProviders, RawArgs) ->
  89. %% Make sure crypto is running
  90. case crypto:start() of
  91. ok -> ok;
  92. {error,{already_started,crypto}} -> ok
  93. end,
  94. application:start(asn1),
  95. application:start(public_key),
  96. application:start(ssl),
  97. inets:start(),
  98. %% Process each command, resetting any state between each one
  99. BaseDir = rebar_utils:base_dir(State),
  100. State2 = rebar_state:set(State, base_dir,
  101. filename:join(filename:absname(rebar_state:dir(State)), BaseDir)),
  102. {ok, Providers} = application:get_env(rebar, providers),
  103. {ok, PluginProviders, State3} = rebar_plugins:install(State2),
  104. rebar_core:update_code_path(State3),
  105. AllProviders = Providers++PluginProviders++GlobalPluginProviders,
  106. State4 = rebar_state:create_logic_providers(AllProviders, State3),
  107. {Task, Args} = parse_args(RawArgs),
  108. rebar_core:process_command(rebar_state:command_args(State4, Args), list_to_atom(Task)).
  109. init_config() ->
  110. %% Initialize logging system
  111. Verbosity = log_level(),
  112. ok = rebar_log:init(command_line, Verbosity),
  113. Config = case os:getenv("REBAR_CONFIG") of
  114. false ->
  115. rebar_config:consult_file(?DEFAULT_CONFIG_FILE);
  116. ConfigFile ->
  117. rebar_config:consult_file(ConfigFile)
  118. end,
  119. Config1 = case rebar_config:consult_file(?LOCK_FILE) of
  120. [D] ->
  121. [{locks, D} | Config];
  122. _ ->
  123. Config
  124. end,
  125. %% If $HOME/.rebar3/config exists load and use as global config
  126. Home = rebar_utils:home_dir(),
  127. GlobalConfigFile = filename:join([Home, ?CONFIG_DIR, "config"]),
  128. State = case filelib:is_regular(GlobalConfigFile) of
  129. true ->
  130. ?DEBUG("Load global config file ~p",
  131. [GlobalConfigFile]),
  132. GlobalConfig = rebar_state:new(global, rebar_config:consult_file(GlobalConfigFile)),
  133. {ok, PluginProviders, GlobalConfig1} = rebar_plugins:install(GlobalConfig),
  134. rebar_state:new(GlobalConfig1, Config1);
  135. false ->
  136. PluginProviders = [],
  137. rebar_state:new(Config1)
  138. end,
  139. %% Determine the location of the rebar executable; important for pulling
  140. %% resources out of the escript
  141. State1 = try
  142. ScriptName = filename:absname(escript:script_name()),
  143. rebar_state:set(State, escript, ScriptName)
  144. catch
  145. _:_ ->
  146. State
  147. end,
  148. %% TODO: Do we need this still? I think it may still be used.
  149. %% Initialize vsn cache
  150. {PluginProviders, rebar_state:set(State1, vsn_cache, dict:new())}.
  151. %%
  152. %% Parse command line arguments using getopt and also filtering out any
  153. %% key=value pairs. What's left is the list of commands to run
  154. %%
  155. parse_args([]) ->
  156. parse_args(["help"]);
  157. parse_args([H | Rest]) when H =:= "-h"
  158. ; H =:= "--help" ->
  159. parse_args(["help" | Rest]);
  160. parse_args([H | Rest]) when H =:= "-v"
  161. ; H =:= "--version" ->
  162. parse_args(["version" | Rest]);
  163. parse_args([RawTask | RawRest]) ->
  164. {RawTask, RawRest}.
  165. set_options(State, {Options, NonOptArgs}) ->
  166. GlobalDefines = proplists:get_all_values(defines, Options),
  167. State1 = rebar_state:set(State, defines, GlobalDefines),
  168. %% Set global variables based on getopt options
  169. State2 = set_global_flag(State1, Options, force),
  170. Task = proplists:get_value(task, Options, "help"),
  171. {rebar_state:set(State2, task, Task), NonOptArgs}.
  172. %%
  173. %% get log level based on getopt option
  174. %%
  175. log_level() ->
  176. case os:getenv("QUIET") of
  177. false ->
  178. DefaultLevel = rebar_log:default_level(),
  179. case os:getenv("DEBUG") of
  180. false ->
  181. DefaultLevel;
  182. _ ->
  183. DefaultLevel + 3
  184. end;
  185. _ ->
  186. rebar_log:error_level()
  187. end.
  188. %%
  189. %% show version information and halt
  190. %%
  191. version() ->
  192. {ok, Vsn} = application:get_key(rebar, vsn),
  193. ?CONSOLE("rebar ~s on Erlang/OTP ~s Erts ~s",
  194. [Vsn, erlang:system_info(otp_release), erlang:system_info(version)]).
  195. %% TODO: Actually make it 'global'
  196. %%
  197. %% set global flag based on getopt option boolean value
  198. %%
  199. set_global_flag(State, Options, Flag) ->
  200. Value = case proplists:get_bool(Flag, Options) of
  201. true ->
  202. "1";
  203. false ->
  204. "0"
  205. end,
  206. rebar_state:set(State, Flag, Value).
  207. %%
  208. %% options accepted via getopt
  209. %%
  210. global_option_spec_list() ->
  211. [
  212. %% {Name, ShortOpt, LongOpt, ArgSpec, HelpMsg}
  213. {help, $h, "help", undefined, "Print this help."},
  214. %{verbose, $v, "verbose", integer, "Verbosity level (-v, -vv)."},
  215. {version, $V, "version", undefined, "Show version information."},
  216. %{config, $C, "config", string, "Rebar config file to use."},
  217. {task, undefined, undefined, string, "Task to run."}
  218. ].