From 98752aab9390137d7cba0b70e0c1c08b9ca308d4 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Wed, 26 Aug 2015 22:13:04 -0500 Subject: wip: move state into app_info --- src/rebar_utils.erl | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'src/rebar_utils.erl') diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index 02a2262..cf6e8e8 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -26,7 +26,10 @@ %% ------------------------------------------------------------------- -module(rebar_utils). --export([sort_deps/1, +-export([get/2, + get/3, + set/3, + sort_deps/1, droplast/1, filtermap/2, is_arch/1, @@ -80,6 +83,21 @@ %% Public API %% ==================================================================== +get(Opts, Key) -> + {ok, Value} = dict:find(Key, Opts), + Value. + +get(Opts, Key, Default) -> + case dict:find(Key, Opts) of + {ok, Value} -> + Value; + error -> + Default + end. + +set(Opts, Key, Value) -> + dict:store(Key, Value, Opts). + sort_deps(Deps) -> %% We need a sort stable, based on the name. So that for multiple deps on %% the same level with the same name, we keep the order the parents had. @@ -219,11 +237,11 @@ deprecated(Old, New, When) -> [Old, Old, New, Old, When]). %% @doc Return list of erl_opts --spec erl_opts(rebar_state:t()) -> list(). -erl_opts(Config) -> - RawErlOpts = filter_defines(rebar_state:get(Config, erl_opts, []), []), +-spec erl_opts(rebar_app_info:t()) -> list(). +erl_opts(AppInfo) -> + RawErlOpts = filter_defines(rebar_utils:get(AppInfo, erl_opts, []), []), Defines = [{d, list_to_atom(D)} || - D <- rebar_state:get(Config, defines, [])], + D <- rebar_utils:get(AppInfo, defines, [])], Opts = Defines ++ RawErlOpts, case proplists:is_defined(no_debug_info, Opts) of true -> -- cgit v1.1 From 32d07ec0767524c8bdf0321c70a741ab64dedd78 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Sat, 29 Aug 2015 21:41:58 -0500 Subject: build on already created AppInfo instead of having to do copy --- src/rebar_utils.erl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src/rebar_utils.erl') diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index cf6e8e8..7363d0a 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -237,17 +237,17 @@ deprecated(Old, New, When) -> [Old, Old, New, Old, When]). %% @doc Return list of erl_opts --spec erl_opts(rebar_app_info:t()) -> list(). -erl_opts(AppInfo) -> - RawErlOpts = filter_defines(rebar_utils:get(AppInfo, erl_opts, []), []), +-spec erl_opts(rebar_dict()) -> list(). +erl_opts(Opts) -> + RawErlOpts = filter_defines(rebar_utils:get(Opts, erl_opts, []), []), Defines = [{d, list_to_atom(D)} || - D <- rebar_utils:get(AppInfo, defines, [])], - Opts = Defines ++ RawErlOpts, - case proplists:is_defined(no_debug_info, Opts) of + D <- rebar_utils:get(Opts, defines, [])], + AllOpts = Defines ++ RawErlOpts, + case proplists:is_defined(no_debug_info, AllOpts) of true -> - [O || O <- Opts, O =/= no_debug_info]; + [O || O <- AllOpts, O =/= no_debug_info]; false -> - [debug_info|Opts] + [debug_info|AllOpts] end. %% for use by `do` task -- cgit v1.1 From 8e25a45cbbc3ba796e3cb4331c15a2914fa0e644 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Sun, 30 Aug 2015 13:39:38 -0500 Subject: update use of hooks and plugins with state in app_info --- src/rebar_utils.erl | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) (limited to 'src/rebar_utils.erl') diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index 7363d0a..9f66181 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -69,7 +69,11 @@ escape_double_quotes_weak/1, check_min_otp_version/1, check_blacklisted_otp_versions/1, - info_useless/2]). + info_useless/2, + apply_overrides/3, + add_to_profile/3, + merge_opts/2, + merge_opts/3]). %% for internal use only -export([otp_release/0]). @@ -397,6 +401,93 @@ abort_if_blacklisted(BlacklistedRegex, OtpRelease) -> [OtpRelease, BlacklistedRegex]) end. +apply_overrides(Opts, Name, Overrides) -> + %% Inefficient. We want the order we get here though. + Opts1 = lists:foldl(fun({override, O}, OptsAcc) -> + lists:foldl(fun({deps, Value}, OptsAcc1) -> + rebar_utils:set(OptsAcc1, {deps,default}, Value); + ({Key, Value}, OptsAcc1) -> + rebar_utils:set(OptsAcc1, Key, Value) + end, OptsAcc, O); + (_, OptsAcc) -> + OptsAcc + end, Opts, Overrides), + + Opts2 = lists:foldl(fun({override, N, O}, OptsAcc) when N =:= Name -> + lists:foldl(fun({deps, Value}, OptsAcc1) -> + rebar_utils:set(OptsAcc1, {deps,default}, Value); + ({Key, Value}, OptsAcc1) -> + rebar_utils:set(OptsAcc1, Key, Value) + end, OptsAcc, O); + (_, OptsAcc) -> + OptsAcc + end, Opts1, Overrides), + + lists:foldl(fun({add, N, O}, OptsAcc) when N =:= Name -> + lists:foldl(fun({deps, Value}, OptsAcc1) -> + OldValue = rebar_utils:get(OptsAcc1, {deps,default}, []), + rebar_utils:set(OptsAcc1, {deps,default}, Value++OldValue); + ({Key, Value}, OptsAcc1) -> + OldValue = rebar_utils:get(OptsAcc1, Key, []), + rebar_utils:set(OptsAcc1, Key, Value++OldValue) + end, OptsAcc, O); + (_, OptsAcc) -> + OptsAcc + end, Opts2, Overrides). + +add_to_profile(Opts, Profile, KVs) when is_atom(Profile), is_list(KVs) -> + Profiles = rebar_utils:get(Opts, profiles, []), + ProfileOpts = dict:from_list(proplists:get_value(Profile, Profiles, [])), + NewOpts = rebar_utils:merge_opts(Profile, dict:from_list(KVs), ProfileOpts), + NewProfiles = [{Profile, dict:to_list(NewOpts)}|lists:keydelete(Profile, 1, Profiles)], + rebar_utils:set(Opts, profiles, NewProfiles). + +merge_opts(Profile, NewOpts, OldOpts) -> + Opts = merge_opts(NewOpts, OldOpts), + + Opts2 = case dict:find(plugins, NewOpts) of + {ok, Value} -> + dict:store({plugins, Profile}, Value, Opts); + error -> + Opts + end, + + case dict:find(deps, NewOpts) of + {ok, Value2} -> + dict:store({deps, Profile}, Value2, Opts2); + error -> + Opts2 + end. + +merge_opts(NewOpts, OldOpts) -> + dict:merge(fun(deps, _NewValue, OldValue) -> + OldValue; + ({deps, _}, NewValue, _OldValue) -> + NewValue; + (plugins, NewValue, _OldValue) -> + NewValue; + ({plugins, _}, NewValue, _OldValue) -> + NewValue; + (profiles, NewValue, OldValue) -> + dict:to_list(merge_opts(dict:from_list(NewValue), dict:from_list(OldValue))); + (_Key, NewValue, OldValue) when is_list(NewValue) -> + case io_lib:printable_list(NewValue) of + true when NewValue =:= [] -> + case io_lib:printable_list(OldValue) of + true -> + NewValue; + false -> + OldValue + end; + true -> + NewValue; + false -> + rebar_utils:tup_umerge(rebar_utils:tup_sort(NewValue) + ,rebar_utils:tup_sort(OldValue)) + end; + (_Key, NewValue, _OldValue) -> + NewValue + end, NewOpts, OldOpts). %% ==================================================================== %% Internal functions -- cgit v1.1 From e6f6ccc7570ad57f6d1416b3f8c59551b7aaa5c6 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Mon, 31 Aug 2015 21:40:33 -0500 Subject: more cleanup --- src/rebar_utils.erl | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) (limited to 'src/rebar_utils.erl') diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index 9f66181..7260f6d 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -269,20 +269,23 @@ deps_to_binary([{Name, Source} | T]) -> deps_to_binary([Name | T]) -> [ec_cnv:to_binary(Name) | deps_to_binary(T)]. -tup_dedup([]) -> +tup_dedup(List) -> + tup_dedup_(tup_sort(List)). + +tup_dedup_([]) -> []; -tup_dedup([A]) -> +tup_dedup_([A]) -> [A]; -tup_dedup([A,B|T]) when element(1, A) =:= element(1, B) -> - tup_dedup([A | T]); -tup_dedup([A,B|T]) when element(1, A) =:= B -> - tup_dedup([A | T]); -tup_dedup([A,B|T]) when A =:= element(1, B) -> - tup_dedup([A | T]); -tup_dedup([A,A|T]) -> - [A|tup_dedup(T)]; -tup_dedup([A|T]) -> - [A|tup_dedup(T)]. +tup_dedup_([A,B|T]) when element(1, A) =:= element(1, B) -> + tup_dedup_([A | T]); +tup_dedup_([A,B|T]) when element(1, A) =:= B -> + tup_dedup_([A | T]); +tup_dedup_([A,B|T]) when A =:= element(1, B) -> + tup_dedup_([A | T]); +tup_dedup_([A,A|T]) -> + [A|tup_dedup_(T)]; +tup_dedup_([A|T]) -> + [A|tup_dedup_(T)]. %% Sort the list in proplist-order, meaning that `{a,b}' and `{a,c}' %% both compare as usual, and `a' and `b' do the same, but `a' and `{a,b}' will @@ -311,9 +314,12 @@ tup_sort(List) -> %% %% This lets us apply proper overrides to list of elements according to profile %% priority. This function depends on a stable proplist sort. -tup_umerge([], Olds) -> +tup_umerge(NewList, OldList) -> + tup_umerge_(tup_sort(NewList), tup_sort(OldList)). + +tup_umerge_([], Olds) -> Olds; -tup_umerge([New|News], Olds) -> +tup_umerge_([New|News], Olds) -> lists:reverse(umerge(News, Olds, [], New)). tup_find(_Elem, []) -> -- cgit v1.1 From d034f40c38753860c2da894f1966af5399f3efce Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Tue, 1 Sep 2015 17:48:04 -0500 Subject: move opts functions to new module rebar_opts --- src/rebar_utils.erl | 152 +--------------------------------------------------- 1 file changed, 2 insertions(+), 150 deletions(-) (limited to 'src/rebar_utils.erl') diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index 7260f6d..564b384 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -26,10 +26,7 @@ %% ------------------------------------------------------------------- -module(rebar_utils). --export([get/2, - get/3, - set/3, - sort_deps/1, +-export([sort_deps/1, droplast/1, filtermap/2, is_arch/1, @@ -48,7 +45,6 @@ vcs_vsn/3, deprecated/3, deprecated/4, - erl_opts/1, indent/1, update_code/1, remove_from_code_path/1, @@ -69,11 +65,7 @@ escape_double_quotes_weak/1, check_min_otp_version/1, check_blacklisted_otp_versions/1, - info_useless/2, - apply_overrides/3, - add_to_profile/3, - merge_opts/2, - merge_opts/3]). + info_useless/2]). %% for internal use only -export([otp_release/0]). @@ -87,21 +79,6 @@ %% Public API %% ==================================================================== -get(Opts, Key) -> - {ok, Value} = dict:find(Key, Opts), - Value. - -get(Opts, Key, Default) -> - case dict:find(Key, Opts) of - {ok, Value} -> - Value; - error -> - Default - end. - -set(Opts, Key, Value) -> - dict:store(Key, Value, Opts). - sort_deps(Deps) -> %% We need a sort stable, based on the name. So that for multiple deps on %% the same level with the same name, we keep the order the parents had. @@ -240,20 +217,6 @@ deprecated(Old, New, When) -> "'~p' will be removed ~s.~n">>, [Old, Old, New, Old, When]). -%% @doc Return list of erl_opts --spec erl_opts(rebar_dict()) -> list(). -erl_opts(Opts) -> - RawErlOpts = filter_defines(rebar_utils:get(Opts, erl_opts, []), []), - Defines = [{d, list_to_atom(D)} || - D <- rebar_utils:get(Opts, defines, [])], - AllOpts = Defines ++ RawErlOpts, - case proplists:is_defined(no_debug_info, AllOpts) of - true -> - [O || O <- AllOpts, O =/= no_debug_info]; - false -> - [debug_info|AllOpts] - end. - %% for use by `do` task %% note: this does not handle the case where you have an argument that @@ -407,94 +370,6 @@ abort_if_blacklisted(BlacklistedRegex, OtpRelease) -> [OtpRelease, BlacklistedRegex]) end. -apply_overrides(Opts, Name, Overrides) -> - %% Inefficient. We want the order we get here though. - Opts1 = lists:foldl(fun({override, O}, OptsAcc) -> - lists:foldl(fun({deps, Value}, OptsAcc1) -> - rebar_utils:set(OptsAcc1, {deps,default}, Value); - ({Key, Value}, OptsAcc1) -> - rebar_utils:set(OptsAcc1, Key, Value) - end, OptsAcc, O); - (_, OptsAcc) -> - OptsAcc - end, Opts, Overrides), - - Opts2 = lists:foldl(fun({override, N, O}, OptsAcc) when N =:= Name -> - lists:foldl(fun({deps, Value}, OptsAcc1) -> - rebar_utils:set(OptsAcc1, {deps,default}, Value); - ({Key, Value}, OptsAcc1) -> - rebar_utils:set(OptsAcc1, Key, Value) - end, OptsAcc, O); - (_, OptsAcc) -> - OptsAcc - end, Opts1, Overrides), - - lists:foldl(fun({add, N, O}, OptsAcc) when N =:= Name -> - lists:foldl(fun({deps, Value}, OptsAcc1) -> - OldValue = rebar_utils:get(OptsAcc1, {deps,default}, []), - rebar_utils:set(OptsAcc1, {deps,default}, Value++OldValue); - ({Key, Value}, OptsAcc1) -> - OldValue = rebar_utils:get(OptsAcc1, Key, []), - rebar_utils:set(OptsAcc1, Key, Value++OldValue) - end, OptsAcc, O); - (_, OptsAcc) -> - OptsAcc - end, Opts2, Overrides). - -add_to_profile(Opts, Profile, KVs) when is_atom(Profile), is_list(KVs) -> - Profiles = rebar_utils:get(Opts, profiles, []), - ProfileOpts = dict:from_list(proplists:get_value(Profile, Profiles, [])), - NewOpts = rebar_utils:merge_opts(Profile, dict:from_list(KVs), ProfileOpts), - NewProfiles = [{Profile, dict:to_list(NewOpts)}|lists:keydelete(Profile, 1, Profiles)], - rebar_utils:set(Opts, profiles, NewProfiles). - -merge_opts(Profile, NewOpts, OldOpts) -> - Opts = merge_opts(NewOpts, OldOpts), - - Opts2 = case dict:find(plugins, NewOpts) of - {ok, Value} -> - dict:store({plugins, Profile}, Value, Opts); - error -> - Opts - end, - - case dict:find(deps, NewOpts) of - {ok, Value2} -> - dict:store({deps, Profile}, Value2, Opts2); - error -> - Opts2 - end. - -merge_opts(NewOpts, OldOpts) -> - dict:merge(fun(deps, _NewValue, OldValue) -> - OldValue; - ({deps, _}, NewValue, _OldValue) -> - NewValue; - (plugins, NewValue, _OldValue) -> - NewValue; - ({plugins, _}, NewValue, _OldValue) -> - NewValue; - (profiles, NewValue, OldValue) -> - dict:to_list(merge_opts(dict:from_list(NewValue), dict:from_list(OldValue))); - (_Key, NewValue, OldValue) when is_list(NewValue) -> - case io_lib:printable_list(NewValue) of - true when NewValue =:= [] -> - case io_lib:printable_list(OldValue) of - true -> - NewValue; - false -> - OldValue - end; - true -> - NewValue; - false -> - rebar_utils:tup_umerge(rebar_utils:tup_sort(NewValue) - ,rebar_utils:tup_sort(OldValue)) - end; - (_Key, NewValue, _OldValue) -> - NewValue - end, NewOpts, OldOpts). - %% ==================================================================== %% Internal functions %% ==================================================================== @@ -759,29 +634,6 @@ find_resource_module(Type, Resources) -> {ok, Module} end. -%% -%% Filter a list of erl_opts platform_define options such that only -%% those which match the provided architecture regex are returned. -%% -filter_defines([], Acc) -> - lists:reverse(Acc); -filter_defines([{platform_define, ArchRegex, Key} | Rest], Acc) -> - case rebar_utils:is_arch(ArchRegex) of - true -> - filter_defines(Rest, [{d, Key} | Acc]); - false -> - filter_defines(Rest, Acc) - end; -filter_defines([{platform_define, ArchRegex, Key, Value} | Rest], Acc) -> - case rebar_utils:is_arch(ArchRegex) of - true -> - filter_defines(Rest, [{d, Key, Value} | Acc]); - false -> - filter_defines(Rest, Acc) - end; -filter_defines([Opt | Rest], Acc) -> - filter_defines(Rest, [Opt | Acc]). - %% @doc ident to the level specified -spec indent(non_neg_integer()) -> iolist(). indent(Amount) when erlang:is_integer(Amount) -> -- cgit v1.1