Skip to content

Instantly share code, notes, and snippets.

@cararemixed
Created September 21, 2015 22:12
Show Gist options
  • Select an option

  • Save cararemixed/c0f35e088b7aeca07e72 to your computer and use it in GitHub Desktop.

Select an option

Save cararemixed/c0f35e088b7aeca07e72 to your computer and use it in GitHub Desktop.
Advanced rebar3 configuration loader
%% Add some helpful errors to our configuration loading script to
%% make troubleshooting common problems easier.
Consult = fun (Name) ->
rebar_api:debug("Consulting ~p", [Name]),
case file:consult(Name) of
{error, enoent} ->
rebar_api:error("Error: missing file ~s", [Name]),
erlang:halt(1);
{error, {Line, erl_parse, Problem}} ->
{ok, Bin} = file:read_file(Name),
Lines = re:split(Bin, "\n", [{return, list}]),
Start = max(0, Line - 4),
Focus = string:join(lists:sublist(Lines, Start, 5), "\n"),
rebar_api:error(
"Failed to parse ~s~n"
" ~s (line ~B)~n>>>>~n"
"~s~n<<<<~n"
" Probably missing or incorrect punctuation?~n",
[
Name, Problem, Line, Focus
]),
erlang:halt(1);
{error, Error} ->
rebar_api:error("Failed to pase ~s:~n ~p", [Name, Error]),
erlang:halt(1);
Result ->
Result
end
end.
{ok, RelxConfig} = Consult("config/relx.config").
LoadEnv = fun (Name) ->
{ok, Config} = Consult("config/environments/" ++ Name),
{list_to_atom(filename:basename(Name, ".config")), Config}
end,
{ok, EnvConfigFiles} = file:list_dir("config/environments"),
ProfileConfigs = lists:map(LoadEnv, EnvConfigFiles).
LoadOverride = fun (Name) ->
{ok, Config} = Consult("config/overrides/" ++ Name),
{override, list_to_atom(filename:basename(Name, ".config")), Config}
end,
{ok, OverrideConfigFiles} = file:list_dir("config/overrides"),
OverrideConfigs = lists:map(LoadOverride, OverrideConfigFiles).
[
{relx, RelxConfig},
{profiles, ProfileConfigs},
{overrides, OverrideConfigs}
%% include everything from rebar.config
| CONFIG
].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment