diff options
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | src/rcl_app_discovery.erl | 82 | ||||
-rw-r--r-- | src/rcl_dscv_util.erl | 144 | ||||
-rw-r--r-- | src/rcl_prv_config.erl | 2 | ||||
-rw-r--r-- | src/rcl_prv_discover.erl | 16 | ||||
-rw-r--r-- | src/rcl_rel_discovery.erl | 75 | ||||
-rw-r--r-- | src/rcl_state.erl | 17 | ||||
-rw-r--r-- | test/rclt_release_SUITE.erl | 3 |
8 files changed, 232 insertions, 111 deletions
@@ -36,7 +36,7 @@ $(error "Rebar not available on this system") endif .PHONY: all compile doc clean test dialyzer typer shell distclean pdf \ - get-deps escript clean-common-test-data rebuild + update-deps escript clean-common-test-data rebuild all: deps compile escript dialyzer test @@ -114,4 +114,4 @@ distclean: clean - rm -rf $(DEPS_PLT) - rm -rvf $(CURDIR)/deps/* -rebuild: distclean get-deps compile escript dialyzer test +rebuild: distclean deps compile escript dialyzer test diff --git a/src/rcl_app_discovery.erl b/src/rcl_app_discovery.erl index 20df1cf..43557c4 100644 --- a/src/rcl_app_discovery.erl +++ b/src/rcl_app_discovery.erl @@ -53,39 +53,33 @@ format_error(ErrorDetails) %%% Internal Functions %%%=================================================================== resolve_app_metadata(State, LibDirs) -> - AppMeta0 = lists:flatten(ec_plists:map(fun(LibDir) -> - discover_dir([], LibDir) - end, LibDirs)), - AppMeta1 = setup_overrides(State, AppMeta0), - - Errors = [case El of - {error, Ret} -> Ret; - _ -> El - end - || El <- AppMeta1, - case El of - {error, _} -> + AppMeta0 = lists:flatten(rcl_dscv_util:do(fun discover_dir/2, LibDirs)), + case [case Err of + {error, Ret} -> + Ret + end + || Err <- AppMeta0, + case Err of + {error, _} -> true; _ -> false - end], - - case Errors of + end] of [] -> - AppMeta2 = lists:flatten(AppMeta1), + AppMeta1 = [App || {ok, App} <- setup_overrides(State, AppMeta0)], 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]] + [[rcl_app_info:format(1, App), "\n"] || App <- AppMeta1]] end), - {ok, AppMeta2}; - _ -> + {ok, AppMeta1}; + Errors -> ?RCL_ERROR(Errors) end. app_name({error, _}) -> undefined; -app_name(AppMeta) -> +app_name({ok, AppMeta}) -> rcl_app_info:name(AppMeta). setup_overrides(State, AppMetas0) -> @@ -94,16 +88,15 @@ setup_overrides(State, 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 - [] -> + {noresult, false} -> {error, {invalid_override, AppName, FileName}}; Error = {error, _} -> Error; - App -> - rcl_app_info:link(App, true) + {ok, App} -> + {ok, rcl_app_info:link(App, true)} end || {AppName, FileName} <- Overrides] ++ AppMetas1. - -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?", @@ -127,27 +120,16 @@ format_detail({unversioned_app, AppDir, _AppName}) -> 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 discover_dir([file:name()], directory | file) -> + {ok, rcl_app_info:t()} | {error, Reason::term()}. +discover_dir(_File, directory) -> + {noresult, true}; +discover_dir(File, file) -> + is_valid_otp_app(File). + +-spec is_valid_otp_app(file:name()) -> {ok, rcl_app_info:t()} | {error, Reason::term()} | + {noresult, false}. --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), @@ -157,14 +139,14 @@ is_valid_otp_app(File) -> true -> has_at_least_one_beam(EbinDir, File); false -> - [] + {noresult, false} end; _ -> - [] + {noresult, false} end. -spec has_at_least_one_beam(file:name(), file:filename()) -> - rcl_app_info:t() | {error, Reason::term()}. + {ok, rcl_app_info:t()} | {error, Reason::term()}. has_at_least_one_beam(EbinDir, File) -> case file:list_dir(EbinDir) of {ok, List} -> @@ -179,7 +161,7 @@ has_at_least_one_beam(EbinDir, File) -> end. -spec gather_application_info(file:name(), file:filename()) -> - rcl_app_info:t() | {error, Reason::term()}. + {ok, rcl_app_info:t()} | {error, Reason::term()}. gather_application_info(EbinDir, File) -> AppDir = filename:dirname(EbinDir), case file:consult(File) of @@ -192,7 +174,7 @@ gather_application_info(EbinDir, File) -> end. -spec get_vsn(file:name(), atom(), proplists:proplist()) -> - rcl_app_info:t() | {error, Reason::term()}. + {ok, rcl_app_info:t()} | {error, Reason::term()}. get_vsn(AppDir, AppName, AppDetail) -> case proplists:get_value(vsn, AppDetail) of undefined -> @@ -200,7 +182,7 @@ get_vsn(AppDir, AppName, AppDetail) -> AppVsn -> case get_deps(AppDir, AppName, AppVsn, AppDetail) of {ok, App} -> - App; + {ok, App}; {error, Detail} -> {error, {app_info_error, Detail}} end diff --git a/src/rcl_dscv_util.erl b/src/rcl_dscv_util.erl new file mode 100644 index 0000000..4fb6126 --- /dev/null +++ b/src/rcl_dscv_util.erl @@ -0,0 +1,144 @@ +%% -*- 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, +%%% Version 2.0 (the "License"); you may not use this file +%%% except in compliance with the License. You may obtain +%%% a copy of the License at +%%% +%%% http://www.apache.org/licenses/LICENSE-2.0 +%%% +%%% Unless required by applicable law or agreed to in writing, +%%% software distributed under the License is distributed on an +%%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +%%% KIND, either express or implied. See the License for the +%%% specific language governing permissions and limitations +%%% under the License. +%%%--------------------------------------------------------------------------- +%%% @author Eric Merritt <[email protected]> +%%% @copyright (C) 2012 Erlware, LLC. +%%% +%%% @doc This provider uses the lib_dir setting of the state. It searches the +%%% Lib Dirs looking for all OTP Applications that are available. When it finds +%%% 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_dscv_util). + +-export([do/2, + format_error/1]). + +-include_lib("relcool/include/relcool.hrl"). + +%%============================================================================ +%% Types +%%============================================================================ + +-type process_fun(Result) :: fun((filename:name(), file | directory) -> + {ok, Result} | + {error, term()} | + {ok, Result, Recurse::boolean()} | + {noresult, Recurse::boolean()} | + {error, term()}). + +%%============================================================================ +%% API +%%============================================================================ + +%% @doc recursively dig down into the library directories specified in the state +%% looking for OTP Applications +-spec do(process_fun([term()] | term()), [filename:name()]) -> + [term() | {error, term()}]. +do(ProcessDir, LibDirs) -> + lists:flatten(ec_plists:map(fun(LibDir) -> + discover_dir(ProcessDir, LibDir, + ec_file:type(LibDir)) + end, LibDirs)). + +-spec format_error([ErrorDetail::term()]) -> iolist(). +format_error(ErrorDetails) + when erlang:is_list(ErrorDetails) -> + [[format_detail(ErrorDetail), "\n"] || ErrorDetail <- ErrorDetails]. + +%%%=================================================================== +%%% Internal Functions +%%%=================================================================== +-spec format_detail(ErrorDetail::term()) -> iolist(). +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({not_a_directory, EbinDir}) -> + io_lib:format("~s is not a directory when it should be a directory", [EbinDir]). + +-spec discover_dir(process_fun([term()] | term()), + file:name(), + directory | file | symlink) -> + [{ok, term()} + | {error, Reason::term()}] + | [] + | {ok, term()} + | {error, Reason::term()}. +discover_dir(ProcessDir, File, directory) -> + case ProcessDir(File, directory) of + {ok, Result, true} -> + [{ok, Result} | recurse(ProcessDir, File)]; + {noresult, true} -> + recurse(ProcessDir, File); + {ok, Result, _} -> + [{ok, Result}]; + {noresult, _} -> + []; + Err = {error, _} -> + [Err] + end; +discover_dir(ProcessDir, File, file) -> + Res = ProcessDir(File, file), + io:format("Result -> ~p~n", [Res]), + case ProcessDir(File, file) of + {ok, Result} -> + [{ok, Result}]; + {noresult, _} -> + []; + Err = {error, _} -> + [Err] + end; +discover_dir(ProcessDir, File, symlink) -> + case filelib:is_dir(File) of + false -> + discover_dir(ProcessDir, File, file); + true -> + discover_real_symlink_dir(ProcessDir, File) + end. + +discover_real_symlink_dir(ProcessDir, File) -> + {ok, CurCwd} = file:get_cwd(), + ok = file:set_cwd(File), + {ok, ActualRealDir} = file:get_cwd(), + ok = file:set_cwd(CurCwd), + lists:prefix(iolist_to_list(filename:absname(ActualRealDir)), + iolist_to_list(filename:absname(File))), + case ProcessDir(File, directory) of + {ok, Result, true} -> + [{ok, Result} | recurse(ProcessDir, File)]; + {noresult, true} -> + recurse(ProcessDir, File); + {ok, Result, _} -> + [{ok, Result}]; + {noresult, _} -> + []; + Err = {error, _} -> + [Err] + end. + +recurse(ProcessDir, File) -> + case file:list_dir(File) of + {error, Reason} -> + {error, {accessing, File, Reason}}; + {ok, List} -> + ec_plists:map(fun(LibDir) -> + discover_dir(ProcessDir, LibDir, ec_file:type(LibDir)) + end, [filename:join([File, Dir]) || Dir <- List]) + end. + +iolist_to_list(IoList) -> + erlang:binary_to_list(erlang:iolist_to_binary(IoList)). diff --git a/src/rcl_prv_config.erl b/src/rcl_prv_config.erl index 1027edf..4d1c119 100644 --- a/src/rcl_prv_config.erl +++ b/src/rcl_prv_config.erl @@ -46,6 +46,8 @@ init(State) -> -spec do(rcl_state:t()) ->{ok, rcl_state:t()} | relcool:error(). do(State) -> case rcl_state:config_file(State) of + [] -> + search_for_dominating_config(State); undefined -> search_for_dominating_config(State); ConfigFile when erlang:is_list(ConfigFile) -> diff --git a/src/rcl_prv_discover.erl b/src/rcl_prv_discover.erl index 2f88354..10fb28b 100644 --- a/src/rcl_prv_discover.erl +++ b/src/rcl_prv_discover.erl @@ -48,9 +48,9 @@ do(State0) -> 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}; + {ok, rcl_state:discovered_releases(State1, lists:foldl(fun add/2, + ec_dictionary:new(ec_dict), + Releases))}; Error -> Error end; @@ -68,16 +68,10 @@ format_error(_) -> %%% Internal Functions %%%=================================================================== %% @doc only add the release if its not documented in the system -add_if_not_found(Rel, State) -> +add(Rel, Dict) -> 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. + ec_dictionary:add({RelName, RelVsn}, Rel, Dict). get_lib_dirs(State) -> LibDirs0 = rcl_state:lib_dirs(State), diff --git a/src/rcl_rel_discovery.erl b/src/rcl_rel_discovery.erl index 0f923a0..38d7913 100644 --- a/src/rcl_rel_discovery.erl +++ b/src/rcl_rel_discovery.erl @@ -54,31 +54,32 @@ format_error(ErrorDetails) %%% Internal Functions %%%=================================================================== resolve_rel_metadata(State, LibDirs, AppMeta) -> - ReleaseMeta0 = - lists:flatten(ec_plists:map(fun(LibDir) -> - discover_dir([], LibDir, AppMeta) - end, LibDirs)), - Errors = [case El of - {error, Ret} -> Ret; - _ -> El - end - || El <- ReleaseMeta0, - case El of - {error, _} -> - true; - _ -> - false - end], + ReleaseMeta0 = lists:flatten(rcl_dscv_util:do(fun(LibDir, FileType) -> + discover_dir(LibDir, + AppMeta, + FileType) + end, LibDirs)), + + Errors = [case El of + {error, Ret} -> Ret; + _ -> El + end + || El <- ReleaseMeta0, + case El of + {error, _} -> + true; + _ -> + false + end], case Errors of [] -> - ReleaseMeta1 = lists:flatten(ReleaseMeta0), rcl_log:debug(rcl_state:log(State), fun() -> ["Resolved the following OTP Releases from the system: \n", - [[rcl_release:format(1, Rel), "\n"] || Rel <- ReleaseMeta1]] + [[rcl_release:format(1, Rel), "\n"] || Rel <- ReleaseMeta0]] end), - {ok, ReleaseMeta1}; + {ok, ReleaseMeta0}; _ -> ?RCL_ERROR(Errors) end. @@ -89,38 +90,26 @@ format_detail({accessing, File, eaccess}) -> format_detail({accessing, File, Type}) -> io_lib:format("error (~p) accessing file ~s", [Type, File]). --spec discover_dir([file:name()], file:name(), [rcl_app_info:t()]) -> - [rcl_release:t() | {error, Reason::term()}] - | rcl_release:t() - | {error, Reason::term()}. -discover_dir(IgnoreDirs, File, AppMeta) -> - 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, AppMeta) - end, - [filename:join([File, Dir]) || Dir <- List]) - end; - false -> - is_valid_release(File, AppMeta) - end. +-spec discover_dir(file:name(), [rcl_app_info:t()], directory | file) -> + {ok, rcl_release:t()} + | {error, Reason::term()} + | {noresult, false}. +discover_dir(_File, _AppMeta, directory) -> + {noresult, true}; +discover_dir(File, AppMeta, file) -> + is_valid_release(File, AppMeta). -spec is_valid_release(file:name(), [rcl_app_info:t()]) -> - rcl_release:t() - | {error, Reason::term()} - | []. + {ok, rcl_release:t()} + | {error, Reason::term()} + | {noresult, false}. is_valid_release(File, AppMeta) -> case lists:suffix(".rel", File) of true -> resolve_release(File, AppMeta); false -> - [] + {noresult, false} end. resolve_release(RelFile, AppMeta) -> @@ -141,7 +130,7 @@ build_release(RelName, RelVsn, ErtsVsn, Apps, AppMeta) -> resolve_apps(Apps, AppMeta, Release, []). resolve_apps([], _AppMeta, Release, Acc) -> - rcl_release:application_details(Release, Acc); + {ok, rcl_release:application_details(Release, Acc)}; resolve_apps([AppInfo | Apps], AppMeta, Release, Acc) -> AppName = erlang:element(1, AppInfo), AppVsn = ec_semver:parse(erlang:element(2, AppInfo)), diff --git a/src/rcl_state.erl b/src/rcl_state.erl index 6fd5655..6568541 100644 --- a/src/rcl_state.erl +++ b/src/rcl_state.erl @@ -42,6 +42,8 @@ get_release/3, update_release/2, releases/1, + discovered_releases/1, + discovered_releases/2, default_release/1, default_release/3, available_apps/1, @@ -72,9 +74,8 @@ default_release :: {rcl_release:name(), rcl_release:vsn()}, sys_config :: file:filename() | undefined, overrides :: [{AppName::atom(), Directory::file:filename()}], - releases :: ec_dictionary:dictionary({ReleaseName::atom(), - ReleaseVsn::string()}, - rcl_release:t()), + releases :: releases(), + discovered_releases :: releases(), config_values :: ec_dictionary:dictionary(Key::atom(), Value::term())}). @@ -87,6 +88,7 @@ rcl_release:t()). -type cmd_args() :: proplists:proplist(). -type caller() :: command_line | api. + -opaque t() :: record(state_t). %%============================================================================ @@ -107,6 +109,7 @@ new(PropList, Target) goals=proplists:get_value(goals, PropList, []), providers = [], releases=ec_dictionary:new(ec_dict), + discovered_releases=ec_dictionary:new(ec_dict), config_values=ec_dictionary:new(ec_dict), overrides = proplists:get_value(overrides, PropList, []), root_dir = proplists:get_value(root_dir, PropList, Root), @@ -197,6 +200,14 @@ get_release(#state_t{releases=Releases}, Name, Vsn) -> releases(#state_t{releases=Releases}) -> Releases. +-spec discovered_releases(t()) -> releases(). +discovered_releases(#state_t{discovered_releases=Releases}) -> + Releases. + +-spec discovered_releases(t(), releases()) -> t(). +discovered_releases(State, Releases) -> + State#state_t{discovered_releases=Releases}. + -spec default_release(t()) -> {rcl_release:name() | undefined, rcl_release:vsn() | undefined}. default_release(#state_t{default_release=Def}) -> diff --git a/test/rclt_release_SUITE.erl b/test/rclt_release_SUITE.erl index dd03b9f..fccdfcc 100644 --- a/test/rclt_release_SUITE.erl +++ b/test/rclt_release_SUITE.erl @@ -245,7 +245,6 @@ make_implicit_config_release(Config) -> {ok, FooRoot} = file:get_cwd(), {ok, State} = relcool:do(undefined, undefined, [], [LibDir1], 2, OutputDir, undefined), - [{{foo, "0.0.1"}, Release}] = ec_dictionary:to_list(rcl_state:releases(State)), ?assert(ec_file:exists(OutputDir)), AppSpecs = rcl_release:applications(Release), @@ -430,7 +429,7 @@ overlay_release(Config) -> ?assert(proplists:is_defined(config_file, TemplateData)), ?assertEqual([""], proplists:get_value(goals, TemplateData)), - ?assertEqual("undefined", + ?assertEqual([], proplists:get_value(sys_config, TemplateData)), ?assert(proplists:is_defined(root_dir, TemplateData)), ?assertEqual(foo, |