From 424fdcb8fa40019fd06c51aa086504d03a8ae4ba Mon Sep 17 00:00:00 2001 From: Hakan Nilsson Date: Fri, 20 Nov 2020 21:59:48 +0100 Subject: [PATCH 1/4] Add support for reading relx config file --- src/rebar_relx.erl | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/rebar_relx.erl b/src/rebar_relx.erl index 5f079176..ee3bfae4 100644 --- a/src/rebar_relx.erl +++ b/src/rebar_relx.erl @@ -21,8 +21,7 @@ -spec do(atom(), rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}. do(Provider, State) -> {Opts, _} = rebar_state:command_parsed_args(State), - %% TODO: read in ./relx.config if it exists or --config value - RelxConfig = rebar_state:get(State, relx, []), + RelxConfig = read_relx_config(State, Opts), ProfileString = rebar_dir:profile_dir_name(State), ExtraOverlays = [{profile_string, ProfileString}], @@ -72,6 +71,33 @@ do(Provider, State) -> {ok, State}. +read_relx_config(State, Options) -> + ConfigFile = proplists:get_value(config, Options, []), + case ConfigFile of + "" -> + case {rebar_state:get(State, relx, []), file:consult("relx.config")} of + {[], {ok, Config}} -> + ?DEBUG("Using relx.config", []), + Config; + {Config, {error, enoent}} -> + ?DEBUG("Using relx config from rebar.config", []), + Config; + {_, {error, Reason}} -> + erlang:error(?PRV_ERROR({config_file, "relx.config", Reason})); + {RebarConfig, {ok, _RelxConfig}} -> + ?WARN("Found conflicting relx configs, using rebar.config", []), + RebarConfig + end; + ConfigFile -> + case file:consult(ConfigFile) of + {ok, Config} -> + ?DEBUG("Using relx config file: ~p", [ConfigFile]), + Config; + {error, Reason} -> + erlang:error(?PRV_ERROR({config_file, ConfigFile, Reason})) + end + end. + -spec format_error(any()) -> iolist(). format_error(unknown_release) -> "Option --relname is missing"; @@ -79,6 +105,8 @@ format_error(unknown_vsn) -> "Option --relvsn is missing"; format_error(all_relup) -> "Option --all can not be applied to `relup` command"; +format_error({config_file, Filename, Error}) -> + io_lib:format("Failed to read config file ~s: ~p", [Filename, Error]); format_error(Error) -> io_lib:format("~p", [Error]). From 2ae71b2b88c5320aa4bdc6b74abdb10d3111499f Mon Sep 17 00:00:00 2001 From: Hakan Nilsson Date: Sat, 21 Nov 2020 00:33:13 +0100 Subject: [PATCH 2/4] Add tests for relx config file --- test/rebar_release_SUITE.erl | 29 +++++++++++++++++++++++++++++ test/rebar_test_utils.erl | 1 + 2 files changed, 30 insertions(+) diff --git a/test/rebar_release_SUITE.erl b/test/rebar_release_SUITE.erl index dd1d4085..5560a35e 100644 --- a/test/rebar_release_SUITE.erl +++ b/test/rebar_release_SUITE.erl @@ -4,6 +4,7 @@ -include_lib("eunit/include/eunit.hrl"). all() -> [release, + config_file, dev_mode_release, profile_dev_mode_override_release, tar, @@ -45,6 +46,34 @@ release(Config) -> {ok, [{release, list_to_atom(Name), Vsn, false}]} ). +config_file(Config) -> + AppDir = ?config(apps, Config), + Name = list_to_atom(?config(name, Config)), + %% Relase build fails if no relx config exists + ?assertError({error, {relx, no_releases_in_system}}, + rebar_test_utils:run_and_check(Config, [], ["release"], result)), + %% Write relx.config + RelxConfig = fun(Vsn) -> [{release, {Name, Vsn}, [Name]}, {lib_dirs, [AppDir]}] end, + rebar_test_utils:create_config(AppDir, "relx.config", RelxConfig("1.0.0")), + %% Release is built with relx.config (default) + rebar_test_utils:run_and_check(Config, [], ["release"], + {ok, [{release, Name, "1.0.0", false}]}), + %% Release is built with custom.config (--config) + rebar_test_utils:create_config(AppDir, "custom.config", RelxConfig("2.0.0")), + rebar_test_utils:run_and_check(Config, [], ["release", "--config", "custom.config"], + {ok, [{release, Name, "2.0.0", false}]}), + %% Fail due to non-existing file + ?assertError({error, {rebar_relx, {config_file, "no_exist.config", enoent}}}, + rebar_test_utils:run_and_check(Config, [], + ["release", "--config", "no_exist.config"], result)), + %% Fail due to non-existing file, even with relx config in rebar.config + ?assertError({error, {rebar_relx, {config_file, "no_exist.config", enoent}}}, + rebar_test_utils:run_and_check(Config, [{relx, RelxConfig("3.0.0")}], + ["release", "--config", "no_exist.config"], result)), + %% rebar.config overrides relx.config if both exist + rebar_test_utils:run_and_check(Config, [{relx, RelxConfig("4.0.0")}], ["release"], + {ok, [{release, Name, "4.0.0", false}]}). + dev_mode_release(Config) -> AppDir = ?config(apps, Config), Name = ?config(name, Config), diff --git a/test/rebar_test_utils.erl b/test/rebar_test_utils.erl index 6e79f679..a0ec1506 100644 --- a/test/rebar_test_utils.erl +++ b/test/rebar_test_utils.erl @@ -431,6 +431,7 @@ check_results(AppDir, Expected, ProfileRun) -> end, DevMode = lists:all(IsSymLinkFun, RelLibs), ?assertEqual(ExpectedDevMode, DevMode), + ?assert(ec_file:exists(filename:join([ReleaseDir, Name, "releases", Vsn]))), %% throws not_found if it doesn't exist ok From 96d61cf5f80a8917b66c73eb41a8ec4fd1773f2e Mon Sep 17 00:00:00 2001 From: Hakan Nilsson Date: Mon, 23 Nov 2020 15:43:02 +0100 Subject: [PATCH 3/4] Improved logging --- src/rebar_relx.erl | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/rebar_relx.erl b/src/rebar_relx.erl index ee3bfae4..4cc935aa 100644 --- a/src/rebar_relx.erl +++ b/src/rebar_relx.erl @@ -77,21 +77,23 @@ read_relx_config(State, Options) -> "" -> case {rebar_state:get(State, relx, []), file:consult("relx.config")} of {[], {ok, Config}} -> - ?DEBUG("Using relx.config", []), + ?DEBUG("Configuring releases with relx.config", []), Config; {Config, {error, enoent}} -> - ?DEBUG("Using relx config from rebar.config", []), + ?DEBUG("Configuring releases the {relx, ...} entry" + " from rebar.config", []), Config; {_, {error, Reason}} -> erlang:error(?PRV_ERROR({config_file, "relx.config", Reason})); {RebarConfig, {ok, _RelxConfig}} -> - ?WARN("Found conflicting relx configs, using rebar.config", []), + ?WARN("Found conflicting relx configs, configuring releases" + " with rebar.config", []), RebarConfig end; ConfigFile -> case file:consult(ConfigFile) of {ok, Config} -> - ?DEBUG("Using relx config file: ~p", [ConfigFile]), + ?DEBUG("Configuring releases with: ~ts", [ConfigFile]), Config; {error, Reason} -> erlang:error(?PRV_ERROR({config_file, ConfigFile, Reason})) @@ -106,7 +108,7 @@ format_error(unknown_vsn) -> format_error(all_relup) -> "Option --all can not be applied to `relup` command"; format_error({config_file, Filename, Error}) -> - io_lib:format("Failed to read config file ~s: ~p", [Filename, Error]); + io_lib:format("Failed to read config file ~ts: ~p", [Filename, Error]); format_error(Error) -> io_lib:format("~p", [Error]). From 7884a4b109af1d11dcf887e391a99f189aaf165e Mon Sep 17 00:00:00 2001 From: Hakan Nilsson Date: Mon, 23 Nov 2020 15:43:21 +0100 Subject: [PATCH 4/4] Use root dir when loading relx.config --- src/rebar_relx.erl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/rebar_relx.erl b/src/rebar_relx.erl index 4cc935aa..af4104f3 100644 --- a/src/rebar_relx.erl +++ b/src/rebar_relx.erl @@ -75,7 +75,8 @@ read_relx_config(State, Options) -> ConfigFile = proplists:get_value(config, Options, []), case ConfigFile of "" -> - case {rebar_state:get(State, relx, []), file:consult("relx.config")} of + ConfigPath = filename:join([rebar_dir:root_dir(State), "relx.config"]), + case {rebar_state:get(State, relx, []), file:consult(ConfigPath)} of {[], {ok, Config}} -> ?DEBUG("Configuring releases with relx.config", []), Config;