Browse Source

print and format error message for bad .app files and all bad configs

pull/470/head
Tristan Sloughter 10 years ago
parent
commit
8528204431
7 changed files with 51 additions and 58 deletions
  1. +15
    -19
      src/rebar_app_discover.erl
  2. +1
    -6
      src/rebar_app_info.erl
  3. +2
    -12
      src/rebar_config.erl
  4. +17
    -1
      src/rebar_file_utils.erl
  5. +8
    -3
      src/rebar_prv_app_discovery.erl
  6. +2
    -2
      src/rebar_prv_dialyzer.erl
  7. +6
    -15
      src/rebar_prv_shell.erl

+ 15
- 19
src/rebar_app_discover.erl View File

@ -182,25 +182,21 @@ app_dir(AppFile) ->
-spec create_app_info(file:name(), file:name()) -> rebar_app_info:t() | {error, term()}. -spec create_app_info(file:name(), file:name()) -> rebar_app_info:t() | {error, term()}.
create_app_info(AppDir, AppFile) -> create_app_info(AppDir, AppFile) ->
case file:consult(AppFile) of
{ok, [{application, AppName, AppDetails}]} ->
AppVsn = proplists:get_value(vsn, AppDetails),
Applications = proplists:get_value(applications, AppDetails, []),
IncludedApplications = proplists:get_value(included_applications, AppDetails, []),
{ok, AppInfo} = rebar_app_info:new(AppName, AppVsn, AppDir, []),
AppInfo1 = rebar_app_info:applications(
rebar_app_info:app_details(AppInfo, AppDetails),
IncludedApplications++Applications),
Valid = case rebar_app_utils:validate_application_info(AppInfo1) of
true ->
true;
_ ->
false
end,
rebar_app_info:dir(rebar_app_info:valid(AppInfo1, Valid), AppDir);
{error, Reason} ->
{error, Reason}
end.
[{application, AppName, AppDetails}] = rebar_file_utils:try_consult(AppFile),
AppVsn = proplists:get_value(vsn, AppDetails),
Applications = proplists:get_value(applications, AppDetails, []),
IncludedApplications = proplists:get_value(included_applications, AppDetails, []),
{ok, AppInfo} = rebar_app_info:new(AppName, AppVsn, AppDir, []),
AppInfo1 = rebar_app_info:applications(
rebar_app_info:app_details(AppInfo, AppDetails),
IncludedApplications++Applications),
Valid = case rebar_app_utils:validate_application_info(AppInfo1) of
true ->
true;
_ ->
false
end,
rebar_app_info:dir(rebar_app_info:valid(AppInfo1, Valid), AppDir).
dedup([]) -> []; dedup([]) -> [];
dedup([A]) -> [A]; dedup([A]) -> [A];

+ 1
- 6
src/rebar_app_info.erl View File

@ -167,12 +167,7 @@ app_details(AppInfo=#app_info_t{app_details=[]}) ->
File -> File ->
File File
end, end,
case file:consult(AppFile) of
{ok, [{application, _, AppDetails}]} ->
AppDetails;
_ ->
[]
end;
rebar_file_utils:try_consult(AppFile);
app_details(#app_info_t{app_details=AppDetails}) -> app_details(#app_info_t{app_details=AppDetails}) ->
AppDetails. AppDetails.

+ 2
- 12
src/rebar_config.erl View File

@ -57,7 +57,7 @@ consult_file(File) ->
{ok, Terms} = consult_and_eval(File, Script), {ok, Terms} = consult_and_eval(File, Script),
Terms; Terms;
false -> false ->
try_consult(File)
rebar_file_utils:try_consult(File)
end end
end. end.
@ -87,22 +87,12 @@ format_error({bad_dep_name, Dep}) ->
consult_and_eval(File, Script) -> consult_and_eval(File, Script) ->
?DEBUG("Evaluating config script ~p", [Script]), ?DEBUG("Evaluating config script ~p", [Script]),
StateData = try_consult(File),
StateData = rebar_file_utils:try_consult(File),
file:script(Script, bs([{'CONFIG', StateData}, {'SCRIPT', Script}])). file:script(Script, bs([{'CONFIG', StateData}, {'SCRIPT', Script}])).
remove_script_ext(F) -> remove_script_ext(F) ->
filename:rootname(F, ".script"). filename:rootname(F, ".script").
try_consult(File) ->
case file:consult(File) of
{ok, Terms} ->
Terms;
{error, enoent} ->
[];
{error, Reason} ->
?ABORT("Failed to read config file ~s:~n ~p", [File, Reason])
end.
bs(Vars) -> bs(Vars) ->
lists:foldl(fun({K,V}, Bs) -> lists:foldl(fun({K,V}, Bs) ->
erl_eval:add_binding(K, V, Bs) erl_eval:add_binding(K, V, Bs)

+ 17
- 1
src/rebar_file_utils.erl View File

@ -26,7 +26,9 @@
%% ------------------------------------------------------------------- %% -------------------------------------------------------------------
-module(rebar_file_utils). -module(rebar_file_utils).
-export([symlink_or_copy/2,
-export([try_consult/1,
format_error/1,
symlink_or_copy/2,
rm_rf/1, rm_rf/1,
cp_r/2, cp_r/2,
mv/2, mv/2,
@ -37,11 +39,25 @@
reset_dir/1]). reset_dir/1]).
-include("rebar.hrl"). -include("rebar.hrl").
-include_lib("providers/include/providers.hrl").
%% =================================================================== %% ===================================================================
%% Public API %% Public API
%% =================================================================== %% ===================================================================
try_consult(File) ->
case file:consult(File) of
{ok, Terms} ->
Terms;
{error, enoent} ->
[];
{error, Reason} ->
throw(?PRV_ERROR({bad_term_file, File, Reason}))
end.
format_error({bad_term_file, AppFile, Reason}) ->
io_lib:format("Error reading file ~s: ~s", [AppFile, file:format_error(Reason)]).
symlink_or_copy(Source, Target) -> symlink_or_copy(Source, Target) ->
Link = case os:type() of Link = case os:type() of
{win32, _} -> {win32, _} ->

+ 8
- 3
src/rebar_prv_app_discovery.erl View File

@ -38,7 +38,7 @@ do(State) ->
State1 = rebar_app_discover:do(State, LibDirs), State1 = rebar_app_discover:do(State, LibDirs),
{ok, State1} {ok, State1}
catch catch
throw:{error, Error}->
throw:{error, Error} ->
?PRV_ERROR(Error) ?PRV_ERROR(Error)
end. end.
@ -46,12 +46,17 @@ do(State) ->
format_error({multiple_app_files, Files}) -> format_error({multiple_app_files, Files}) ->
io_lib:format("Multiple app files found in one app dir: ~s", [string:join(Files, " and ")]); io_lib:format("Multiple app files found in one app dir: ~s", [string:join(Files, " and ")]);
format_error({invalid_app_file, File, Reason}) -> format_error({invalid_app_file, File, Reason}) ->
case Reason of
case Reason of
{Line, erl_parse, Description} -> {Line, erl_parse, Description} ->
io_lib:format("Invalid app file ~s at line ~b: ~p",
io_lib:format("Invalid app file ~s at line ~b: ~p",
[File, Line, lists:flatten(Description)]); [File, Line, lists:flatten(Description)]);
_ -> _ ->
io_lib:format("Invalid app file ~s: ~p", [File, Reason]) io_lib:format("Invalid app file ~s: ~p", [File, Reason])
end; end;
%% Provide a slightly more informative error message for consult of app file failure
format_error({rebar_file_utils, {bad_term_file, AppFile, Reason}}) ->
io_lib:format("Error in app file ~s: ~s", [rebar_dir:make_relative_path(AppFile,
rebar_dir:get_cwd()),
file:format_error(Reason)]);
format_error(Reason) -> format_error(Reason) ->
io_lib:format("~p", [Reason]). io_lib:format("~p", [Reason]).

+ 2
- 2
src/rebar_prv_dialyzer.erl View File

@ -237,8 +237,8 @@ ebin_to_info(EbinDir, AppName) ->
{IncApps ++ DepApps, Files, Warnings}; {IncApps ++ DepApps, Files, Warnings};
{error, enoent} when AppName =:= erts -> {error, enoent} when AppName =:= erts ->
{[], ebin_files(EbinDir), []}; {[], ebin_files(EbinDir), []};
_ ->
Error = io_lib:format("Could not parse ~p", [AppFile]),
{error, Reason} ->
Error = io_lib:format("Could not parse ~s: ~p", [AppFile, file:format_error(Reason)]),
throw({dialyzer_error, Error}) throw({dialyzer_error, Error})
end. end.

+ 6
- 15
src/rebar_prv_shell.erl View File

@ -136,20 +136,18 @@ reread_config(State) ->
case find_config(State) of case find_config(State) of
no_config -> no_config ->
ok; ok;
{ok, ConfigList} ->
ConfigList ->
lists:foreach(fun ({Application, Items}) -> lists:foreach(fun ({Application, Items}) ->
lists:foreach(fun ({Key, Val}) -> lists:foreach(fun ({Key, Val}) ->
application:set_env(Application, Key, Val) application:set_env(Application, Key, Val)
end, end,
Items) Items)
end, end,
ConfigList);
{error, Error} ->
?ABORT("Error while attempting to read configuration file: ~p", [Error])
ConfigList)
end. end.
% First try the --config flag, then try the relx sys_config % First try the --config flag, then try the relx sys_config
-spec find_config(rebar_state:t()) -> {ok, [tuple()] class="p">}|no_config|{error, tuple()}.
-spec find_config(rebar_state:t()) -> [tuple()] | no_config.
find_config(State) -> find_config(State) ->
case find_config_option(State) of case find_config_option(State) of
no_config -> no_config ->
@ -158,7 +156,7 @@ find_config(State) ->
Result Result
end. end.
-spec find_config_option(rebar_state:t()) -> {ok, [tuple()] class="p">}|no_config|{error, tuple()}.
-spec find_config_option(rebar_state:t()) -> [tuple()] | no_config.
find_config_option(State) -> find_config_option(State) ->
{Opts, _} = rebar_state:command_parsed_args(State), {Opts, _} = rebar_state:command_parsed_args(State),
case proplists:get_value(config, Opts) of case proplists:get_value(config, Opts) of
@ -168,7 +166,7 @@ find_config_option(State) ->
consult_config(State, Filename) consult_config(State, Filename)
end. end.
-spec find_config_relx(rebar_state:t()) -> {ok, [tuple()] class="p">}|no_config|{error, tuple()}.
-spec find_config_relx(rebar_state:t()) -> [tuple()] | no_config.
find_config_relx(State) -> find_config_relx(State) ->
case proplists:get_value(sys_config, rebar_state:get(State, relx, [])) of case proplists:get_value(sys_config, rebar_state:get(State, relx, [])) of
undefined -> undefined ->
@ -181,11 +179,4 @@ find_config_relx(State) ->
consult_config(State, Filename) -> consult_config(State, Filename) ->
Fullpath = filename:join(rebar_dir:root_dir(State), Filename), Fullpath = filename:join(rebar_dir:root_dir(State), Filename),
?DEBUG("Loading configuration from ~p", [Fullpath]), ?DEBUG("Loading configuration from ~p", [Fullpath]),
case file:consult(Fullpath) of
{ok, [Config]} ->
{ok, Config};
{ok, []} ->
{ok, []};
{error, Error} ->
{error, {Error, Fullpath}}
end.
rebar_file_utils:try_consult(Fullpath).

Loading…
Cancel
Save