diff options
Diffstat (limited to 'src/rebar_state.erl')
-rw-r--r-- | src/rebar_state.erl | 135 |
1 files changed, 70 insertions, 65 deletions
diff --git a/src/rebar_state.erl b/src/rebar_state.erl index 706c528..f39251d 100644 --- a/src/rebar_state.erl +++ b/src/rebar_state.erl @@ -3,49 +3,39 @@ -export([new/0, new/1, new/2, new/3, get/2, get/3, set/3, command_args/1, command_args/2, + command_parsed_args/1, command_parsed_args/2, dir/1, dir/2, - set_skip_dir/2, is_skip_dir/2, reset_skip_dirs/1, create_logic_providers/2, project_apps/1, project_apps/2, deps_names/1, - binary_deps/1, binary_deps/2, + pkg_deps/1, pkg_deps/2, src_deps/1, src_deps/2, + src_apps/1, src_apps/2, prepend_hook/3, append_hook/3, hooks/2, providers/1, providers/2, add_provider/2]). -include("rebar.hrl"). --ifdef(namespaced_types). -%% dict:dict() exists starting from Erlang 17. --type rebar_dict() :: dict:dict(term(), term()). --else. -%% dict() has been obsoleted in Erlang 17 and deprecated in 18. --type rebar_dict() :: dict(). --endif. +-record(state_t, {dir :: file:name(), + opts = [], --record(state_t, {dir :: file:filename(), - opts = [] :: list(), - local_opts = [] :: list(), - config = new_globals() :: rebar_dict(), + command_args = [], + command_parsed_args = [], - envs = new_env() :: rebar_dict(), - command_args = [] :: list(), + src_deps = [], + src_apps = [], + pkg_deps = [] :: [rlx_depsolver:constraint()], + project_apps = [], - src_deps = ordsets:new() :: ordsets:ordset(rebar_app_info:t()), - binary_deps = [], - project_apps = ordsets:new() :: ordsets:ordset(rebar_app_info:t()), - - providers = [], - hooks = [], - skip_dirs = new_skip_dirs() :: rebar_dict() }). + providers = []}). -export_type([t/0]). --opaque t() :: #state_t{}. +-type t() :: record(state_t). -spec new() -> t(). new() -> @@ -64,9 +54,9 @@ new(ParentState=#state_t{}, Config) -> -spec new(t(), list(), file:name()) -> t(). new(ParentState, Config, Dir) -> - _Opts = ParentState#state_t.opts, + Opts = ParentState#state_t.opts, LocalOpts = case rebar_config:consult_file(?LOCK_FILE) of - {ok, [D]} -> + [D] -> [{locks, D} | Config]; _ -> Config @@ -74,7 +64,7 @@ new(ParentState, Config, Dir) -> ProviderModules = [], create_logic_providers(ProviderModules, ParentState#state_t{dir=Dir - ,opts=LocalOpts}). + ,opts=lists:umerge(LocalOpts, Opts)}). get(State, Key) -> proplists:get_value(Key, State#state_t.opts). @@ -82,33 +72,23 @@ get(State, Key) -> get(State, Key, Default) -> proplists:get_value(Key, State#state_t.opts, Default). +-spec set(t(), any(), any()) -> t(). set(State, Key, Value) -> Opts = proplists:delete(Key, State#state_t.opts), State#state_t { opts = [{Key, Value} | Opts] }. -set_skip_dir(State, Dir) -> - OldSkipDirs = State#state_t.skip_dirs, - NewSkipDirs = case is_skip_dir(State, Dir) of - false -> - ?DEBUG("Adding skip dir: ~s\n", [Dir]), - dict:store(Dir, true, OldSkipDirs); - true -> - OldSkipDirs - end, - State#state_t{skip_dirs = NewSkipDirs}. - -is_skip_dir(State, Dir) -> - dict:is_key(Dir, State#state_t.skip_dirs). - -reset_skip_dirs(State) -> - State#state_t{skip_dirs = new_skip_dirs()}. - command_args(#state_t{command_args=CmdArgs}) -> CmdArgs. command_args(State, CmdArgs) -> State#state_t{command_args=CmdArgs}. +command_parsed_args(#state_t{command_parsed_args=CmdArgs}) -> + CmdArgs. + +command_parsed_args(State, CmdArgs) -> + State#state_t{command_parsed_args=CmdArgs}. + dir(#state_t{dir=Dir}) -> Dir. @@ -123,13 +103,14 @@ deps_names(State) -> ec_cnv:to_binary(Dep) end, Deps). -binary_deps(#state_t{binary_deps=BinaryDeps}) -> - BinaryDeps. +-spec pkg_deps(t()) -> [rlx_depsolver:constraint()]. +pkg_deps(#state_t{pkg_deps=PkgDeps}) -> + PkgDeps. -binary_deps(State=#state_t{binary_deps=BinaryDeps}, NewBinaryDeps) when is_list(BinaryDeps) -> - State#state_t{binary_deps=NewBinaryDeps}; -binary_deps(State=#state_t{binary_deps=BinaryDeps}, BinaryDep) -> - State#state_t{binary_deps=[BinaryDep | BinaryDeps]}. +pkg_deps(State=#state_t{pkg_deps=PkgDeps}, NewPkgDeps) when is_list(PkgDeps) -> + State#state_t{pkg_deps=NewPkgDeps}; +pkg_deps(State=#state_t{pkg_deps=PkgDeps}, PkgDep) -> + State#state_t{pkg_deps=[PkgDep | PkgDeps]}. src_deps(#state_t{src_deps=SrcDeps}) -> SrcDeps. @@ -137,7 +118,19 @@ src_deps(#state_t{src_deps=SrcDeps}) -> src_deps(State=#state_t{src_deps=SrcDeps}, NewSrcDeps) when is_list(SrcDeps) -> State#state_t{src_deps=NewSrcDeps}; src_deps(State=#state_t{src_deps=SrcDeps}, SrcDep) -> - State#state_t{src_deps=[SrcDep | SrcDeps]}. + Name = rebar_app_info:name(SrcDep), + NewSrcDeps = lists:keystore(Name, 2, SrcDeps, SrcDep), + State#state_t{src_deps=NewSrcDeps}. + +src_apps(#state_t{src_apps=SrcApps}) -> + SrcApps. + +src_apps(State=#state_t{src_apps=_SrcApps}, NewSrcApps) when is_list(NewSrcApps) -> + State#state_t{src_apps=NewSrcApps}; +src_apps(State=#state_t{src_apps=SrcApps}, NewSrcApp) -> + Name = rebar_app_info:name(NewSrcApp), + NewSrcApps = lists:keystore(Name, 2, SrcApps, NewSrcApp), + State#state_t{src_apps=NewSrcApps}. project_apps(#state_t{project_apps=Apps}) -> Apps. @@ -153,32 +146,44 @@ providers(#state_t{providers=Providers}) -> providers(State, NewProviders) -> State#state_t{providers=NewProviders}. +-spec add_provider(t(), providers:t()) -> t(). add_provider(State=#state_t{providers=Providers}, Provider) -> State#state_t{providers=[Provider | Providers]}. create_logic_providers(ProviderModules, State0) -> lists:foldl(fun(ProviderMod, Acc) -> - {ok, State1} = rebar_provider:new(ProviderMod, Acc), - State1 + case providers:new(ProviderMod, Acc) of + {error, Reason} -> + ?ERROR(Reason++"~n", []), + Acc; + {ok, State1} -> + State1 + end end, State0, ProviderModules). -prepend_hook(State=#state_t{hooks=Hooks}, Target, Hook) -> - {PreHooks, PostHooks} = proplists:get_value(Target, Hooks, {[], []}), - State#state_t{hooks=[{Target, {[Hook | PreHooks], PostHooks}} | proplists:delete(Target, Hooks)]}. +prepend_hook(State=#state_t{providers=Providers}, Target, Hook) -> + State#state_t{providers=add_hook(pre, Providers, Target, Hook)}. -append_hook(State=#state_t{hooks=Hooks}, Target, Hook) -> - {PreHooks, PostHooks} = proplists:get_value(Target, Hooks, {[], []}), - State#state_t{hooks=[{Target, {PreHooks, [Hook | PostHooks]}} | proplists:delete(Target, Hooks)]}. +append_hook(State=#state_t{providers=Providers}, Target, Hook) -> + State#state_t{providers=add_hook(post, Providers, Target, Hook)}. -hooks(#state_t{hooks=Hooks}, Target) -> - proplists:get_value(Target, Hooks, {[], []}). +-spec hooks(t(), atom()) -> {[providers:t()], [providers:t()]}. +hooks(_State=#state_t{providers=Providers}, Target) -> + Provider = providers:get_provider(Target, Providers), + providers:hooks(Provider). %% =================================================================== %% Internal functions %% =================================================================== -new_globals() -> dict:new(). - -new_env() -> dict:new(). - -new_skip_dirs() -> dict:new(). +add_hook(Which, Providers, Target, Hook) -> + Provider = providers:get_provider(Target, Providers), + Hooks = providers:hooks(Provider), + NewHooks = add_hook(Which, Hooks, Hook), + NewProvider = providers:hooks(Provider, NewHooks), + [NewProvider | lists:delete(Provider, Providers)]. + +add_hook(pre, {PreHooks, PostHooks}, Hook) -> + {[Hook | PreHooks], PostHooks}; +add_hook(post, {PreHooks, PostHooks}, Hook) -> + {PreHooks, [Hook | PostHooks]}. |