Merge pull request #90 from Motiejus/dep_plugin

Fix searching for plugins
This commit is contained in:
Dave Smith 2013-05-21 05:22:43 -07:00
commit 638569acc2
5 changed files with 82 additions and 17 deletions

View file

@ -0,0 +1,50 @@
%%% @doc Plugin handling test
%%%
%%% This test checks if plugins are loaded correctly.
%%%
%%% It has three applications:
%%% <ol>
%%% <li>fish. top-level module, has one dependency: `dependsonplugin'.</li>
%%% <li>dependsonplugin. This depends on some pre-compile actions by the
%%% plugin. In the test the plugin creates a file `pre.compile' in the
%%% top-level folder of this application.</li>
%%% <li>testplugin. This is a plugin application which creates the file.</li>
%%% </ol>
-module(depplugins_rt).
-compile(export_all).
-include_lib("eunit/include/eunit.hrl").
files() ->
[
{copy, "../../rebar", "rebar"},
{copy, "rebar.config", "rebar.config"},
{create, "ebin/fish.app", app(fish, [])},
{create, "deps/dependsonplugin/ebin/dependsonplugin.app",
app(dependsonplugin, [])},
{copy, "rebar_dependsonplugin.config",
"deps/dependsonplugin/rebar.config"},
{copy, "testplugin_mod.erl",
"deps/testplugin/plugins/testplugin_mod.erl"},
{create, "deps/testplugin/ebin/testplugin.app",
app(testplugin, [])}
].
run(_Dir) ->
?assertMatch({ok, _}, retest_sh:run("./rebar compile", [])),
?assertEqual(true, filelib:is_regular("deps/dependsonplugin/pre.compile")),
ok.
%%
%% Generate the contents of a simple .app file
%%
app(Name, Modules) ->
App = {application, Name,
[{description, atom_to_list(Name)},
{vsn, "1"},
{modules, Modules},
{registered, []},
{applications, [kernel, stdlib]}]},
io_lib:format("~p.\n", [App]).

View file

@ -0,0 +1 @@
{deps, [dependsonplugin]}.

View file

@ -0,0 +1,2 @@
{deps, [testplugin]}.
{plugins, [testplugin_mod]}.

View file

@ -0,0 +1,6 @@
-module(testplugin_mod).
-compile(export_all).
pre_compile(Config, _) ->
ok = file:write_file("pre.compile", <<"Yadda!">>),
rebar_log:log(info, "Wrote ~p/pre.compile~n", [rebar_utils:get_cwd()]).

View file

@ -530,35 +530,41 @@ plugin_modules(Config, PredirsAssoc, FoundModules, MissingModules) ->
load_plugin_modules(Config, PredirsAssoc, Modules) -> load_plugin_modules(Config, PredirsAssoc, Modules) ->
Cwd = rebar_utils:get_cwd(), Cwd = rebar_utils:get_cwd(),
PluginDir = case rebar_config:get_local(Config, plugin_dir, undefined) of PluginDirs = case rebar_config:get_local(Config, plugin_dir, undefined) of
undefined -> undefined ->
filename:join(Cwd, "plugins"); % Plugin can be in the project's "plugins" folder
[filename:join(Cwd, "plugins")];
Dir -> Dir ->
Dir [Dir]
end, end ++
% We also want to include this case:
% Plugin can be in "plugins" directory of the plugin base directory. For
% example, Cwd depends on Plugin, and deps/Plugin/plugins/Plugin.erl is the
% plugin.
[
filename:join(Dir, "plugins") ||
Dir <- get_plugin_base_dirs(Cwd, PredirsAssoc)
],
%% 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 = get_plugin_base_dir(Cwd, PredirsAssoc),
%% If a plugin is found both 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(PD, RE, false) || PD <- PluginDirs],
++ rebar_utils:find_files(BaseDir, RE, false),
%% Compile and load plugins %% Compile and load plugins
Loaded = [load_plugin(Src) || Src <- Sources], Loaded = [load_plugin(Src) || Src <- lists:append(Sources)],
FilterMissing = is_missing_plugin(Loaded), FilterMissing = is_missing_plugin(Loaded),
NotLoaded = [V || V <- Modules, FilterMissing(V)], NotLoaded = [V || V <- Modules, FilterMissing(V)],
{Loaded, NotLoaded}. {Loaded, NotLoaded}.
get_plugin_base_dir(Cwd, PredirsAssoc) -> %% @doc PredirsAssoc is a dictionary of plugindir -> 'parent' pairs
case dict:find(Cwd, PredirsAssoc) of %% 'parent' in this case depends on plugin; therefore we have to give
{ok, BaseDir} -> %% all plugins that Cwd ('parent' in this case) depends on.
BaseDir; get_plugin_base_dirs(Cwd, PredirsAssoc) ->
error -> [PluginDir || {PluginDir, Master} <- dict:to_list(PredirsAssoc),
Cwd Master =:= 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.