new plugin mechanism
This commit is contained in:
parent
3aef962a07
commit
9b71f9f6c0
|
@ -70,10 +70,10 @@ stop_plugins(Names) ->
|
||||||
%% @doc List all available plugins
|
%% @doc List all available plugins
|
||||||
-spec(list() -> [mqtt_plugin()]).
|
-spec(list() -> [mqtt_plugin()]).
|
||||||
list() ->
|
list() ->
|
||||||
case env(plugins_dir) of
|
case env(plugins_etc) of
|
||||||
{ok, PluginsDir} ->
|
{ok, PluginsEtc} ->
|
||||||
AppFiles = filelib:wildcard("*/ebin/*.app", PluginsDir),
|
CfgFiles = filelib:wildcard("*.config", PluginsEtc),
|
||||||
Plugins = [plugin(PluginsDir, AppFile) || AppFile <- AppFiles],
|
Plugins = [plugin(PluginsEtc, CfgFile) || CfgFile <- CfgFiles],
|
||||||
StartedApps = names(started_app),
|
StartedApps = names(started_app),
|
||||||
lists:map(fun(Plugin = #mqtt_plugin{name = Name}) ->
|
lists:map(fun(Plugin = #mqtt_plugin{name = Name}) ->
|
||||||
case lists:member(Name, StartedApps) of
|
case lists:member(Name, StartedApps) of
|
||||||
|
@ -85,21 +85,13 @@ list() ->
|
||||||
[]
|
[]
|
||||||
end.
|
end.
|
||||||
|
|
||||||
plugin(PluginsDir, AppFile0) ->
|
plugin(PluginsEtc, CfgFile0) ->
|
||||||
AppFile = filename:join(PluginsDir, AppFile0),
|
CfgFile = filename:join(PluginsEtc, CfgFile0),
|
||||||
{ok, [{application, Name, Attrs}]} = file:consult(AppFile),
|
{ok, [[{AppName, AppEnv} | _]]} = file:consult(CfgFile),
|
||||||
CfgFile = filename:join([PluginsDir, Name, "etc/plugin.config"]),
|
{ok, Attrs} = application:get_all_key(AppName),
|
||||||
AppsEnv1 =
|
|
||||||
case filelib:is_file(CfgFile) of
|
|
||||||
true ->
|
|
||||||
{ok, [AppsEnv]} = file:consult(CfgFile),
|
|
||||||
AppsEnv;
|
|
||||||
false ->
|
|
||||||
[]
|
|
||||||
end,
|
|
||||||
Ver = proplists:get_value(vsn, Attrs, "0"),
|
Ver = proplists:get_value(vsn, Attrs, "0"),
|
||||||
Descr = proplists:get_value(description, Attrs, ""),
|
Descr = proplists:get_value(description, Attrs, ""),
|
||||||
#mqtt_plugin{name = Name, version = Ver, config = AppsEnv1, descr = Descr}.
|
#mqtt_plugin{name = AppName, version = Ver, config = AppEnv, descr = Descr}.
|
||||||
|
|
||||||
%% @doc Load a Plugin
|
%% @doc Load a Plugin
|
||||||
-spec(load(atom()) -> ok | {error, any()}).
|
-spec(load(atom()) -> ok | {error, any()}).
|
||||||
|
@ -126,23 +118,16 @@ load_plugin(#mqtt_plugin{name = Name, config = Config}, Persistent) ->
|
||||||
{error, Error}
|
{error, Error}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
load_app(App, Config) ->
|
load_app(App, _Config) ->
|
||||||
case application:load(App) of
|
case application:load(App) of
|
||||||
ok ->
|
ok ->
|
||||||
set_config(Config);
|
ok;
|
||||||
{error, {already_loaded, App}} ->
|
{error, {already_loaded, App}} ->
|
||||||
set_config(Config);
|
ok;
|
||||||
{error, Error} ->
|
{error, Error} ->
|
||||||
{error, Error}
|
{error, Error}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% This trick is awesome:)
|
|
||||||
set_config([]) ->
|
|
||||||
ok;
|
|
||||||
set_config([{AppName, Envs} | Config]) ->
|
|
||||||
[application:set_env(AppName, Par, Val) || {Par, Val} <- Envs],
|
|
||||||
set_config(Config).
|
|
||||||
|
|
||||||
start_app(App, SuccFun) ->
|
start_app(App, SuccFun) ->
|
||||||
case application:ensure_all_started(App) of
|
case application:ensure_all_started(App) of
|
||||||
{ok, Started} ->
|
{ok, Started} ->
|
||||||
|
|
Loading…
Reference in New Issue