From 8f03afded4dbb046bacb241bd66a603dee4a3e40 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Sat, 21 Feb 2015 12:56:47 -0600 Subject: consolidate app validation and exist checks --- src/rebar_app_utils.erl | 116 ++++++++++++++++-------------------------------- 1 file changed, 39 insertions(+), 77 deletions(-) (limited to 'src/rebar_app_utils.erl') diff --git a/src/rebar_app_utils.erl b/src/rebar_app_utils.erl index 2b63519..ef52be8 100644 --- a/src/rebar_app_utils.erl +++ b/src/rebar_app_utils.erl @@ -28,13 +28,13 @@ -export([find/2, find/3, - is_app_dir/0, is_app_dir/1, is_app_src/1, app_src_to_app/1, - load_app_file/2, - app_vsn/2]). + validate_application_info/1, + validate_application_info/2]). -include("rebar.hrl"). +-include_lib("providers/include/providers.hrl"). %% =================================================================== %% Public API @@ -51,35 +51,6 @@ find(Name, Vsn, Apps) -> andalso rebar_app_info:original_vsn(App) =:= Vsn end, Apps). --spec is_app_dir() -> {true, file:name()} | false. -is_app_dir() -> - is_app_dir(rebar_dir:get_cwd()). - --spec is_app_dir(file:name()) -> {true, file:name()} | false. -is_app_dir(Dir) -> - SrcDir = filename:join([Dir, "src"]), - AppSrc = filename:join([SrcDir, "*.app.src"]), - case filelib:wildcard(AppSrc) of - [AppSrcFile] -> - {true, AppSrcFile}; - [] -> - EbinDir = filename:join([Dir, "ebin"]), - App = filename:join([EbinDir, "*.app"]), - case filelib:wildcard(App) of - [AppFile] -> - {true, AppFile}; - [] -> - false; - _ -> - ?ERROR("More than one .app file in ~s~n", [EbinDir]), - false - end; - _ -> - ?ERROR("More than one .app.src file in ~s~n", [SrcDir]), - false - end. - - is_app_src(Filename) -> %% If removing the extension .app.src yields a shorter name, %% this is an .app.src file. @@ -91,58 +62,49 @@ app_src_to_app(Filename) -> filelib:ensure_dir(AppFile), AppFile. -app_vsn(Config, AppFile) -> - case load_app_file(Config, AppFile) of - {ok, Config1, _, AppInfo} -> - AppDir = filename:dirname(filename:dirname(AppFile)), - rebar_utils:vcs_vsn(Config1, get_value(vsn, AppInfo, AppFile), - AppDir); - {error, Reason} -> - ?ABORT("Failed to extract vsn from ~s: ~p\n", - [AppFile, Reason]) - end. +-spec validate_application_info(rebar_app_info:t()) -> boolean(). +validate_application_info(AppInfo) -> + validate_application_info(AppInfo, rebar_app_info:app_details(AppInfo)). -load_app_file(_State, undefined) -> {error, missing_app_file}; -load_app_file(State, Filename) -> - AppFile = {app_file, Filename}, - case rebar_state:get(State, {appfile, AppFile}, undefined) of +validate_application_info(AppInfo, AppDetail) -> + EbinDir = rebar_app_info:ebin_dir(AppInfo), + case rebar_app_info:app_file(AppInfo) of undefined -> - case consult_app_file(Filename) of - {ok, [{application, AppName, AppData}]} -> - State1 = rebar_state:set(State, - {appfile, AppFile}, - {AppName, AppData}), - {ok, State1, AppName, AppData}; - {error, _} = Error -> - Error; - Other -> - {error, {unexpected_terms, Other}} - end; - {AppName, AppData} -> - {ok, State, AppName, AppData} + false; + AppFile -> + case get_modules_list(AppFile, AppDetail) of + {ok, List} -> + has_all_beams(EbinDir, List); + _Error -> + ?PRV_ERROR({module_list, AppFile}) + end end. %% =================================================================== %% Internal functions %% =================================================================== -%% In the case of *.app.src we want to give the user the ability to -%% dynamically script the application resource file (think dynamic version -%% string, etc.), in a way similar to what can be done with the rebar -%% config. However, in the case of *.app, rebar should not manipulate -%% that file. This enforces that dichotomy between app and app.src. -consult_app_file(Filename) -> - case lists:suffix(".app.src", Filename) of - false -> - file:consult(Filename); - true -> - {ok, rebar_config:consult_file(Filename)} - end. - -get_value(Key, AppInfo, AppFile) -> - case proplists:get_value(Key, AppInfo) of +-spec get_modules_list(file:filename_all(), proplists:proplist()) -> + {ok, list()} | + {warning, Reason::term()} | + {error, Reason::term()}. +get_modules_list(AppFile, AppDetail) -> + case proplists:get_value(modules, AppDetail) of undefined -> - ?ABORT("Failed to get app value '~p' from '~s'~n", [Key, AppFile]); - Value -> - Value + {warning, {invalid_app_file, AppFile}}; + ModulesList -> + {ok, ModulesList} end. + +-spec has_all_beams(file:filename_all(), list()) -> true | providers:error(). +has_all_beams(EbinDir, [Module | ModuleList]) -> + BeamFile = filename:join([EbinDir, + ec_cnv:to_list(Module) ++ ".beam"]), + case filelib:is_file(BeamFile) of + true -> + has_all_beams(EbinDir, ModuleList); + false -> + ?PRV_ERROR({missing_module, Module}) + end; +has_all_beams(_, []) -> + true. -- cgit v1.1 From c3e2ab1c1e8ef4d34ea66a64ca2a8b52a9067915 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Sun, 22 Feb 2015 13:18:53 -0600 Subject: remove unneeded get_modules_list function --- src/rebar_app_utils.erl | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) (limited to 'src/rebar_app_utils.erl') diff --git a/src/rebar_app_utils.erl b/src/rebar_app_utils.erl index ef52be8..cd1124a 100644 --- a/src/rebar_app_utils.erl +++ b/src/rebar_app_utils.erl @@ -72,11 +72,12 @@ validate_application_info(AppInfo, AppDetail) -> undefined -> false; AppFile -> - case get_modules_list(AppFile, AppDetail) of - {ok, List} -> - has_all_beams(EbinDir, List); - _Error -> - ?PRV_ERROR({module_list, AppFile}) + case proplists:get_value(modules, AppDetail) of + undefined -> + ?PRV_ERROR({module_list, AppFile}); + List -> + has_all_beams(EbinDir, List) + end end. @@ -84,18 +85,6 @@ validate_application_info(AppInfo, AppDetail) -> %% Internal functions %% =================================================================== --spec get_modules_list(file:filename_all(), proplists:proplist()) -> - {ok, list()} | - {warning, Reason::term()} | - {error, Reason::term()}. -get_modules_list(AppFile, AppDetail) -> - case proplists:get_value(modules, AppDetail) of - undefined -> - {warning, {invalid_app_file, AppFile}}; - ModulesList -> - {ok, ModulesList} - end. - -spec has_all_beams(file:filename_all(), list()) -> true | providers:error(). has_all_beams(EbinDir, [Module | ModuleList]) -> BeamFile = filename:join([EbinDir, -- cgit v1.1