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.

258 line
8.6 KiB

  1. %% -------------------------------------------------------------------
  2. %%
  3. %% rebar: Erlang Build Tools
  4. %%
  5. %% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
  6. %%
  7. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  8. %% of this software and associated documentation files (the "Software"), to deal
  9. %% in the Software without restriction, including without limitation the rights
  10. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. %% copies of the Software, and to permit persons to whom the Software is
  12. %% furnished to do so, subject to the following conditions:
  13. %%
  14. %% The above copyright notice and this permission notice shall be included in
  15. %% all copies or substantial portions of the Software.
  16. %%
  17. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. %% THE SOFTWARE.
  24. %% -------------------------------------------------------------------
  25. -module(rebar_core).
  26. -export([run/1]).
  27. -export([app_dir/1, rel_dir/1]). % Ugh
  28. -include("rebar.hrl").
  29. -ifndef(BUILD_TIME).
  30. -define(BUILD_TIME, "undefined").
  31. -endif.
  32. %% ===================================================================
  33. %% Public API
  34. %% ===================================================================
  35. run(["version"]) ->
  36. %% Load application spec and display vsn and build time info
  37. ok = application:load(rebar),
  38. {ok, Vsn} = application:get_key(rebar, vsn),
  39. ?CONSOLE("Version ~s built ~s\n", [Vsn, ?BUILD_TIME]),
  40. ok;
  41. run(Args) ->
  42. %% Pre-load the rebar app so that we get default configuration
  43. ok = application:load(rebar),
  44. OptSpecList = option_spec_list(),
  45. %% Parse getopt options
  46. case getopt:parse(OptSpecList, Args) of
  47. {ok, {_Options, []}} ->
  48. %% no command to run specified
  49. getopt:usage(OptSpecList, "rebar");
  50. {ok, {Options, NonOptArgs}} ->
  51. case proplists:get_bool(help, Options) of
  52. true ->
  53. %% display help
  54. getopt:usage(OptSpecList, "rebar");
  55. false ->
  56. %% set global variables based on getopt options
  57. set_global_flag(Options, verbose),
  58. set_global_flag(Options, force),
  59. %% run rebar with supplied options
  60. run2(NonOptArgs)
  61. end;
  62. {error, {Reason, Data}} ->
  63. ?ERROR("Error: ~s ~p~n~n", [Reason, Data]),
  64. getopt:usage(OptSpecList, "rebar")
  65. end.
  66. run2(Commands) ->
  67. %% Make sure crypto is running
  68. crypto:start(),
  69. %% Initialize logging system
  70. rebar_log:init(),
  71. %% Convert command strings to atoms
  72. CommandAtoms = [list_to_atom(C) || C <- Commands],
  73. %% Load rebar.config, if it exists
  74. process_dir(rebar_utils:get_cwd(), rebar_config:new(), CommandAtoms).
  75. %% ===================================================================
  76. %% Internal functions
  77. %% ===================================================================
  78. %%
  79. %% set global flag based on getopt option boolean value
  80. %%
  81. set_global_flag(Options, Flag) ->
  82. Value = proplists:get_bool(Flag, Options),
  83. rebar_config:set_global(Flag, Value).
  84. %%
  85. %% options accepted via getopt
  86. %%
  87. option_spec_list() ->
  88. [
  89. %% {Name, ShortOpt, LongOpt, ArgSpec, HelpMsg}
  90. {help, $h, "help", undefined, "Show the program options"},
  91. {verbose, $v, "verbose", {boolean, false}, "Be verbose about what gets done"},
  92. {force, $f, "force", {boolean, false}, "Force"}
  93. ].
  94. process_dir(Dir, ParentConfig, Commands) ->
  95. ok = file:set_cwd(Dir),
  96. Config = rebar_config:new(ParentConfig),
  97. %% Save the current code path and then update it with
  98. %% lib_dirs. Children inherit parents code path, but we
  99. %% also want to ensure that we restore everything to pristine
  100. %% condition after processing this child
  101. CurrentCodePath = update_code_path(Config),
  102. %% Get the list of processing modules and check each one against
  103. %% CWD to see if it's a fit -- if it is, use that set of modules
  104. %% to process this dir.
  105. {ok, AvailModuleSets} = application:get_env(rebar, modules),
  106. {DirModules, ModuleSetFile} = choose_module_set(AvailModuleSets, Dir),
  107. %% Get the list of modules for "any dir". This is a catch-all list of modules
  108. %% that are processed in addion to modules associated with this directory
  109. %5 type. These any_dir modules are processed FIRST.
  110. {ok, AnyDirModules} = application:get_env(rebar, any_dir_modules),
  111. Modules = AnyDirModules ++ DirModules,
  112. %% Give the modules a chance to tweak config and indicate if there
  113. %% are any other dirs that might need processing first.
  114. {UpdatedConfig, Dirs} = acc_modules(select_modules(Modules, preprocess, []),
  115. preprocess, Config, ModuleSetFile, []),
  116. [process_dir(D, UpdatedConfig, Commands) || D <- Dirs],
  117. %% Finally, process the current working directory
  118. apply_commands(Commands, Modules, UpdatedConfig, ModuleSetFile),
  119. %% Once we're all done processing, reset the code path to whatever
  120. %% the parent initialized it to
  121. restore_code_path(CurrentCodePath),
  122. ok.
  123. %%
  124. %% Given a list of module sets from rebar.app and a directory, find
  125. %% the appropriate subset of modules for this directory
  126. %%
  127. choose_module_set([], _Dir) ->
  128. {[], undefined};
  129. choose_module_set([{Fn, Modules} | Rest], Dir) ->
  130. case ?MODULE:Fn(Dir) of
  131. {true, File} ->
  132. {Modules, File};
  133. false ->
  134. choose_module_set(Rest, Dir)
  135. end.
  136. %%
  137. %% Return .app file if the current directory is an OTP app
  138. %%
  139. app_dir(Dir) ->
  140. rebar_app_utils:is_app_dir(Dir).
  141. %%
  142. %% Return the reltool.config file if the current directory is release directory
  143. %%
  144. rel_dir(Dir) ->
  145. rebar_rel_utils:is_rel_dir(Dir).
  146. apply_commands([], _Modules, _Config, _ModuleFile) ->
  147. ok;
  148. apply_commands([Command | Rest], Modules, Config, ModuleFile) ->
  149. case select_modules(Modules, Command, []) of
  150. [] ->
  151. apply_commands(Rest, Modules, Config, ModuleFile);
  152. TargetModules ->
  153. %% Provide some info on where we are
  154. Dir = rebar_utils:get_cwd(),
  155. ?CONSOLE("==> ~s (~s)\n", [filename:basename(Dir), Command]),
  156. %% Run the available modules
  157. case catch(run_modules(TargetModules, Command, Config, ModuleFile)) of
  158. ok ->
  159. apply_commands(Rest, Modules, Config, ModuleFile);
  160. {error, failed} ->
  161. ?FAIL;
  162. Other ->
  163. ?ERROR("~p failed while processing ~s: ~p", [Command, Dir, Other]),
  164. ?FAIL
  165. end
  166. end.
  167. update_code_path(Config) ->
  168. case rebar_config:get(Config, lib_dirs, []) of
  169. [] ->
  170. no_change;
  171. Paths ->
  172. OldPath = code:get_path(),
  173. LibPaths = expand_lib_dirs(Paths, rebar_utils:get_cwd(), []),
  174. ok = code:add_pathsa(LibPaths),
  175. {old, OldPath}
  176. end.
  177. restore_code_path(no_change) ->
  178. ok;
  179. restore_code_path({old, Path}) ->
  180. true = code:set_path(Path),
  181. ok.
  182. expand_lib_dirs([], _Root, Acc) ->
  183. Acc;
  184. expand_lib_dirs([Dir | Rest], Root, Acc) ->
  185. Apps = filelib:wildcard(filename:join([Dir, '*', ebin])),
  186. FqApps = [filename:join([Root, A]) || A <- Apps],
  187. expand_lib_dirs(Rest, Root, Acc ++ FqApps).
  188. select_modules([], _Command, Acc) ->
  189. lists:reverse(Acc);
  190. select_modules([Module | Rest], Command, Acc) ->
  191. Exports = Module:module_info(exports),
  192. case lists:member({Command, 2}, Exports) of
  193. true ->
  194. select_modules(Rest, Command, [Module | Acc]);
  195. false ->
  196. select_modules(Rest, Command, Acc)
  197. end.
  198. run_modules([], _Command, _Config, _File) ->
  199. ok;
  200. run_modules([Module | Rest], Command, Config, File) ->
  201. case Module:Command(Config, File) of
  202. ok ->
  203. run_modules(Rest, Command, Config, File);
  204. {error, Reason} ->
  205. {error, Reason}
  206. end.
  207. acc_modules([], _Command, Config, _File, Acc) ->
  208. {Config, Acc};
  209. acc_modules([Module | Rest], Command, Config, File, Acc) ->
  210. case Module:Command(Config, File) of
  211. {ok, NewConfig, Result} when is_list(Result) ->
  212. List = Result;
  213. {ok, NewConfig, Result} ->
  214. List = [Result]
  215. end,
  216. acc_modules(Rest, Command, NewConfig, File, List ++ Acc).