From c8ef29dcc896a30ab30ca9e14adfb3923d5bc389 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Fri, 24 Jul 2009 18:42:25 +0100 Subject: cosmetic --- src/rabbit_plugin_activator.erl | 254 ++++++++++++++++++++-------------------- 1 file changed, 126 insertions(+), 128 deletions(-) diff --git a/src/rabbit_plugin_activator.erl b/src/rabbit_plugin_activator.erl index 34df9c48..a7311882 100644 --- a/src/rabbit_plugin_activator.erl +++ b/src/rabbit_plugin_activator.erl @@ -37,91 +37,88 @@ -define(DefaultUnpackedPluginDir, "priv/plugins"). -define(DefaultRabbitEBin, "ebin"). -define(BaseApps, [rabbit]). --ifdef(enable_debug). --define(Debug(F, V), io:format("DEBUG: " ++ F, V)). --endif. --ifndef(enable_debug). --define(Debug(_F, _V), ok). --endif. -define(Error(F, V), io:format("ERROR: " ++ F, V)). %%---------------------------------------------------------------------------- start() -> - % Ensure Rabbit is loaded so we can access it's environment - application:load(rabbit), - - % Determine our various directories - PluginDir = get_env_or_default(plugins_dir, ?DefaultPluginDir), - UnpackedPluginDir = get_env_or_default(plugins_expand_dir, ?DefaultUnpackedPluginDir), - RabbitEBin = get_env_or_default(rabbit_ebin, ?DefaultRabbitEBin), - ?Debug("Got Plugin Dir ~p~n", [PluginDir]), - ?Debug("Got Unpacked Plugin Dir ~p~n", [UnpackedPluginDir]), - ?Debug("Got Rabbit EBin Dir ~p~n", [RabbitEBin]), - - % Unpack any .ez plugins - unpack_ez_plugins(PluginDir, UnpackedPluginDir), - - % Build a list of required apps based on the fixed set, and any plugins - RequiredApps = ?BaseApps ++ find_plugins(PluginDir) ++ find_plugins(UnpackedPluginDir), - - % Build the entire set of dependencies - this will load the applications along the way - AllApps = case catch sets:to_list(expand_dependencies(RequiredApps)) of - {unknown_app, _} -> - ?Error("Failed to expand dependencies.~n", []), - halt(1); - AppList -> - AppList - end, - AppVersions = [determine_version(App) || App <- AllApps], - {value, {rabbit, RabbitVersion}} = lists:keysearch(rabbit, 1, AppVersions), - - % Build the overall release descriptor - RDesc = {release, - {"rabbit", RabbitVersion}, - {erts, erlang:system_info(version)}, - AppVersions}, - - % Write it out to ebin/rabbit.rel - ?Debug("Generated rabbit.rel: ~p.~n", [RDesc]), - file:write_file(RabbitEBin ++ "/rabbit.rel", io_lib:format("~p.~n", [RDesc])), - - % Compile the script - case systools:make_script(RabbitEBin ++ "/rabbit", [local, silent]) of - {ok, Module, Warnings} -> - % This gets lots of spurious no-source warnings when we have .ez files, so we want to supress them to prevent - % hiding real issues. - WarningStr = Module:format_warning([W || W <- Warnings, - case W of - {warning, {source_not_found, _}} -> false; - _ -> true - end]), - case length(WarningStr) of - 0 -> ok; - _ -> io:format("~s", [WarningStr]) - end, - ok; - {error, Module, Error} -> - io:format("Boot file generation failed: ~s~n", [Module:format_error(Error)]), - halt(1) - end, - halt(), - ok. - + %% Ensure Rabbit is loaded so we can access it's environment + application:load(rabbit), + + %% Determine our various directories + PluginDir = get_env(plugins_dir, ?DefaultPluginDir), + UnpackedPluginDir = get_env(plugins_expand_dir, ?DefaultUnpackedPluginDir), + RabbitEBin = get_env(rabbit_ebin, ?DefaultRabbitEBin), + + %% Unpack any .ez plugins + unpack_ez_plugins(PluginDir, UnpackedPluginDir), + + %% Build a list of required apps based on the fixed set, and any plugins + RequiredApps = ?BaseApps ++ + find_plugins(PluginDir) ++ + find_plugins(UnpackedPluginDir), + + %% Build the entire set of dependencies - this will load the + %% applications along the way + AllApps = case catch sets:to_list(expand_dependencies(RequiredApps)) of + {unknown_app, _} -> + ?Error("Failed to expand dependencies.~n", []), + halt(1); + AppList -> + AppList + end, + AppVersions = [determine_version(App) || App <- AllApps], + {value, {rabbit, RabbitVersion}} = lists:keysearch(rabbit, 1, AppVersions), + + %% Build the overall release descriptor + RDesc = {release, + {"rabbit", RabbitVersion}, + {erts, erlang:system_info(version)}, + AppVersions}, + + %% Write it out to ebin/rabbit.rel + file:write_file(RabbitEBin ++ "/rabbit.rel", + io_lib:format("~p.~n", [RDesc])), + + %% Compile the script + case systools:make_script(RabbitEBin ++ "/rabbit", [local, silent]) of + {ok, Module, Warnings} -> + %% This gets lots of spurious no-source warnings when we + %% have .ez files, so we want to supress them to prevent + %% hiding real issues. + WarningStr = Module:format_warning( + [W || W <- Warnings, + case W of + {warning, {source_not_found, _}} -> false; + _ -> true + end]), + case length(WarningStr) of + 0 -> ok; + _ -> io:format("~s", [WarningStr]) + end, + ok; + {error, Module, Error} -> + io:format("Boot file generation failed: ~s~n", + [Module:format_error(Error)]), + halt(1) + end, + halt(), + ok. + stop() -> - ok. - -get_env_or_default(Key, Default) -> - case application:get_env(rabbit, Key) of - {ok, V} -> V; - _ -> Default - end. - + ok. + +get_env(Key, Default) -> + case application:get_env(rabbit, Key) of + {ok, V} -> V; + _ -> Default + end. + determine_version(App) -> - application:load(App), - {ok, Vsn} = application:get_key(App, vsn), - {App, Vsn}. - + application:load(App), + {ok, Vsn} = application:get_key(App, vsn), + {App, Vsn}. + assert_dir(Dir) -> case filelib:is_dir(Dir) of true -> ok; @@ -130,63 +127,64 @@ assert_dir(Dir) -> ok = file:make_dir(Dir) end. delete_dir(Dir) -> - case filelib:is_dir(Dir) of - true -> - case file:list_dir(Dir) of - {ok, Files} -> - Paths = [Dir ++ "/" ++ F || F <- Files], - [delete_dir(F) || F <- Paths, filelib:is_dir(F)], - [file:delete(F) || F <- Paths, filelib:is_file(F)] - end, - ok = file:del_dir(Dir); - false -> - ok - end. + case filelib:is_dir(Dir) of + true -> + case file:list_dir(Dir) of + {ok, Files} -> + Paths = [Dir ++ "/" ++ F || F <- Files], + [delete_dir(F) || F <- Paths, filelib:is_dir(F)], + [file:delete(F) || F <- Paths, filelib:is_file(F)] + end, + ok = file:del_dir(Dir); + false -> + ok + end. unpack_ez_plugins(PluginSrcDir, PluginDestDir) -> - % Eliminate the contents of the destination directory - delete_dir(PluginDestDir), - - assert_dir(PluginDestDir), - [unpack_ez_plugin(PluginName, PluginDestDir) || PluginName <- filelib:wildcard(PluginSrcDir ++ "/*.ez")]. - -unpack_ez_plugin(PluginFn, PluginDestDir) -> - zip:unzip(PluginFn, [{cwd, PluginDestDir}]), - ok. + %% Eliminate the contents of the destination directory + delete_dir(PluginDestDir), + assert_dir(PluginDestDir), + [unpack_ez_plugin(PluginName, PluginDestDir) || + PluginName <- filelib:wildcard(PluginSrcDir ++ "/*.ez")]. + +unpack_ez_plugin(PluginFn, PluginDestDir) -> + zip:unzip(PluginFn, [{cwd, PluginDestDir}]), + ok. find_plugins(PluginDir) -> - [prepare_dir_plugin(PluginName) || PluginName <- filelib:wildcard(PluginDir ++ "/*/ebin/*.app")]. - + [prepare_dir_plugin(PluginName) || + PluginName <- filelib:wildcard(PluginDir ++ "/*/ebin/*.app")]. + prepare_dir_plugin(PluginAppDescFn) -> - % Add the plugin ebin directory to the load path - PluginEBinDirN = filename:dirname(PluginAppDescFn), - code:add_path(PluginEBinDirN), - - % We want the second-last token - NameTokens = string:tokens(PluginAppDescFn,"/."), - PluginNameString = lists:nth(length(NameTokens) - 1, NameTokens), - list_to_atom(PluginNameString). + %% Add the plugin ebin directory to the load path + PluginEBinDirN = filename:dirname(PluginAppDescFn), + code:add_path(PluginEBinDirN), + + %% We want the second-last token + NameTokens = string:tokens(PluginAppDescFn,"/."), + PluginNameString = lists:nth(length(NameTokens) - 1, NameTokens), + list_to_atom(PluginNameString). expand_dependencies(Pending) -> - expand_dependencies(sets:new(), Pending). + expand_dependencies(sets:new(), Pending). expand_dependencies(Current, []) -> - Current; + Current; expand_dependencies(Current, [Next|Rest]) -> - case sets:is_element(Next, Current) of - true -> - expand_dependencies(Current, Rest); - false -> - case application:load(Next) of - ok -> - ok; - {error, {already_loaded, _}} -> - ok; - X -> - ?Error("Failed to load ~s: ~p~n", [Next, X]), - throw({unknown_app, Next}) - end, - {ok, Required} = application:get_key(Next, applications), - Unique = [A || A <- Required, not(sets:is_element(A, Current))], - expand_dependencies(sets:add_element(Next, Current), Rest ++ Unique) - end. + case sets:is_element(Next, Current) of + true -> + expand_dependencies(Current, Rest); + false -> + case application:load(Next) of + ok -> + ok; + {error, {already_loaded, _}} -> + ok; + X -> + ?Error("Failed to load ~s: ~p~n", [Next, X]), + throw({unknown_app, Next}) + end, + {ok, Required} = application:get_key(Next, applications), + Unique = [A || A <- Required, not(sets:is_element(A, Current))], + expand_dependencies(sets:add_element(Next, Current), Rest ++ Unique) + end. -- cgit v1.2.1