diff options
Diffstat (limited to 'src/rebar_state.erl')
-rw-r--r-- | src/rebar_state.erl | 56 |
1 files changed, 40 insertions, 16 deletions
diff --git a/src/rebar_state.erl b/src/rebar_state.erl index ddac9d2..7a6e60d 100644 --- a/src/rebar_state.erl +++ b/src/rebar_state.erl @@ -69,25 +69,28 @@ -spec new() -> t(). new() -> - #state_t{dir = rebar_dir:get_cwd()}. + BaseState = base_state(), + BaseState#state_t{dir = rebar_dir:get_cwd()}. -spec new(list()) -> t(). new(Config) when is_list(Config) -> + BaseState = base_state(), Deps = proplists:get_value(deps, Config, []), Opts = dict:from_list([{{deps, default}, Deps} | Config]), - #state_t { dir = rebar_dir:get_cwd(), - default = Opts, - opts = Opts }. + BaseState#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) -> + BaseState = base_state(), Deps = proplists:get_value(deps, Config, []), Opts = dict:from_list([{{deps, default}, Deps} | Config]), - #state_t { dir = rebar_dir:get_cwd(), - current_profiles = [Profile], - default = Opts, - opts = Opts }; + BaseState#state_t { dir = rebar_dir:get_cwd(), + current_profiles = [Profile], + default = Opts, + opts = Opts }; new(ParentState=#state_t{}, Config) -> %% Load terms from rebar.config, if it exists Dir = rebar_dir:get_cwd(), @@ -113,6 +116,15 @@ new(ParentState, Config, Dir) -> ,opts=NewOpts ,default=NewOpts}. +base_state() -> + case application:get_env(rebar, resources) of + undefined -> + Resources = []; + {ok, Resources} -> + Resources + end, + #state_t{resources=Resources}. + get(State, Key) -> {ok, Value} = dict:find(Key, State#state_t.opts), Value. @@ -229,16 +241,28 @@ apply_profiles(State, Profile) when not is_list(Profile) -> apply_profiles(State, [Profile]); apply_profiles(State, [default]) -> State; -apply_profiles(State=#state_t{opts=Opts, current_profiles=CurrentProfiles}, Profiles) -> +apply_profiles(State=#state_t{default = Defaults, current_profiles=CurrentProfiles}, Profiles) -> + AppliedProfiles = deduplicate(CurrentProfiles ++ Profiles), ConfigProfiles = rebar_state:get(State, profiles, []), - {Profiles1, NewOpts} = - lists:foldl(fun(default, {ProfilesAcc, OptsAcc}) -> - {ProfilesAcc, OptsAcc}; - (Profile, {ProfilesAcc, OptsAcc}) -> + NewOpts = + lists:foldl(fun(default, OptsAcc) -> + OptsAcc; + (Profile, OptsAcc) -> ProfileOpts = dict:from_list(proplists:get_value(Profile, ConfigProfiles, [])), - {[Profile]++ProfilesAcc, merge_opts(Profile, ProfileOpts, OptsAcc)} - end, {[], Opts}, Profiles), - State#state_t{current_profiles=CurrentProfiles++Profiles1, opts=NewOpts}. + merge_opts(Profile, ProfileOpts, OptsAcc) + end, Defaults, AppliedProfiles), + State#state_t{current_profiles = AppliedProfiles, opts=NewOpts}. + +deduplicate(Profiles) -> + do_deduplicate(lists:reverse(Profiles), []). + +do_deduplicate([], Acc) -> + Acc; +do_deduplicate([Head | Rest], Acc) -> + case lists:member(Head, Acc) of + true -> do_deduplicate(Rest, Acc); + false -> do_deduplicate(Rest, [Head | Acc]) + end. merge_opts(Profile, NewOpts, OldOpts) -> Opts = merge_opts(NewOpts, OldOpts), |