diff options
-rw-r--r-- | src/rlx_config.erl | 55 | ||||
-rw-r--r-- | test/rlx_release_SUITE.erl | 58 |
2 files changed, 96 insertions, 17 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()) -> diff --git a/test/rlx_release_SUITE.erl b/test/rlx_release_SUITE.erl index ce7d5de..f123e79 100644 --- a/test/rlx_release_SUITE.erl +++ b/test/rlx_release_SUITE.erl @@ -40,7 +40,8 @@ make_relup_release/1, make_relup_release2/1, make_one_app_top_level_release/1, - make_dev_mode_release/1]). + make_dev_mode_release/1, + make_config_script_release/1]). -include_lib("common_test/include/ct.hrl"). -include_lib("eunit/include/eunit.hrl"). @@ -71,7 +72,8 @@ all() -> make_implicit_config_release, make_rerun_overridden_release, overlay_release, make_goalless_release, make_depfree_release, make_invalid_config_release, make_relup_release, make_relup_release2, - make_one_app_top_level_release, make_dev_mode_release]. + make_one_app_top_level_release, make_dev_mode_release, + make_config_script_release]. make_release(Config) -> LibDir1 = proplists:get_value(lib1, Config), @@ -935,6 +937,58 @@ make_dev_mode_release(Config) -> ?assert(ec_file:is_symlink(filename:join([OutputDir, "foo", "releases", "0.0.1", "vm.args"]))). +make_config_script_release(Config) -> + LibDir1 = proplists:get_value(lib1, Config), + FooRoot = filename:join([LibDir1, "foodir1", "foodir2"]), + filelib:ensure_dir(filename:join([FooRoot, "tmp"])), + [(fun({Name, Vsn}) -> + rlx_test_utils:create_app(LibDir1, Name, Vsn, [kernel, stdlib], []) + end)(App) + || + App <- + [{rlx_test_utils:create_random_name("lib_app1_"), rlx_test_utils:create_random_vsn()} + || _ <- lists:seq(1, 100)]], + + rlx_test_utils:create_app(LibDir1, "goal_app_1", "0.0.1", [stdlib,kernel,non_goal_1], []), + rlx_test_utils:create_app(LibDir1, "lib_dep_1", "0.0.1", [stdlib,kernel], []), + rlx_test_utils:create_app(LibDir1, "goal_app_2", "0.0.1", [stdlib,kernel,goal_app_1,non_goal_2], []), + rlx_test_utils:create_app(LibDir1, "non_goal_1", "0.0.1", [stdlib,kernel], [lib_dep_1]), + rlx_test_utils:create_app(LibDir1, "non_goal_2", "0.0.1", [stdlib,kernel], []), + + ConfigFile = filename:join([LibDir1, "relx.config"]), + rlx_test_utils:write_config(ConfigFile, + [{release, {foo, "0.0.1"}, + [goal_app_1, + goal_app_2]}]), + ConfigScriptFile = filename:join([LibDir1, "relx.config.script"]), + ok = file:write_file(ConfigScriptFile, + "case os:getenv(\"RELX_TEST\") of\n" + " \"true\" ->\n" + " {release, {RelName, Version}, Apps} = lists:keyfind(release, 1, CONFIG),\n" + " lists:keyreplace(release, 1, CONFIG, {release, {RelName, \"0.0.2\"}, Apps});\n" + " _ -> CONFIG % env var not defined or anything other than true\n" + "end.\n"), + + OutputDir = filename:join([proplists:get_value(data_dir, Config), + rlx_test_utils:create_random_name("relx-output")]), + ok = file:set_cwd(FooRoot), + {ok, FooRoot} = file:get_cwd(), + + % set the env var that will cause relx.config to be altered by the config script + os:putenv("RELX_TEST", "true"), + + {ok, State} = relx:do(undefined, undefined, [], [LibDir1], 3, + OutputDir, undefined), + [{{foo, "0.0.2"}, Release}] = ec_dictionary:to_list(rlx_state:realized_releases(State)), + ?assert(ec_file:exists(OutputDir)), + AppSpecs = rlx_release:applications(Release), + ?assert(lists:keymember(stdlib, 1, AppSpecs)), + ?assert(lists:keymember(kernel, 1, AppSpecs)), + ?assert(lists:member({non_goal_1, "0.0.1"}, AppSpecs)), + ?assert(lists:member({non_goal_2, "0.0.1"}, AppSpecs)), + ?assert(lists:member({goal_app_1, "0.0.1"}, AppSpecs)), + ?assert(lists:member({goal_app_2, "0.0.1"}, AppSpecs)), + ?assert(lists:member({lib_dep_1, "0.0.1", load}, AppSpecs)). %%%=================================================================== %%% Helper Functions |