diff options
author | Tristan Sloughter <tristan.sloughter@gmail.com> | 2014-12-02 15:55:51 -0600 |
---|---|---|
committer | Tristan Sloughter <tristan.sloughter@gmail.com> | 2014-12-02 15:55:51 -0600 |
commit | 3b2d9ba8c81a41ae5cd554a3f50283e713e191c8 (patch) | |
tree | f5309346fc164fe3556e96db679b32bf5eca5a56 /src/rebar_state.erl | |
parent | 8d655d3c502295394ab30d9fc3fd11679629885d (diff) | |
parent | 3af351cec28521caaa15308b1a4a992380723794 (diff) |
Merge pull request #31 from tsloughter/profiles
Profiles
Diffstat (limited to 'src/rebar_state.erl')
-rw-r--r-- | src/rebar_state.erl | 184 |
1 files changed, 124 insertions, 60 deletions
diff --git a/src/rebar_state.erl b/src/rebar_state.erl index 840b428..77004a7 100644 --- a/src/rebar_state.erl +++ b/src/rebar_state.erl @@ -2,36 +2,49 @@ -export([new/0, new/1, new/2, new/3, get/2, get/3, set/3, + + opts/1, + default/1, default/2, + + lock/1, lock/2, + + current_profile/1, + current_profile/2, + command_args/1, command_args/2, command_parsed_args/1, command_parsed_args/2, + apply_profile/2, + dir/1, dir/2, create_logic_providers/2, project_apps/1, project_apps/2, + deps_to_build/1, deps_to_build/2, + all_deps/1, all_deps/2, deps_names/1, - 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"). --record(state_t, {dir :: file:name(), - opts = [], +-record(state_t, {dir :: file:name(), + opts = dict:new() :: rebar_dict(), + default = dict:new() :: rebar_dict(), - command_args = [], + lock = [], + current_profile = default :: atom(), + + command_args = [], command_parsed_args = [], - src_deps = [], - src_apps = [], - pkg_deps = [] :: [rebar_packages:package()], - project_apps = [], + project_apps = [] :: [rebar_app_into:t()], + deps_to_build = [] :: [rebar_app_into:t()], + all_deps = [] :: [rebar_app_into:t()], - providers = []}). + providers = []}). -export_type([t/0]). @@ -39,17 +52,26 @@ -spec new() -> t(). new() -> - #state_t{dir = rebar_utils:get_cwd()}. + #state_t{dir = rebar_dir:get_cwd()}. -spec new(list()) -> t(). new(Config) when is_list(Config) -> - #state_t { dir = rebar_utils:get_cwd(), - opts = Config }. - --spec new(t(), list()) -> t(). + Opts = dict:from_list(Config), + #state_t { dir = rebar_dir:get_cwd(), + default = Opts, + opts = Opts }. + +-spec new(t() | atom(), list()) -> t(). +new(Profile, Config) when is_atom(Profile) + , is_list(Config) -> + Opts = dict:from_list(Config), + #state_t { dir = rebar_dir:get_cwd(), + current_profile = Profile, + default = Opts, + opts = Opts }; new(ParentState=#state_t{}, Config) -> %% Load terms from rebar.config, if it exists - Dir = rebar_utils:get_cwd(), + Dir = rebar_dir:get_cwd(), new(ParentState, Config, Dir). -spec new(t(), list(), file:name()) -> t(). @@ -57,25 +79,55 @@ new(ParentState, Config, Dir) -> Opts = ParentState#state_t.opts, LocalOpts = case rebar_config:consult_file(?LOCK_FILE) of [D] -> - [{locks, D} | Config]; + dict:from_list([{locks, D} | Config]); _ -> - Config + dict:from_list(Config) end, - + NewOpts = dict:merge(fun(_Key, Value1, _Value2) -> + Value1 + end, LocalOpts, Opts), ProviderModules = [], - create_logic_providers(ProviderModules, ParentState#state_t{dir=Dir - ,opts=lists:umerge(LocalOpts, Opts)}). + create_logic_providers(ProviderModules + ,ParentState#state_t{dir=Dir + ,opts=NewOpts + ,default=NewOpts}). get(State, Key) -> - proplists:get_value(Key, State#state_t.opts). + {ok, Value} = dict:find(Key, State#state_t.opts), + Value. get(State, Key, Default) -> - proplists:get_value(Key, State#state_t.opts, Default). + case dict:find(Key, State#state_t.opts) of + {ok, Value} -> + Value; + error -> + Default + end. -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(State=#state_t{opts=Opts}, Key, Value) -> + State#state_t{ opts = dict:store(Key, Value, Opts) }. + +default(#state_t{default=Opts}) -> + Opts. + +default(State, Opts) -> + State#state_t{default=Opts}. + +opts(#state_t{opts=Opts}) -> + Opts. + +current_profile(#state_t{current_profile=Profile}) -> + Profile. + +current_profile(State, Profile) -> + apply_profile(State#state_t{current_profile=Profile}, Profile). + +lock(#state_t{lock=Lock}) -> + Lock. + +lock(State=#state_t{lock=Lock}, App) -> + State#state_t{lock=[App | Lock]}. command_args(#state_t{command_args=CmdArgs}) -> CmdArgs. @@ -89,48 +141,46 @@ command_parsed_args(#state_t{command_parsed_args=CmdArgs}) -> command_parsed_args(State, CmdArgs) -> State#state_t{command_parsed_args=CmdArgs}. +%% Only apply profiles to the default profile +apply_profile(State=#state_t{default=Opts}, Profile) -> + ConfigProfiles = rebar_state:get(State, profiles, []), + Deps = rebar_state:get(State, deps, []), + Opts1 = dict:store({deps, default}, Deps, Opts), + ProfileOpts = dict:from_list(proplists:get_value(Profile, ConfigProfiles, [])), + State#state_t{opts=merge_opts(Profile, ProfileOpts, Opts1)}. + +merge_opts(Profile, Opts1, Opts2) -> + dict:fold(fun(deps, Value, OptsAcc) -> + dict:store({deps, Profile}, Value, OptsAcc); + (Key, Value, OptsAcc) -> + case dict:fetch(Key, Opts2) of + OldValue when is_list(OldValue) -> + case io_lib:printable_list(Value) of + true -> + dict:store(Key, Value, OptsAcc); + false -> + dict:store(Key, lists:keymerge(1, lists:keysort(1, OldValue), lists:keysort(1, Value)), OptsAcc) + end; + _ -> + dict:store(Key, Value, OptsAcc) + end + end, Opts2, Opts1). + dir(#state_t{dir=Dir}) -> Dir. dir(State=#state_t{}, Dir) -> State#state_t{dir=filename:absname(Dir)}. -deps_names(State) -> - Deps = rebar_state:get(State, deps, []), +deps_names(Deps) when is_list(Deps) -> lists:map(fun(Dep) when is_tuple(Dep) -> ec_cnv:to_binary(element(1, Dep)); (Dep) when is_atom(Dep) -> ec_cnv:to_binary(Dep) - end, Deps). - --spec pkg_deps(t()) -> [rebar_packages:package()]. -pkg_deps(#state_t{pkg_deps=PkgDeps}) -> - PkgDeps. - -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. - -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) -> - 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}. + end, Deps); +deps_names(State) -> + Deps = rebar_state:get(State, deps, []), + deps_names(Deps). project_apps(#state_t{project_apps=Apps}) -> Apps. @@ -138,7 +188,21 @@ project_apps(#state_t{project_apps=Apps}) -> project_apps(State=#state_t{}, NewApps) when is_list(NewApps) -> State#state_t{project_apps=NewApps}; project_apps(State=#state_t{project_apps=Apps}, App) -> - State#state_t{project_apps=[App | Apps]}. + State#state_t{project_apps=lists:keystore(rebar_app_info:name(App), 2, Apps, App)}. + +deps_to_build(#state_t{deps_to_build=Apps}) -> + Apps. + +deps_to_build(State=#state_t{}, NewApps) when is_list(NewApps) -> + State#state_t{deps_to_build=NewApps}; +deps_to_build(State=#state_t{deps_to_build=Apps}, App) -> + State#state_t{deps_to_build=lists:keystore(rebar_app_info:name(App), 2, Apps, App)}. + +all_deps(#state_t{all_deps=Apps}) -> + Apps. + +all_deps(State=#state_t{}, NewApps) -> + State#state_t{all_deps=NewApps}. providers(#state_t{providers=Providers}) -> Providers. |