diff options
author | Luis Rascao <[email protected]> | 2015-01-30 17:31:59 +0000 |
---|---|---|
committer | Luis Rascao <[email protected]> | 2015-01-30 17:31:59 +0000 |
commit | 7c116cbb2f296e38f7eeda0959af4488e5a2f6e6 (patch) | |
tree | b06d1bf1ab27d8769fc15c62a5f0310539f10fe8 /src/rlx_config.erl | |
parent | e6e3b8d74e0c5d0c3eedc74f256ec7749c9309a0 (diff) | |
download | relx-7c116cbb2f296e38f7eeda0959af4488e5a2f6e6.tar.gz relx-7c116cbb2f296e38f7eeda0959af4488e5a2f6e6.tar.bz2 relx-7c116cbb2f296e38f7eeda0959af4488e5a2f6e6.zip |
add config script funcionality
similar to rebar's config script, append a .script
suffix to the provided relx config that is evaluated
and can possibly change values in the original config
Diffstat (limited to 'src/rlx_config.erl')
-rw-r--r-- | src/rlx_config.erl | 55 |
1 files changed, 40 insertions, 15 deletions
diff --git a/src/rlx_config.erl b/src/rlx_config.erl index dd416ee..045bf33 100644 --- a/src/rlx_config.erl +++ b/src/rlx_config.erl @@ -97,25 +97,50 @@ parent_dir([_H], Acc) -> parent_dir([H | T], Acc) -> parent_dir(T, [H | Acc]). +-spec config_script_file(file:filename(), rlx_state:t()) -> file:filename(). +config_script_file(ConfigFile, _State) -> + ConfigFile ++ ".script". + +bs(Vars) -> + lists:foldl(fun({K,V}, Bs) -> + erl_eval:add_binding(K, V, Bs) + end, erl_eval:new_bindings(), Vars). + +-spec apply_config_script(proplists:proplist(), file:filename()) -> + proplists:proplist(). +apply_config_script(ConfigData, ConfigScriptFile) -> + {ok, Config} = file:script(ConfigScriptFile, bs([{'CONFIG', ConfigData}, + {'SCRIPT', ConfigScriptFile}])), + Config. + -spec load_config(file:filename() | proplists:proplist(), rlx_state:t()) -> {ok, rlx_state:t()} | relx:error(). load_config(ConfigFile, State) -> {ok, CurrentCwd} = file:get_cwd(), - case filelib:is_regular(ConfigFile) of - true -> - ok = file:set_cwd(filename:dirname(ConfigFile)), - Result = case file:consult(ConfigFile) of - {error, Reason} -> - ?RLX_ERROR({consult, ConfigFile, Reason}); - {ok, Terms} -> - CliTerms = rlx_state:cli_args(State), - lists:foldl(fun load_terms/2, {ok, State}, merge_configs(CliTerms, Terms)) - end, - ok = file:set_cwd(CurrentCwd), - Result; - false -> - CliTerms = rlx_state:cli_args(State), - lists:foldl(fun load_terms/2, {ok, State}, merge_configs(CliTerms, ConfigFile)) + CliTerms = rlx_state:cli_args(State), + Config0 = case filelib:is_regular(ConfigFile) of + true -> + ok = file:set_cwd(filename:dirname(ConfigFile)), + Result = case file:consult(ConfigFile) of + {error, Reason} -> + ?RLX_ERROR({consult, ConfigFile, Reason}); + {ok, Terms} -> merge_configs(CliTerms, Terms) + end, + ok = file:set_cwd(CurrentCwd), + Result; + false -> merge_configs(CliTerms, ConfigFile) + end, + % we now take the merged config and try to apply a config script to it, + % get a new config as a result + case Config0 of + {error, _} = Error -> Error; + _ -> + ConfigScriptFile = config_script_file(ConfigFile, State), + Config1 = case filelib:is_regular(ConfigScriptFile) of + false -> Config0; + true -> apply_config_script(Config0, ConfigScriptFile) + end, + lists:foldl(fun load_terms/2, {ok, State}, Config1) end. -spec load_terms(term(), {ok, rlx_state:t()} | relx:error()) -> |