您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

658 行
26 KiB

14 年前
  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(rebar_core).
  28. -export([process_commands/2, help/2]).
  29. -include("rebar.hrl").
  30. %% ===================================================================
  31. %% Internal functions
  32. %% ===================================================================
  33. help(ParentConfig, Commands) ->
  34. %% get all core modules
  35. {ok, AnyDirModules} = application:get_env(rebar, any_dir_modules),
  36. {ok, RawCoreModules} = application:get_env(rebar, modules),
  37. AppDirModules = proplists:get_value(app_dir, RawCoreModules),
  38. RelDirModules = proplists:get_value(rel_dir, RawCoreModules),
  39. CoreModules = AnyDirModules ++ AppDirModules ++ RelDirModules,
  40. %% get plugin modules
  41. Predirs = [],
  42. Dir = rebar_utils:get_cwd(),
  43. PredirsAssoc = remember_cwd_predirs(Dir, Predirs),
  44. Config = maybe_load_local_config(Dir, ParentConfig),
  45. {ok, PluginModules} = plugin_modules(Config, PredirsAssoc),
  46. AllModules = CoreModules ++ PluginModules,
  47. lists:foreach(
  48. fun(Cmd) ->
  49. ?CONSOLE("==> help ~p~n~n", [Cmd]),
  50. CmdModules = select_modules(AllModules, Cmd, []),
  51. Modules = select_modules(CmdModules, info, []),
  52. lists:foreach(fun(M) ->
  53. ?CONSOLE("=== ~p:~p ===~n", [M, Cmd]),
  54. M:info(help, Cmd),
  55. ?CONSOLE("~n", [])
  56. end, Modules)
  57. end, Commands).
  58. process_commands([], ParentConfig) ->
  59. AbortTrapped = rebar_config:get_xconf(ParentConfig, abort_trapped, false),
  60. case {get_operations(ParentConfig), AbortTrapped} of
  61. {0, _} ->
  62. %% None of the commands had any effect
  63. ?FAIL;
  64. {_, true} ->
  65. %% An abort was previously trapped
  66. ?FAIL;
  67. _ ->
  68. ok
  69. end;
  70. process_commands([Command | Rest], ParentConfig) ->
  71. %% Reset skip dirs
  72. ParentConfig1 = rebar_config:reset_skip_dirs(ParentConfig),
  73. Operations = get_operations(ParentConfig1),
  74. ParentConfig4 =
  75. try
  76. %% Convert the code path so that all the entries are
  77. %% absolute paths. If not, code:set_path() may choke on
  78. %% invalid relative paths when trying to restore the code
  79. %% path from inside a subdirectory.
  80. true = rebar_utils:expand_code_path(),
  81. {ParentConfig2, _DirSet} = process_dir(rebar_utils:get_cwd(),
  82. Command, ParentConfig1,
  83. sets:new()),
  84. case get_operations(ParentConfig2) of
  85. Operations ->
  86. %% This command didn't do anything
  87. ?CONSOLE("Command '~p' not understood or not applicable~n",
  88. [Command]);
  89. _ ->
  90. ok
  91. end,
  92. %% TODO: reconsider after config inheritance removal/re-design
  93. ParentConfig3 = rebar_config:clean_config(ParentConfig1,
  94. ParentConfig2),
  95. %% Wipe out vsn cache to avoid invalid hits when
  96. %% dependencies are updated
  97. rebar_config:set_xconf(ParentConfig3, vsn_cache, dict:new())
  98. catch
  99. throw:rebar_abort ->
  100. case rebar_config:get_xconf(ParentConfig1, keep_going, false) of
  101. false ->
  102. ?FAIL;
  103. true ->
  104. ?WARN("Continuing on after abort: ~p\n", [Rest]),
  105. rebar_config:set_xconf(ParentConfig1,
  106. abort_trapped, true)
  107. end
  108. end,
  109. process_commands(Rest, ParentConfig4).
  110. process_dir(Dir, Command, ParentConfig, DirSet) ->
  111. case filelib:is_dir(Dir) of
  112. false ->
  113. ?WARN("Skipping non-existent sub-dir: ~p\n", [Dir]),
  114. {ParentConfig, DirSet};
  115. true ->
  116. WouldCd = would_cd_into_dir(Dir, Command, ParentConfig),
  117. ok = file:set_cwd(Dir),
  118. Config = maybe_load_local_config(Dir, ParentConfig),
  119. %% Save the current code path and then update it with
  120. %% lib_dirs. Children inherit parents code path, but we also
  121. %% want to ensure that we restore everything to pristine
  122. %% condition after processing this child
  123. CurrentCodePath = update_code_path(Config),
  124. %% Get the list of processing modules and check each one
  125. %% against CWD to see if it's a fit -- if it is, use that
  126. %% set of modules to process this dir.
  127. {ok, AvailModuleSets} = application:get_env(rebar, modules),
  128. ModuleSet = choose_module_set(AvailModuleSets, Dir),
  129. skip_or_process_dir(Dir, Command, Config, DirSet, CurrentCodePath,
  130. ModuleSet, WouldCd)
  131. end.
  132. would_cd_into_dir(Dir, Command, Config) ->
  133. case would_cd_into_dir1(Dir, Command, Config) of
  134. true ->
  135. would_cd;
  136. false ->
  137. would_not_cd
  138. end.
  139. would_cd_into_dir1(Dir, Command, Config) ->
  140. rebar_utils:processing_base_dir(Config, Dir) orelse
  141. rebar_config:is_recursive(Config) orelse
  142. is_recursive_command(Command, Config) orelse
  143. is_generate_in_rel_dir(Command, Dir).
  144. %% Check whether the command is part of the built-in (or extended via
  145. %% rebar.config) list of default-recursive commands.
  146. is_recursive_command(Command, Config) ->
  147. {ok, AppCmds} = application:get_env(rebar, recursive_cmds),
  148. ConfCmds = rebar_config:get_local(Config, recursive_cmds, []),
  149. RecursiveCmds = AppCmds ++ ConfCmds,
  150. lists:member(Command, RecursiveCmds).
  151. %% If the directory we're about to process contains
  152. %% reltool.config[.script] and the command to be applied is
  153. %% 'generate', then it's safe to process. We do this to retain the
  154. %% behavior of specifying {sub_dirs, ["rel"]} and have "rebar generate"
  155. %% pick up rel/reltool.config[.script]. Without this workaround you'd
  156. %% have to run "rebar -r generate" (which you don't want to do if you
  157. %% have deps or other sub_dirs) or "cd rel && rebar generate".
  158. is_generate_in_rel_dir(generate, Dir) ->
  159. case rebar_rel_utils:is_rel_dir(Dir) of
  160. {true, _} ->
  161. true;
  162. false ->
  163. false
  164. end;
  165. is_generate_in_rel_dir(_, _) ->
  166. false.
  167. skip_or_process_dir(Dir, Command, Config, DirSet, CurrentCodePath,
  168. {[], undefined}=ModuleSet, WouldCd) ->
  169. process_dir1(Dir, Command, Config, DirSet, CurrentCodePath, ModuleSet,
  170. WouldCd);
  171. skip_or_process_dir(Dir, Command, Config, DirSet, CurrentCodePath,
  172. {_, File}=ModuleSet, WouldCd) ->
  173. case lists:suffix(".app.src", File)
  174. orelse lists:suffix(".app", File) of
  175. true ->
  176. %% .app or .app.src file, check if is_skipped_app
  177. skip_or_process_dir1(Dir, Command, Config, DirSet, CurrentCodePath,
  178. ModuleSet, WouldCd, File);
  179. false ->
  180. %% not an app dir, no need to consider apps=/skip_apps=
  181. process_dir1(Dir, Command, Config, DirSet, CurrentCodePath,
  182. ModuleSet, WouldCd)
  183. end.
  184. skip_or_process_dir1(Dir, Command, Config, DirSet, CurrentCodePath, ModuleSet,
  185. WouldCd, AppFile) ->
  186. case rebar_app_utils:is_skipped_app(Config, AppFile) of
  187. {Config1, {true, _SkippedApp}} when Command == 'update-deps' ->
  188. %% update-deps does its own app skipping. Unfortunately there's no
  189. %% way to signal this to rebar_core, so we have to explicitly do it
  190. %% here... Otherwise if you use app=, it'll skip the toplevel
  191. %% directory and nothing will be updated.
  192. process_dir1(Dir, Command, Config1, DirSet, CurrentCodePath,
  193. ModuleSet, WouldCd);
  194. {Config1, {true, SkippedApp}} ->
  195. ?DEBUG("Skipping app: ~p~n", [SkippedApp]),
  196. {increment_operations(Config1), DirSet};
  197. {Config1, false} ->
  198. process_dir1(Dir, Command, Config1, DirSet, CurrentCodePath,
  199. ModuleSet, WouldCd)
  200. end.
  201. process_dir1(Dir, Command, Config, DirSet, CurrentCodePath,
  202. {DirModules, File}, WouldCd) ->
  203. Config0 = rebar_config:set_xconf(Config, current_command, Command),
  204. %% Get the list of modules for "any dir". This is a catch-all list
  205. %% of modules that are processed in addition to modules associated
  206. %% with this directory type. These any_dir modules are processed
  207. %% FIRST.
  208. {ok, AnyDirModules} = application:get_env(rebar, any_dir_modules),
  209. Modules = AnyDirModules ++ DirModules,
  210. %% Invoke 'preprocess' on the modules -- this yields a list of other
  211. %% directories that should be processed _before_ the current one.
  212. {Config1, Predirs} = acc_modules(Modules, preprocess, Config0, File),
  213. %% Remember associated pre-dirs (used for plugin lookup)
  214. PredirsAssoc = remember_cwd_predirs(Dir, Predirs),
  215. %% Get the list of plug-in modules from rebar.config. These
  216. %% modules may participate in preprocess and postprocess.
  217. {ok, PluginModules} = plugin_modules(Config1, PredirsAssoc),
  218. AllModules = Modules ++ PluginModules,
  219. {Config2, PluginPredirs} = acc_modules(PluginModules, preprocess, Config1,
  220. File),
  221. AllPredirs = Predirs ++ PluginPredirs,
  222. ?DEBUG("Predirs: ~p\n", [AllPredirs]),
  223. {Config3, DirSet2} = process_each(AllPredirs, Command, Config2, DirSet,
  224. File),
  225. %% Make sure the CWD is reset properly; processing the dirs may have
  226. %% caused it to change
  227. ok = file:set_cwd(Dir),
  228. %% Maybe apply command to Dir
  229. Config4 = maybe_execute(Dir, Command, Config3, Modules, PluginModules,
  230. AllModules, File, WouldCd),
  231. %% Mark the current directory as processed
  232. DirSet3 = sets:add_element(Dir, DirSet2),
  233. %% Invoke 'postprocess' on the modules. This yields a list of other
  234. %% directories that should be processed _after_ the current one.
  235. {Config5, Postdirs} = acc_modules(AllModules, postprocess, Config4, File),
  236. ?DEBUG("Postdirs: ~p\n", [Postdirs]),
  237. Res = process_each(Postdirs, Command, Config5, DirSet3, File),
  238. %% Make sure the CWD is reset properly; processing the dirs may have
  239. %% caused it to change
  240. ok = file:set_cwd(Dir),
  241. %% Once we're all done processing, reset the code path to whatever
  242. %% the parent initialized it to
  243. restore_code_path(CurrentCodePath),
  244. %% Return the updated {config, dirset} as result
  245. Res.
  246. maybe_execute(Dir, Command, Config, Modules, PluginModules, AllModules, File,
  247. would_cd) ->
  248. %% Check that this directory is not on the skip list
  249. case rebar_config:is_skip_dir(Config, Dir) of
  250. true ->
  251. %% Do not execute the command on the directory, as some
  252. %% module has requested a skip on it.
  253. ?INFO("Skipping ~s in ~s\n", [Command, Dir]),
  254. Config;
  255. false ->
  256. %% Check for and get command specific environments
  257. {Config1, Env} = setup_envs(Config, Modules),
  258. %% Execute any before_command plugins on this directory
  259. Config2 = execute_pre(Command, PluginModules, Config1, File, Env),
  260. %% Execute the current command on this directory
  261. Config3 = execute(Command, AllModules, Config2, File, Env),
  262. %% Execute any after_command plugins on this directory
  263. execute_post(Command, PluginModules, Config3, File, Env)
  264. end;
  265. maybe_execute(_Dir, _Command, Config, _Modules, _PluginModules, _AllModules,
  266. _File, would_not_cd) ->
  267. Config.
  268. remember_cwd_predirs(Cwd, Predirs) ->
  269. Store = fun(Dir, Dict) ->
  270. case dict:find(Dir, Dict) of
  271. error ->
  272. ?DEBUG("Associate sub_dir ~s with ~s~n",
  273. [Dir, Cwd]),
  274. dict:store(Dir, Cwd, Dict);
  275. {ok, Existing} ->
  276. ?ABORT("Internal consistency assertion failed.~n"
  277. "sub_dir ~s already associated with ~s.~n"
  278. "Duplicate sub_dirs or deps entries?",
  279. [Dir, Existing])
  280. end
  281. end,
  282. lists:foldl(Store, dict:new(), Predirs).
  283. maybe_load_local_config(Dir, ParentConfig) ->
  284. %% We need to ensure we don't overwrite custom
  285. %% config when we are dealing with base_dir.
  286. case rebar_utils:processing_base_dir(ParentConfig, Dir) of
  287. true ->
  288. ParentConfig;
  289. false ->
  290. rebar_config:new(ParentConfig)
  291. end.
  292. %%
  293. %% Given a list of directories and a set of previously processed directories,
  294. %% process each one we haven't seen yet
  295. %%
  296. process_each([], _Command, Config, DirSet, _File) ->
  297. %% reset cached (setup_env) envs
  298. Config1 = rebar_config:reset_envs(Config),
  299. {Config1, DirSet};
  300. process_each([Dir | Rest], Command, Config, DirSet, File) ->
  301. case sets:is_element(Dir, DirSet) of
  302. true ->
  303. ?DEBUG("Skipping ~s; already processed!\n", [Dir]),
  304. process_each(Rest, Command, Config, DirSet, File);
  305. false ->
  306. {Config1, DirSet2} = process_dir(Dir, Command, Config, DirSet),
  307. Config2 = rebar_config:clean_config(Config, Config1),
  308. %% reset cached (setup_env) envs
  309. Config3 = rebar_config:reset_envs(Config2),
  310. process_each(Rest, Command, Config3, DirSet2, File)
  311. end.
  312. %%
  313. %% Given a list of module sets from rebar.app and a directory, find
  314. %% the appropriate subset of modules for this directory
  315. %%
  316. choose_module_set([], _Dir) ->
  317. {[], undefined};
  318. choose_module_set([{Type, Modules} | Rest], Dir) ->
  319. case is_dir_type(Type, Dir) of
  320. {true, File} ->
  321. {Modules, File};
  322. false ->
  323. choose_module_set(Rest, Dir)
  324. end.
  325. is_dir_type(app_dir, Dir) ->
  326. rebar_app_utils:is_app_dir(Dir);
  327. is_dir_type(rel_dir, Dir) ->
  328. rebar_rel_utils:is_rel_dir(Dir);
  329. is_dir_type(_, _) ->
  330. false.
  331. execute_pre(Command, Modules, Config, ModuleFile, Env) ->
  332. execute_plugin_hook("pre_", Command, Modules,
  333. Config, ModuleFile, Env).
  334. execute_post(Command, Modules, Config, ModuleFile, Env) ->
  335. execute_plugin_hook("post_", Command, Modules,
  336. Config, ModuleFile, Env).
  337. execute_plugin_hook(Hook, Command, Modules, Config, ModuleFile, Env) ->
  338. HookFunction = list_to_atom(Hook ++ atom_to_list(Command)),
  339. execute(HookFunction, hook, Modules, Config, ModuleFile, Env).
  340. %%
  341. %% Execute a command across all applicable modules
  342. %%
  343. execute(Command, Modules, Config, ModuleFile, Env) ->
  344. execute(Command, not_a_hook, Modules, Config, ModuleFile, Env).
  345. execute(Command, Type, Modules, Config, ModuleFile, Env) ->
  346. case select_modules(Modules, Command, []) of
  347. [] ->
  348. case Type of
  349. hook ->
  350. ok;
  351. not_a_hook ->
  352. ?WARN("'~p' command does not apply to directory ~s\n",
  353. [Command, rebar_utils:get_cwd()])
  354. end,
  355. Config;
  356. TargetModules ->
  357. %% Provide some info on where we are
  358. Dir = rebar_utils:get_cwd(),
  359. ?CONSOLE("==> ~s (~s)\n", [filename:basename(Dir), Command]),
  360. Config1 = increment_operations(Config),
  361. %% Run the available modules
  362. apply_hooks(pre_hooks, Config1, Command, Env),
  363. case catch(run_modules(TargetModules, Command,
  364. Config1, ModuleFile)) of
  365. {ok, NewConfig} ->
  366. apply_hooks(post_hooks, NewConfig, Command, Env),
  367. NewConfig;
  368. {error, failed} ->
  369. ?FAIL;
  370. {Module, {error, _} = Other} ->
  371. ?ABORT("~p failed while processing ~s in module ~s: ~s\n",
  372. [Command, Dir, Module,
  373. io_lib:print(Other, 1, 80, -1)]);
  374. Other ->
  375. ?ABORT("~p failed while processing ~s: ~s\n",
  376. [Command, Dir, io_lib:print(Other, 1, 80, -1)])
  377. end
  378. end.
  379. %% Increment the count of operations, since some module
  380. %% responds to this command
  381. increment_operations(Config) ->
  382. Operations = get_operations(Config),
  383. rebar_config:set_xconf(Config, operations, Operations + 1).
  384. get_operations(Config) ->
  385. rebar_config:get_xconf(Config, operations).
  386. update_code_path(Config) ->
  387. case rebar_config:get_local(Config, lib_dirs, []) of
  388. [] ->
  389. no_change;
  390. Paths ->
  391. LibPaths = expand_lib_dirs(Paths, rebar_utils:get_cwd(), []),
  392. ok = code:add_pathsa(LibPaths),
  393. %% track just the paths we added, so we can remove them without
  394. %% removing other paths added by this dep
  395. {added, LibPaths}
  396. end.
  397. restore_code_path(no_change) ->
  398. ok;
  399. restore_code_path({added, Paths}) ->
  400. %% Verify that all of the paths still exist -- some dynamically
  401. %% added paths can get blown away during clean.
  402. _ = [code:del_path(F) || F <- Paths, erl_prim_loader_is_file(F)],
  403. ok.
  404. erl_prim_loader_is_file(File) ->
  405. erl_prim_loader:read_file_info(File) =/= error.
  406. expand_lib_dirs([], _Root, Acc) ->
  407. Acc;
  408. expand_lib_dirs([Dir | Rest], Root, Acc) ->
  409. Apps = filelib:wildcard(filename:join([Dir, "*", "ebin"])),
  410. FqApps = case filename:pathtype(Dir) of
  411. absolute -> Apps;
  412. _ -> [filename:join([Root, A]) || A <- Apps]
  413. end,
  414. expand_lib_dirs(Rest, Root, Acc ++ FqApps).
  415. select_modules([], _Command, Acc) ->
  416. lists:reverse(Acc);
  417. select_modules([Module | Rest], Command, Acc) ->
  418. {module, Module} = code:ensure_loaded(Module),
  419. case erlang:function_exported(Module, Command, 2) of
  420. true ->
  421. select_modules(Rest, Command, [Module | Acc]);
  422. false ->
  423. select_modules(Rest, Command, Acc)
  424. end.
  425. run_modules([], _Command, Config, _File) ->
  426. {ok, Config};
  427. run_modules([Module | Rest], Command, Config, File) ->
  428. case Module:Command(Config, File) of
  429. ok ->
  430. run_modules(Rest, Command, Config, File);
  431. {ok, NewConfig} ->
  432. run_modules(Rest, Command, NewConfig, File);
  433. {error, _} = Error ->
  434. {Module, Error}
  435. end.
  436. apply_hooks(Mode, Config, Command, Env0) ->
  437. Hooks = rebar_config:get_local(Config, Mode, []),
  438. Env = rebar_utils:patch_env(Config, Env0),
  439. lists:foreach(fun apply_hook/1,
  440. [{Env, Hook} || Hook <- Hooks,
  441. element(1, Hook) =:= Command orelse
  442. element(2, Hook) =:= Command]).
  443. apply_hook({Env, {Arch, Command, Hook}}) ->
  444. case rebar_utils:is_arch(Arch) of
  445. true ->
  446. apply_hook({Env, {Command, Hook}});
  447. false ->
  448. ok
  449. end;
  450. apply_hook({Env, {Command, Hook}}) ->
  451. Msg = lists:flatten(io_lib:format("Command [~p] failed!~n", [Command])),
  452. rebar_utils:sh(Hook, [{env, Env}, {abort_on_error, Msg}]).
  453. setup_envs(Config, Modules) ->
  454. lists:foldl(fun(M, {C,E}=T) ->
  455. case erlang:function_exported(M, setup_env, 1) of
  456. true ->
  457. Env = M:setup_env(C),
  458. C1 = rebar_config:save_env(C, M, Env),
  459. {C1, E++Env};
  460. false ->
  461. T
  462. end
  463. end, {Config, []}, Modules).
  464. acc_modules(Modules, Command, Config, File) ->
  465. acc_modules(select_modules(Modules, Command, []),
  466. Command, Config, File, []).
  467. acc_modules([], _Command, Config, _File, Acc) ->
  468. {Config, Acc};
  469. acc_modules([Module | Rest], Command, Config, File, Acc) ->
  470. {Config1, Dirs1} = case Module:Command(Config, File) of
  471. {ok, Dirs} ->
  472. {Config, Dirs};
  473. {ok, NewConfig, Dirs} ->
  474. {NewConfig, Dirs}
  475. end,
  476. acc_modules(Rest, Command, Config1, File, Acc ++ Dirs1).
  477. %%
  478. %% Return a flat list of rebar plugin modules.
  479. %%
  480. plugin_modules(Config, PredirsAssoc) ->
  481. Modules = lists:flatten(rebar_config:get_all(Config, plugins)),
  482. ?DEBUG("Plugins requested while processing ~s: ~p~n",
  483. [rebar_utils:get_cwd(), Modules]),
  484. plugin_modules(Config, PredirsAssoc, ulist(Modules)).
  485. ulist(L) ->
  486. ulist(L, []).
  487. ulist([], Acc) ->
  488. lists:reverse(Acc);
  489. ulist([H | T], Acc) ->
  490. case lists:member(H, Acc) of
  491. true ->
  492. ulist(T, Acc);
  493. false ->
  494. ulist(T, [H | Acc])
  495. end.
  496. plugin_modules(_Config, _PredirsAssoc, []) ->
  497. {ok, []};
  498. plugin_modules(Config, PredirsAssoc, Modules) ->
  499. FoundModules = [M || M <- Modules, code:which(M) =/= non_existing],
  500. plugin_modules(Config, PredirsAssoc, FoundModules, Modules -- FoundModules).
  501. plugin_modules(_Config, _PredirsAssoc, FoundModules, []) ->
  502. {ok, FoundModules};
  503. plugin_modules(Config, PredirsAssoc, FoundModules, MissingModules) ->
  504. {Loaded, NotLoaded} = load_plugin_modules(Config, PredirsAssoc,
  505. MissingModules),
  506. AllViablePlugins = FoundModules ++ Loaded,
  507. case NotLoaded =/= [] of
  508. true ->
  509. %% NB: we continue to ignore this situation, as did the
  510. %% original code
  511. ?WARN("Missing plugins: ~p\n", [NotLoaded]);
  512. false ->
  513. ?DEBUG("Loaded plugins: ~p~n", [AllViablePlugins]),
  514. ok
  515. end,
  516. {ok, AllViablePlugins}.
  517. load_plugin_modules(Config, PredirsAssoc, Modules) ->
  518. Cwd = rebar_utils:get_cwd(),
  519. PluginDirs = get_all_plugin_dirs(Config, Cwd, PredirsAssoc),
  520. ?DEBUG("Plugin dirs for ~s:~n~p~n", [Cwd, PluginDirs]),
  521. %% Find relevant sources in base_dir and plugin_dir
  522. Erls = string:join([atom_to_list(M)++"\\.erl" || M <- Modules], "|"),
  523. RE = "^" ++ Erls ++ "\$",
  524. %% If a plugin is found both in base_dir and plugin_dir, the clash
  525. %% will provoke an error and we'll abort.
  526. Sources = [rebar_utils:find_files(PD, RE, false) || PD <- PluginDirs],
  527. %% Compile and load plugins
  528. Loaded = [load_plugin(Src) || Src <- lists:append(Sources)],
  529. FilterMissing = is_missing_plugin(Loaded),
  530. NotLoaded = [V || V <- Modules, FilterMissing(V)],
  531. {Loaded, NotLoaded}.
  532. get_all_plugin_dirs(Config, Cwd, PredirsAssoc) ->
  533. [rebar_utils:get_cwd()]
  534. ++ get_plugin_dir(Config, Cwd)
  535. ++ get_base_plugin_dirs(Cwd, PredirsAssoc).
  536. get_plugin_dir(Config, Cwd) ->
  537. case rebar_config:get_local(Config, plugin_dir, undefined) of
  538. undefined ->
  539. %% Plugin can be in the project's "plugins" folder
  540. [filename:join(Cwd, "plugins")];
  541. Dir ->
  542. [Dir]
  543. end.
  544. %% We also want to include this case:
  545. %% Plugin can be in "plugins" directory of the plugin base directory.
  546. %% For example, Cwd depends on Plugin, and deps/Plugin/plugins/Plugin.erl
  547. %% is the plugin.
  548. get_base_plugin_dirs(Cwd, PredirsAssoc) ->
  549. [filename:join(Dir, "plugins") ||
  550. Dir <- get_plugin_base_dirs(Cwd, PredirsAssoc)].
  551. %% @doc PredirsAssoc is a dictionary of plugindir -> 'parent' pairs.
  552. %% 'parent' in this case depends on plugin; therefore we have to give
  553. %% all plugins that Cwd ('parent' in this case) depends on.
  554. get_plugin_base_dirs(Cwd, PredirsAssoc) ->
  555. [PluginDir || {PluginDir, Master} <- dict:to_list(PredirsAssoc),
  556. Master =:= Cwd].
  557. is_missing_plugin(Loaded) ->
  558. fun(Mod) -> not lists:member(Mod, Loaded) end.
  559. load_plugin(Src) ->
  560. case compile:file(Src, [binary, return_errors]) of
  561. {ok, Mod, Bin} ->
  562. load_plugin_module(Mod, Bin, Src);
  563. {error, Errors, _Warnings} ->
  564. ?ABORT("Plugin ~s contains compilation errors: ~p~n",
  565. [Src, Errors])
  566. end.
  567. load_plugin_module(Mod, Bin, Src) ->
  568. case code:is_loaded(Mod) of
  569. {file, Loaded} ->
  570. ?ABORT("Plugin ~p clashes with previously loaded module ~p~n",
  571. [Mod, Loaded]);
  572. false ->
  573. ?INFO("Loading plugin ~p from ~s~n", [Mod, Src]),
  574. {module, Mod} = code:load_binary(Mod, Src, Bin),
  575. Mod
  576. end.