From 9efadeb73a53d254dd64a0fa1d50b3363d5f993c Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 14 Sep 2012 09:24:12 -0500 Subject: add system and rebar discover to rcl_prv_discover --- src/rcl_prv_discover.erl | 73 ++++++++++++++++++++++++++++++++++++++------ test/rclt_discover_SUITE.erl | 4 ++- 2 files changed, 67 insertions(+), 10 deletions(-) diff --git a/src/rcl_prv_discover.erl b/src/rcl_prv_discover.erl index e9a6dea..1acf85a 100644 --- a/src/rcl_prv_discover.erl +++ b/src/rcl_prv_discover.erl @@ -23,12 +23,11 @@ %%% those OTP Applications it loads the information about them and adds them to %%% the state of available apps. This implements the rcl_provider behaviour. -module(rcl_prv_discover). - -behaviour(rcl_provider). -export([init/1, do/1, - format/1]). + format_error/1]). %%============================================================================ %% API @@ -37,15 +36,23 @@ init(State) -> {ok, State}. +%% @doc recursively dig down into the library directories specified in the state +%% looking for OTP Applications -spec do(rcl_state:t()) -> {error, Reason::term()} | {ok, rcl_state:t()}. do(State) -> - LibDirs = rcl_state:lib_dirs(State), - AppMeta = lists:flatten(ec_plists:map(fun discover_dir/1, LibDirs)), + LibDirs = get_lib_dirs(State), + rcl_log:info(rcl_state:log(State), + fun() -> + ["Resolving OTP Applications from directories:\n", + [[LibDir, "\n"] || LibDir <- LibDirs]] + end), + + AppMeta0 = lists:flatten(ec_plists:map(fun discover_dir/1, LibDirs)), Errors = [case El of {error, Ret} -> Ret; _ -> El end - || El <- AppMeta, + || El <- AppMeta0, case El of {error, _} -> true; @@ -55,21 +62,69 @@ do(State) -> lists:filter(fun({error, _}) -> true; (_) -> false - end, AppMeta), + end, AppMeta0), case Errors of [] -> - {ok, rcl_state:available_apps(State, lists:flatten(AppMeta))}; + AppMeta1 = lists:flatten(AppMeta0), + rcl_log:debug(rcl_state:log(State), + fun() -> + ["Resolved the following OTP Applications from the system: \n", + [[rcl_app_info:format(App), "\n"] || App <- AppMeta1]] + end), + {ok, rcl_state:available_apps(State, AppMeta1)}; _ -> {error, Errors} end. --spec format({error, [ErrorDetail::term()]}) -> iolist(). -format({error, ErrorDetails}) -> +-spec format_error({error, [ErrorDetail::term()]}) -> iolist(). +format_error({error, ErrorDetails}) -> [[format_detail(ErrorDetail), "\n"] || ErrorDetail <- ErrorDetails]. %%%=================================================================== %%% Internal Functions %%%=================================================================== +get_lib_dirs(State) -> + LibDirs0 = rcl_state:lib_dirs(State), + add_rebar_deps_dir(State, LibDirs0). + +-spec add_rebar_deps_dir(rcl_state:t(), [file:name()]) -> [file:name()]. +add_rebar_deps_dir(State, LibDirs) -> + ExcludeRebar = rcl_state:get(State, discover_exclude_rebar, false), + case ExcludeRebar of + true -> + add_system_lib_dir(State, LibDirs); + false -> + %% Check to see if there is a rebar.config. If so then look for a deps + %% dir. If both are there then we add that to the lib dirs. + {ok, Cwd} = file:get_cwd(), + + RebarConfig = filename:join([Cwd, "rebar.config"]), + DepsDir = filename:join([Cwd, "deps"]), + case filelib:is_regular(RebarConfig) andalso filelib:is_dir(DepsDir) of + true -> + add_system_lib_dir(State, [filename:absname(Cwd) | LibDirs]); + false -> + add_system_lib_dir(State, LibDirs) + end + end. + +-spec add_system_lib_dir(rcl_state:t(), [file:name()]) -> [file:name()]. +add_system_lib_dir(State, LibDirs) -> + ExcludeSystem = rcl_state:get(State, discover_exclude_system, false), + + case ExcludeSystem of + true -> + LibDirs; + false -> + SystemLibDir0 = code:lib_dir(), + case filelib:is_dir(SystemLibDir0) of + true -> + [SystemLibDir0 | LibDirs]; + false -> + LibDirs + end + end. + -spec format_detail(ErrorDetail::term()) -> iolist(). format_detail({accessing, File, eaccess}) -> io_lib:format("permission denied accessing file ~s", [File]); diff --git a/test/rclt_discover_SUITE.erl b/test/rclt_discover_SUITE.erl index 718fd03..dca683a 100644 --- a/test/rclt_discover_SUITE.erl +++ b/test/rclt_discover_SUITE.erl @@ -72,7 +72,9 @@ normal_case(Config) -> || App <- [{create_random_name("lib_app2_"), create_random_vsn()} || _ <- lists:seq(1, 100)]], - State0 = proplists:get_value(state, Config), + State0 = rcl_state:put(rcl_state:put(proplists:get_value(state, Config), + discover_exclude_rebar, true), + discover_exclude_system, true), {DiscoverProvider, {ok, State1}} = rcl_provider:new(rcl_prv_discover, State0), {ok, State2} = rcl_provider:do(DiscoverProvider, State1), lists:foreach(fun(App) -> -- cgit v1.2.3