aboutsummaryrefslogtreecommitdiffstats
path: root/src/rcl_prv_discover.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/rcl_prv_discover.erl')
-rw-r--r--src/rcl_prv_discover.erl242
1 files changed, 42 insertions, 200 deletions
diff --git a/src/rcl_prv_discover.erl b/src/rcl_prv_discover.erl
index 0a12d24..2f88354 100644
--- a/src/rcl_prv_discover.erl
+++ b/src/rcl_prv_discover.erl
@@ -1,4 +1,4 @@
-%% -*- mode: Erlang; fill-column: 80; comment-column: 75; -*-
+%% -*- erlang-indent-level: 4; indent-tabs-mode: nil; fill-column: 80 -*-
%%% Copyright 2012 Erlware, LLC. All Rights Reserved.
%%%
%%% This file is provided to you under the Apache License,
@@ -41,100 +41,60 @@ init(State) ->
%% @doc recursively dig down into the library directories specified in the state
%% looking for OTP Applications
-spec do(rcl_state:t()) -> {ok, rcl_state:t()} | relcool:error().
-do(State) ->
- OutputDir = rcl_state:output_dir(State),
- LibDirs = get_lib_dirs(State),
- rcl_log:info(rcl_state:log(State),
- fun() ->
- ["Resolving OTP Applications from directories:\n",
- [[rcl_util:indent(1), LibDir, "\n"] || LibDir <- LibDirs]]
- end),
- resolve_app_metadata(State, LibDirs, OutputDir).
+do(State0) ->
+ LibDirs = get_lib_dirs(State0),
+ case rcl_app_discovery:do(State0, LibDirs) of
+ {ok, AppMeta} ->
+ case rcl_rel_discovery:do(State0, LibDirs, AppMeta) of
+ {ok, Releases} ->
+ State1 = rcl_state:available_apps(State0, AppMeta),
+ State3 = lists:foldl(fun add_if_not_found/2,
+ State1, Releases),
+ {ok, State3};
+ Error ->
+ Error
+ end;
+ Error ->
+ Error
+ end.
--spec format_error([ErrorDetail::term()]) -> iolist().
-format_error(ErrorDetails)
- when erlang:is_list(ErrorDetails) ->
- [[format_detail(ErrorDetail), "\n"] || ErrorDetail <- ErrorDetails].
+%% @doc this is here to comply with the signature. However, we do not actually
+%% produce any errors and so simply return an empty string.
+-spec format_error(any()) -> iolist().
+format_error(_) ->
+ "".
%%%===================================================================
%%% Internal Functions
%%%===================================================================
-resolve_app_metadata(State, LibDirs, OutputDir) ->
- AppMeta0 = lists:flatten(ec_plists:map(fun(LibDir) ->
- discover_dir([OutputDir],
- LibDir)
- end, LibDirs)),
- AppMeta1 = setup_overrides(State, AppMeta0),
-
- Errors = [case El of
- {error, Ret} -> Ret;
- _ -> El
- end
- || El <- AppMeta1,
- case El of
- {error, _} ->
- true;
- _ ->
- false
- end],
-
- case Errors of
- [] ->
- AppMeta2 = lists:flatten(AppMeta1),
- rcl_log:debug(rcl_state:log(State),
- fun() ->
- ["Resolved the following OTP Applications from the system: \n",
- [[rcl_app_info:format(1, App), "\n"] || App <- AppMeta2]]
- end),
- {ok, rcl_state:available_apps(State, AppMeta2)};
- _ ->
- ?RCL_ERROR(Errors)
+%% @doc only add the release if its not documented in the system
+add_if_not_found(Rel, State) ->
+ RelName = rcl_release:name(Rel),
+ RelVsn = rcl_release:vsn(Rel),
+ try
+ rcl_state:get_release(State, RelName, RelVsn),
+ State
+ catch
+ throw:not_found ->
+ rcl_state:add_release(State, Rel)
end.
-app_name({error, _}) ->
- undefined;
-app_name(AppMeta) ->
- rcl_app_info:name(AppMeta).
-
-setup_overrides(State, AppMetas0) ->
- Overrides = rcl_state:overrides(State),
- AppMetas1 = [AppMeta || AppMeta <- AppMetas0,
- not lists:keymember(app_name(AppMeta), 1, Overrides)],
- [case is_valid_otp_app(filename:join([FileName, "ebin",
- erlang:atom_to_list(AppName) ++ ".app"])) of
- [] ->
- {error, {invalid_override, AppName, FileName}};
- Error = {error, _} ->
- Error;
- App ->
- rcl_app_info:link(App, true)
- end || {AppName, FileName} <- Overrides] ++ AppMetas1.
-
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
+ case rcl_state:get(State, disable_default_libs, false) of
true ->
- add_system_lib_dir(State, LibDirs);
+ LibDirs0;
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
+ add_current_dir(State, LibDirs0)
end.
+-spec add_current_dir(rcl_state:t(), [file:name()]) -> [file:name()].
+add_current_dir(State, LibDirs) ->
+ %% 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.
+ Root = rcl_state:root_dir(State),
+ add_system_lib_dir(State, [filename:absname(Root) | LibDirs]).
+
-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),
@@ -151,121 +111,3 @@ add_system_lib_dir(State, LibDirs) ->
LibDirs
end
end.
-
--spec format_detail(ErrorDetail::term()) -> iolist().
-format_detail({error, {invalid_override, AppName, FileName}}) ->
- io_lib:format("Override {~p, ~p} is not a valid OTP App. Perhaps you forgot to build it?",
- [AppName, FileName]);
-format_detail({accessing, File, eaccess}) ->
- io_lib:format("permission denied accessing file ~s", [File]);
-format_detail({accessing, File, Type}) ->
- io_lib:format("error (~p) accessing file ~s", [Type, File]);
-format_detail({no_beam_files, EbinDir}) ->
- io_lib:format("no beam files found in directory ~s", [EbinDir]);
-format_detail({not_a_directory, EbinDir}) ->
- io_lib:format("~s is not a directory when it should be a directory", [EbinDir]);
-format_detail({unable_to_load_app, AppDir, _}) ->
- io_lib:format("Unable to load the application metadata from ~s", [AppDir]);
-format_detail({invalid_app_file, File}) ->
- io_lib:format("Application metadata file exists but is malformed: ~s",
- [File]);
-format_detail({unversioned_app, AppDir, _AppName}) ->
- io_lib:format("Application metadata exists but version is not available: ~s",
- [AppDir]);
-format_detail({app_info_error, {Module, Detail}}) ->
- Module:format_error(Detail).
-
--spec discover_dir([file:name()],
- file:name()) ->
- [rcl_app_info:t() | {error, Reason::term()}] |
- rcl_app_info:t() | {error, Reason::term()}.
-discover_dir(IgnoreDirs, File) ->
- case (not lists:member(File, IgnoreDirs))
- andalso filelib:is_dir(File) of
- true ->
- case file:list_dir(File) of
- {error, Reason} ->
- {error, {accessing, File, Reason}};
- {ok, List} ->
- ec_plists:map(fun(LibDir) ->
- discover_dir(IgnoreDirs, LibDir)
- end, [filename:join([File, Dir]) || Dir <- List])
- end;
- false ->
- is_valid_otp_app(File)
- end.
-
--spec is_valid_otp_app(file:name()) -> rcl_app_info:t() | {error, Reason::term()} | [].
-is_valid_otp_app(File) ->
- %% Is this an ebin dir?
- EbinDir = filename:dirname(File),
- case filename:basename(EbinDir) of
- "ebin" ->
- case lists:suffix(".app", File) of
- true ->
- has_at_least_one_beam(EbinDir, File);
- false ->
- []
- end;
- _ ->
- []
- end.
-
--spec has_at_least_one_beam(file:name(), file:filename()) ->
- rcl_app_info:t() | {error, Reason::term()}.
-has_at_least_one_beam(EbinDir, File) ->
- case file:list_dir(EbinDir) of
- {ok, List} ->
- case lists:any(fun(NFile) -> lists:suffix(".beam", NFile) end, List) of
- true ->
- gather_application_info(EbinDir, File);
- false ->
- {error, {no_beam_files, EbinDir}}
- end;
- _ ->
- {error, {not_a_directory, EbinDir}}
- end.
-
--spec gather_application_info(file:name(), file:filename()) ->
- rcl_app_info:t() | {error, Reason::term()}.
-gather_application_info(EbinDir, File) ->
- AppDir = filename:dirname(EbinDir),
- case file:consult(File) of
- {ok, [{application, AppName, AppDetail}]} ->
- get_vsn(AppDir, AppName, AppDetail);
- {error, Reason} ->
- {error, {unable_to_load_app, AppDir, Reason}};
- _ ->
- {error, {invalid_app_file, File}}
- end.
-
--spec get_vsn(file:name(), atom(), proplists:proplist()) ->
- rcl_app_info:t() | {error, Reason::term()}.
-get_vsn(AppDir, AppName, AppDetail) ->
- case proplists:get_value(vsn, AppDetail) of
- undefined ->
- {error, {unversioned_app, AppDir, AppName}};
- AppVsn ->
- case get_deps(AppDir, AppName, AppVsn, AppDetail) of
- {ok, App} ->
- App;
- {error, Detail} ->
- {error, {app_info_error, Detail}}
- end
- end.
-
--spec get_deps(file:name(), atom(), string(), proplists:proplist()) ->
- {ok, rcl_app_info:t()} | {error, Reason::term()}.
-get_deps(AppDir, AppName, AppVsn, AppDetail) ->
- ActiveApps = proplists:get_value(applications, AppDetail, []),
- LibraryApps = proplists:get_value(included_applications, AppDetail, []),
- rcl_app_info:new(AppName, AppVsn, AppDir, ActiveApps, LibraryApps).
-
-%%%===================================================================
-%%% Test Functions
-%%%===================================================================
-
--ifndef(NOTEST).
--include_lib("eunit/include/eunit.hrl").
-
--endif.