Skip to content

Commit

Permalink
Fix loading of local plugins in sub directories
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuncer Ayaz committed Feb 3, 2012
1 parent 6898eff commit b7fb2ae
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions src/rebar_core.erl
Expand Up @@ -166,9 +166,11 @@ process_dir0(Dir, Command, DirSet, Config, CurrentCodePath,
%% directories that should be processed _before_ the current one. %% directories that should be processed _before_ the current one.
Predirs = acc_modules(Modules, preprocess, Config, ModuleSetFile), Predirs = acc_modules(Modules, preprocess, Config, ModuleSetFile),


SubdirAssoc = remember_cwd_subdir(Dir, Predirs),

%% Get the list of plug-in modules from rebar.config. These %% Get the list of plug-in modules from rebar.config. These
%% modules may participate in preprocess and postprocess. %% modules may participate in preprocess and postprocess.
{ok, PluginModules} = plugin_modules(Config), {ok, PluginModules} = plugin_modules(Config, SubdirAssoc),


PluginPredirs = acc_modules(PluginModules, preprocess, PluginPredirs = acc_modules(PluginModules, preprocess,
Config, ModuleSetFile), Config, ModuleSetFile),
Expand Down Expand Up @@ -226,6 +228,20 @@ process_dir0(Dir, Command, DirSet, Config, CurrentCodePath,
%% Return the updated dirset as our result %% Return the updated dirset as our result
DirSet4. DirSet4.


remember_cwd_subdir(Cwd, Subdirs) ->
Store = fun(Dir, Dict) ->
case dict:find(Dir, Dict) of
error ->
?DEBUG("Associate sub_dir ~s with ~s~n", [Dir, Cwd]),
dict:store(Dir, Cwd, Dict);
{ok, Existing} ->
?ABORT("sub_dir ~s already associated with ~s~n",
[Dir, Existing]),
Dict
end
end,
lists:foldl(Store, dict:new(), Subdirs).

maybe_load_local_config(Dir, ParentConfig) -> maybe_load_local_config(Dir, ParentConfig) ->
%% We need to ensure we don't overwrite custom %% We need to ensure we don't overwrite custom
%% config when we are dealing with base_dir. %% config when we are dealing with base_dir.
Expand Down Expand Up @@ -426,9 +442,9 @@ acc_modules([Module | Rest], Command, Config, File, Acc) ->
%% %%
%% Return a flat list of rebar plugin modules. %% Return a flat list of rebar plugin modules.
%% %%
plugin_modules(Config) -> plugin_modules(Config, SubdirAssoc) ->
Modules = lists:flatten(rebar_config:get_all(Config, plugins)), Modules = lists:flatten(rebar_config:get_all(Config, plugins)),
plugin_modules(Config, ulist(Modules)). plugin_modules(Config, SubdirAssoc, ulist(Modules)).


ulist(L) -> ulist(L) ->
ulist(L, []). ulist(L, []).
Expand All @@ -443,16 +459,16 @@ ulist([H | T], Acc) ->
ulist(T, [H | Acc]) ulist(T, [H | Acc])
end. end.


plugin_modules(_Config, []) -> plugin_modules(_Config, _SubdirAssoc, []) ->
{ok, []}; {ok, []};
plugin_modules(Config, Modules) -> plugin_modules(Config, SubdirAssoc, Modules) ->
FoundModules = [M || M <- Modules, code:which(M) =/= non_existing], FoundModules = [M || M <- Modules, code:which(M) =/= non_existing],
plugin_modules(Config, FoundModules, Modules -- FoundModules). plugin_modules(Config, SubdirAssoc, FoundModules, Modules -- FoundModules).


plugin_modules(_Config, FoundModules, []) -> plugin_modules(_Config, _SubdirAssoc, FoundModules, []) ->
{ok, FoundModules}; {ok, FoundModules};
plugin_modules(Config, FoundModules, MissingModules) -> plugin_modules(Config, SubdirAssoc, FoundModules, MissingModules) ->
{Loaded, NotLoaded} = load_plugin_modules(Config, MissingModules), {Loaded, NotLoaded} = load_plugin_modules(Config, SubdirAssoc, MissingModules),
AllViablePlugins = FoundModules ++ Loaded, AllViablePlugins = FoundModules ++ Loaded,
case NotLoaded =/= [] of case NotLoaded =/= [] of
true -> true ->
Expand All @@ -464,19 +480,20 @@ plugin_modules(Config, FoundModules, MissingModules) ->
end, end,
{ok, AllViablePlugins}. {ok, AllViablePlugins}.


load_plugin_modules(Config, Modules) -> load_plugin_modules(Config, SubdirAssoc, Modules) ->
Cwd = rebar_utils:get_cwd(),
PluginDir = case rebar_config:get_local(Config, plugin_dir, undefined) of PluginDir = case rebar_config:get_local(Config, plugin_dir, undefined) of
undefined -> undefined ->
filename:join(rebar_utils:get_cwd(), "plugins"); filename:join(Cwd, "plugins");
Dir -> Dir ->
Dir Dir
end, end,


%% Find relevant sources in base_dir and plugin_dir %% Find relevant sources in base_dir and plugin_dir
Erls = string:join([atom_to_list(M)++"\\.erl" || M <- Modules], "|"), Erls = string:join([atom_to_list(M)++"\\.erl" || M <- Modules], "|"),
RE = "^" ++ Erls ++ "\$", RE = "^" ++ Erls ++ "\$",
BaseDir = rebar_config:get_global(base_dir, []), BaseDir = get_plugin_base_dir(Cwd, SubdirAssoc),
%% If a plugin is found in base_dir and plugin_dir the clash %% If a plugin is found both in base_dir and plugin_dir, the clash
%% will provoke an error and we'll abort. %% will provoke an error and we'll abort.
Sources = rebar_utils:find_files(PluginDir, RE, false) Sources = rebar_utils:find_files(PluginDir, RE, false)
++ rebar_utils:find_files(BaseDir, RE, false), ++ rebar_utils:find_files(BaseDir, RE, false),
Expand All @@ -487,6 +504,14 @@ load_plugin_modules(Config, Modules) ->
NotLoaded = [V || V <- Modules, FilterMissing(V)], NotLoaded = [V || V <- Modules, FilterMissing(V)],
{Loaded, NotLoaded}. {Loaded, NotLoaded}.


get_plugin_base_dir(Cwd, SubdirAssoc) ->
case dict:find(Cwd, SubdirAssoc) of
{ok, BaseDir} ->
BaseDir;
error ->
Cwd
end.

is_missing_plugin(Loaded) -> is_missing_plugin(Loaded) ->
fun(Mod) -> not lists:member(Mod, Loaded) end. fun(Mod) -> not lists:member(Mod, Loaded) end.


Expand Down

0 comments on commit b7fb2ae

Please sign in to comment.