diff options
author | Tristan Sloughter <[email protected]> | 2019-08-19 10:05:23 -0600 |
---|---|---|
committer | GitHub <[email protected]> | 2019-08-19 10:05:23 -0600 |
commit | 6ba95994c5c070a774cd248286e2b4b4e21274d0 (patch) | |
tree | 83f79b829f53b241d5742d79bc6d28f3dd8e0848 | |
parent | def78e1c07c2ae79b8ba5293365a466e79a88b15 (diff) | |
parent | 460645219de1fea56e2d6885ec1ce355232c63e4 (diff) | |
download | relx-6ba95994c5c070a774cd248286e2b4b4e21274d0.tar.gz relx-6ba95994c5c070a774cd248286e2b4b4e21274d0.tar.bz2 relx-6ba95994c5c070a774cd248286e2b4b4e21274d0.zip |
Merge pull request #749 from g-andrade/feature/api-caller-custom-overlay-vars
Allow for API callers to define arbitrary overlay variables
-rw-r--r-- | src/rlx_cmd_args.erl | 3 | ||||
-rw-r--r-- | src/rlx_prv_overlay.erl | 9 | ||||
-rw-r--r-- | src/rlx_state.erl | 8 | ||||
-rw-r--r-- | test/rlx_release_SUITE.erl | 43 | ||||
-rw-r--r-- | test/rlx_test_utils.erl | 3 |
5 files changed, 58 insertions, 8 deletions
diff --git a/src/rlx_cmd_args.erl b/src/rlx_cmd_args.erl index 4f5e9da..1dc10c1 100644 --- a/src/rlx_cmd_args.erl +++ b/src/rlx_cmd_args.erl @@ -298,6 +298,9 @@ create(add_providers, Opts) -> create(providers, Opts) -> Providers = proplists:get_value(providers, Opts, []), {providers, Providers}; +create(api_caller_overlays, Opts) -> + ApiCallerOverlays = proplists:get_value(api_caller_overlays, Opts, []), + {api_caller_overlays, ApiCallerOverlays}; create(_, _) -> []. diff --git a/src/rlx_prv_overlay.erl b/src/rlx_prv_overlay.erl index 75608b3..ec8a97f 100644 --- a/src/rlx_prv_overlay.erl +++ b/src/rlx_prv_overlay.erl @@ -152,10 +152,15 @@ get_overlay_vars_from_file(State, OverlayVars) -> -spec read_overlay_vars(rlx_state:t(), proplists:proplist(), [file:name()]) -> proplists:proplist() | relx:error(). read_overlay_vars(State, OverlayVars, FileNames) -> + ApiCallerVars = rlx_state:api_caller_overlays(State), Terms = merge_overlay_vars(State, FileNames), - case render_overlay_vars(OverlayVars, Terms, []) of + case render_overlay_vars(OverlayVars ++ ApiCallerVars, Terms, []) of {ok, NewTerms} -> - OverlayVars ++ NewTerms; + % We place `ApiCallerVars' at the end on purpose; their + % definitions should be overwrittenable by both internal + % and rendered vars, as not to change behaviour in + % setups preceding the support for API caller overlays. + OverlayVars ++ NewTerms ++ ApiCallerVars; Error -> Error end. diff --git a/src/rlx_state.erl b/src/rlx_state.erl index cab55f6..9f9d835 100644 --- a/src/rlx_state.erl +++ b/src/rlx_state.erl @@ -44,6 +44,7 @@ goals/2, config_file/1, config_file/2, + api_caller_overlays/1, cli_args/1, cli_args/2, providers/1, @@ -104,6 +105,7 @@ output_dir :: file:name(), lib_dirs=[] :: [file:name()], config_file=[] :: file:filename() | undefined, + api_caller_overlays=[] :: [{atom(),term()}], cli_args=[] :: proplists:proplist(), goals=[] :: [rlx_depsolver:raw_constraint()], providers=[] :: [providers:t()], @@ -160,8 +162,10 @@ new(Config, CommandLineConfig, Targets) Log = proplists:get_value( log, CommandLineConfig, ec_cmd_log:new(error, Caller, rlx_util:intensity())), + ApiCallerOverlays = proplists:get_value(api_caller_overlays, CommandLineConfig, []), State0 = #state_t{log=Log, config_file=Config, + api_caller_overlays=ApiCallerOverlays, cli_args=CommandLineConfig, actions=Targets, caller=Caller, @@ -270,6 +274,10 @@ config_file(#state_t{config_file=ConfigFiles}) -> config_file(State, ConfigFiles) -> State#state_t{config_file=ConfigFiles}. +-spec api_caller_overlays(t()) -> [{atom(),term()}]. +api_caller_overlays(#state_t{api_caller_overlays = ApiCallerOverlays}) -> + ApiCallerOverlays. + -spec cli_args(t()) -> proplists:proplist(). cli_args(#state_t{cli_args=CliArgs}) -> CliArgs. diff --git a/test/rlx_release_SUITE.erl b/test/rlx_release_SUITE.erl index d7340de..a2aab71 100644 --- a/test/rlx_release_SUITE.erl +++ b/test/rlx_release_SUITE.erl @@ -818,12 +818,14 @@ overlay_release(Config) -> goal_app_2]}]), VarsFile1 = filename:join([LibDir1, "vars1.config"]), - %% tpl_var is defined in vars1, but redifined in vars2 using template. + %% `tpl_var' is defined in vars1, but redifined in vars2 using template. + %% `api_caller_var' is to be injected as an API caller overlay var. rlx_test_utils:write_config(VarsFile1, [{yahoo, "yahoo"}, {yahoo2, [{foo, "bar"}]}, {foo_yahoo, "foo_{{yahoo}}"}, {foo_dir, "foodir"}, - {tpl_var, "defined in vars1"}]), + {tpl_var, "defined in vars1"}, + {api_caller_var, "{{api_caller_var}}"}]), VarsFile2 = filename:join([LibDir1, "vars2.config"]), rlx_test_utils:write_config(VarsFile2, [{google, "yahoo"}, @@ -851,8 +853,25 @@ overlay_release(Config) -> OutputDir = filename:join([proplists:get_value(priv_dir, Config), rlx_test_utils:create_random_name("relx-output")]), - {ok, State} = relx:do(undefined, undefined, [], [LibDir1], 3, - OutputDir, ConfigFile), + ApiCallerVarValue = "api-caller-var", + ApiCallerReleaseNameValue = "release-var-conflict", + ApiCallerConfigFileValue = "state-var-conflict", + ApiCallerYahooValue = "overlays-file-conflict", + ApiCallerOverlays = + [{api_caller_var, ApiCallerVarValue}, + {release_name, ApiCallerReleaseNameValue}, + {config_file, ApiCallerConfigFileValue}, + {yahoo, ApiCallerYahooValue}], + + {ok, State} = relx:do([{relname, undefined}, + {relvsn, undefined}, + {goals, []}, + {lib_dirs, [LibDir1]}, + {log_level, 3}, + {output_dir, OutputDir}, + {config, ConfigFile}, + {api_caller_overlays, ApiCallerOverlays}], + ["release"]), [{{foo, "0.0.1"}, Release}] = ec_dictionary:to_list(rlx_state:realized_releases(State)), AppSpecs = rlx_release:applications(Release), @@ -889,6 +908,8 @@ overlay_release(Config) -> proplists:get_value(release_version, TemplateData)), ?assertEqual(foo, proplists:get_value(release_name, TemplateData)), + ?assertMatch(ConfigFile, + proplists:get_value(config_file, TemplateData)), ?assertEqual("yahoo/foo4", proplists:get_value(yahoo4, TemplateData)), ?assertEqual("foo_yahoo", @@ -902,7 +923,19 @@ overlay_release(Config) -> %% This should be rendered correctly based on VarsFile2 file, regardless %% of tpl_var defined in VarsFile1 or not. ?assertEqual("Redefined in vars2 with a template value", - proplists:get_value(tpl_var, TemplateData)). + proplists:get_value(tpl_var, TemplateData)), + + ?assertEqual(ApiCallerVarValue, + proplists:get_value(api_caller_var, TemplateData)), + % The following complements a few of the assertions above + % by ensuring that the values of the overwritten API caller + % overlays were distinct from their internal values. + ?assertNotEqual(ApiCallerReleaseNameValue, + proplists:get_value(release_name, TemplateData)), + ?assertNotEqual(ApiCallerConfigFileValue, + proplists:get_value(config_file, TemplateData)), + ?assertNotEqual(ApiCallerYahooValue, + proplists:get_value(yahoo, TemplateData)). make_goalless_release(Config) -> LibDir1 = proplists:get_value(lib1, Config), diff --git a/test/rlx_test_utils.erl b/test/rlx_test_utils.erl index 5328559..2c5f7af 100644 --- a/test/rlx_test_utils.erl +++ b/test/rlx_test_utils.erl @@ -186,7 +186,8 @@ test_template_contents() -> "{google, \"{{google}}\"}.\n" "{prop1, \"{{prop1}}\"}.\n" "{prop2, {{prop2}}}.\n" - "{tpl_var, \"{{tpl_var}}\"}.\n". + "{tpl_var, \"{{tpl_var}}\"}.\n" + "{api_caller_var, \"{{api_caller_var}}\"}.\n". escript_contents() -> "#!/usr/bin/env escript\n" |