Fix for destruction of config app vars on reset

This commit is contained in:
Jeremy Raymond 2012-02-11 13:26:09 -05:00 committed by Tuncer Ayaz
parent 1a613d6123
commit 7e84634b8f

View file

@ -555,13 +555,48 @@ reconstruct_app_env_vars([App|Apps]) ->
_ -> _ ->
[] []
end, end,
AllVars = CmdVars ++ AppVars,
%% App vars specified in config files override those in the .app file.
%% Config files later in the args list override earlier ones.
AppVars1 = case init:get_argument(config) of
{ok, ConfigFiles} ->
{App, MergedAppVars} = lists:foldl(fun merge_app_vars/2,
{App, AppVars},
ConfigFiles),
MergedAppVars;
error ->
AppVars
end,
AllVars = CmdVars ++ AppVars1,
?DEBUG("Reconstruct ~p ~p\n", [App, AllVars]), ?DEBUG("Reconstruct ~p ~p\n", [App, AllVars]),
lists:foreach(fun({K, V}) -> application:set_env(App, K, V) end, AllVars), lists:foreach(fun({K, V}) -> application:set_env(App, K, V) end, AllVars),
reconstruct_app_env_vars(Apps); reconstruct_app_env_vars(Apps);
reconstruct_app_env_vars([]) -> reconstruct_app_env_vars([]) ->
ok. ok.
merge_app_vars(ConfigFile, {App, AppVars}) ->
File = ensure_config_extension(ConfigFile),
FileAppVars = app_vars_from_config_file(File, App),
Dict1 = dict:from_list(AppVars),
Dict2 = dict:from_list(FileAppVars),
Dict3 = dict:merge(fun(_Key, _Value1, Value2) -> Value2 end, Dict1, Dict2),
{App, dict:to_list(Dict3)}.
ensure_config_extension(File) ->
%% config files must end with .config on disk but when specifying them
%% via the -config option the extension is optional
BaseFileName = filename:basename(File, ".config"),
DirName = filename:dirname(File),
filename:join(DirName, BaseFileName ++ ".config").
app_vars_from_config_file(File, App) ->
case file:consult(File) of
{ok, [Env]} ->
proplists:get_value(App, Env, []);
_ ->
[]
end.
wait_until_dead(Pid) when is_pid(Pid) -> wait_until_dead(Pid) when is_pid(Pid) ->
Ref = erlang:monitor(process, Pid), Ref = erlang:monitor(process, Pid),
receive receive