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]).