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_app_discover.erl | 2 +- src/rebar_app_info.erl | 8 +-- src/rebar_dir.erl | 8 +-- src/rebar_erlc_compiler.erl | 16 ++--- src/rebar_hooks.erl | 4 +- src/rebar_opts.erl | 155 ++++++++++++++++++++++++++++++++++++++++++++ src/rebar_state.erl | 6 +- src/rebar_utils.erl | 152 +------------------------------------------ 8 files changed, 179 insertions(+), 172 deletions(-) create mode 100644 src/rebar_opts.erl diff --git a/src/rebar_app_discover.erl b/src/rebar_app_discover.erl index 1052774..e702786 100644 --- a/src/rebar_app_discover.erl +++ b/src/rebar_app_discover.erl @@ -138,7 +138,7 @@ maybe_reset_hooks(C, Dir, State) -> reset_hooks(Opts) -> lists:foldl(fun(Key, OptsAcc) -> - rebar_utils:set(OptsAcc, Key, []) + rebar_opts:set(OptsAcc, Key, []) end, Opts, [post_hooks, pre_hooks, provider_hooks, artifacts]). -spec all_app_dirs(list(file:name())) -> list(file:name()). diff --git a/src/rebar_app_info.erl b/src/rebar_app_info.erl index 1e5710f..268b09d 100644 --- a/src/rebar_app_info.erl +++ b/src/rebar_app_info.erl @@ -156,7 +156,7 @@ update_opts(AppInfo, Opts, Config) -> true = rebar_config:verify_config_format(Terms), LocalOpts = dict:from_list(Terms), - NewOpts = rebar_utils:merge_opts(LocalOpts, Opts), + NewOpts = rebar_opts:merge_opts(LocalOpts, Opts), AppInfo#app_info_t{opts=NewOpts ,default=NewOpts}. @@ -425,11 +425,11 @@ all(Dir, [File|Artifacts]) -> apply_overrides(Overrides, AppInfo) -> Name = binary_to_atom(rebar_app_info:name(AppInfo), utf8), - Opts = rebar_utils:apply_overrides(opts(AppInfo), Name, Overrides), + Opts = rebar_opts:apply_overrides(opts(AppInfo), Name, Overrides), AppInfo#app_info_t{default=Opts, opts=Opts}. add_to_profile(AppInfo, Profile, KVs) when is_atom(Profile), is_list(KVs) -> - Opts = rebar_utils:add_to_profile(opts(AppInfo), Profile, KVs), + Opts = rebar_opts:add_to_profile(opts(AppInfo), Profile, KVs), AppInfo#app_info_t{opts=Opts}. apply_profiles(AppInfo, Profile) when not is_list(Profile) -> @@ -456,7 +456,7 @@ apply_profiles(AppInfo=#app_info_t{default = Defaults, profiles=CurrentProfiles} case proplists:get_value(Profile, ConfigProfiles, []) of OptsList when is_list(OptsList) -> ProfileOpts = dict:from_list(OptsList), - rebar_utils:merge_opts(Profile, ProfileOpts, OptsAcc); + rebar_opts:merge_opts(Profile, ProfileOpts, OptsAcc); Other -> throw(?PRV_ERROR({profile_not_list, Profile, Other})) end diff --git a/src/rebar_dir.erl b/src/rebar_dir.erl index 39fa723..7aeb11e 100644 --- a/src/rebar_dir.erl +++ b/src/rebar_dir.erl @@ -134,9 +134,9 @@ src_dirs(Opts) -> src_dirs(Opts, []). -spec src_dirs(rebar_dict(), list(file:filename_all())) -> list(file:filename_all()). src_dirs(Opts, Default) -> - ErlOpts = rebar_utils:erl_opts(Opts), + ErlOpts = rebar_opts:erl_opts(Opts), Vs = proplists:get_all_values(src_dirs, ErlOpts), - case lists:append([rebar_utils:get(Opts, src_dirs, []) | Vs]) of + case lists:append([rebar_opts:get(Opts, src_dirs, []) | Vs]) of [] -> Default; Dirs -> Dirs end. @@ -146,9 +146,9 @@ extra_src_dirs(Opts) -> extra_src_dirs(Opts, []). -spec extra_src_dirs(rebar_dict(), list(file:filename_all())) -> list(file:filename_all()). extra_src_dirs(Opts, Default) -> - ErlOpts = rebar_utils:erl_opts(Opts), + ErlOpts = rebar_opts:erl_opts(Opts), Vs = proplists:get_all_values(extra_src_dirs, ErlOpts), - case lists:append([rebar_utils:get(Opts, extra_src_dirs, []) | Vs]) of + case lists:append([rebar_opts:get(Opts, extra_src_dirs, []) | Vs]) of [] -> Default; Dirs -> Dirs end. diff --git a/src/rebar_erlc_compiler.erl b/src/rebar_erlc_compiler.erl index 869ade3..0fc5455 100644 --- a/src/rebar_erlc_compiler.erl +++ b/src/rebar_erlc_compiler.erl @@ -88,17 +88,17 @@ compile(AppInfo) -> -spec compile(rebar_dict(), file:name(), file:name()) -> 'ok'. compile(Opts, Dir, OutDir) -> rebar_base_compiler:run(Opts, - check_files(rebar_utils:get( + check_files(rebar_opts:get( Opts, xrl_first_files, [])), filename:join(Dir, "src"), ".xrl", filename:join(Dir, "src"), ".erl", fun compile_xrl/3), rebar_base_compiler:run(Opts, - check_files(rebar_utils:get( + check_files(rebar_opts:get( Opts, yrl_first_files, [])), filename:join(Dir, "src"), ".yrl", filename:join(Dir, "src"), ".erl", fun compile_yrl/3), rebar_base_compiler:run(Opts, - check_files(rebar_utils:get( + check_files(rebar_opts:get( Opts, mib_first_files, [])), filename:join(Dir, "mibs"), ".mib", filename:join([Dir, "priv", "mibs"]), ".bin", fun compile_mib/3), @@ -136,7 +136,7 @@ clean(_Opts, AppDir) -> -spec doterl_compile(rebar_dict(), file:filename(), file:filename()) -> ok. doterl_compile(Opts, Dir, ODir) -> - ErlOpts = rebar_utils:erl_opts(Opts), + ErlOpts = rebar_opts:erl_opts(Opts), doterl_compile(Opts, Dir, ODir, [], ErlOpts). doterl_compile(Opts, Dir, OutDir, MoreSources, ErlOpts) -> @@ -184,7 +184,7 @@ doterl_compile(Opts, Dir, OutDir, MoreSources, ErlOpts) -> %% and parse_transform options. Also produce specific erl_opts for these first %% files, so that yet to be compiled parse transformations are excluded from it. erl_first_files(Opts, ErlOpts, Dir, NeededErlFiles) -> - ErlFirstFilesConf = rebar_utils:get(Opts, erl_first_files, []), + ErlFirstFilesConf = rebar_opts:get(Opts, erl_first_files, []), NeededSrcDirs = lists:usort(lists:map(fun filename:dirname/1, NeededErlFiles)), %% NOTE: order of files here is important! ErlFirstFiles = @@ -430,7 +430,7 @@ compile_mib(Source, Target, Opts) -> ok = filelib:ensure_dir(filename:join([Dir, "include", "dummy.hrl"])), AllOpts = [{outdir, Dir} ,{i, [Dir]}] ++ - rebar_utils:get(Opts, mib_opts, []), + rebar_opts:get(Opts, mib_opts, []), case snmpc:compile(Source, AllOpts) of {ok, _} -> @@ -453,13 +453,13 @@ compile_mib(Source, Target, Opts) -> -spec compile_xrl(file:filename(), file:filename(), rebar_dict()) -> 'ok'. compile_xrl(Source, Target, Opts) -> - AllOpts = [{scannerfile, Target} | rebar_utils:get(Opts, xrl_opts, [])], + AllOpts = [{scannerfile, Target} | rebar_opts:get(Opts, xrl_opts, [])], compile_xrl_yrl(Opts, Source, Target, AllOpts, leex). -spec compile_yrl(file:filename(), file:filename(), rebar_dict()) -> 'ok'. compile_yrl(Source, Target, Opts) -> - AllOpts = [{parserfile, Target} | rebar_utils:get(Opts, yrl_opts, [])], + AllOpts = [{parserfile, Target} | rebar_opts:get(Opts, yrl_opts, [])], compile_xrl_yrl(Opts, Source, Target, AllOpts, yecc). -spec compile_xrl_yrl(rebar_dict(), file:filename(), diff --git a/src/rebar_hooks.erl b/src/rebar_hooks.erl index cb44b81..6db3c77 100644 --- a/src/rebar_hooks.erl +++ b/src/rebar_hooks.erl @@ -19,7 +19,7 @@ run_all_hooks(Dir, Type, Command, Providers, State) -> run_hooks(Dir, Type, Command, rebar_state:opts(State), State). run_provider_hooks(Dir, Type, Command, Providers, Opts, State) -> - case rebar_utils:get(Opts, provider_hooks, []) of + case rebar_opts:get(Opts, provider_hooks, []) of [] -> ok; AllHooks -> @@ -76,7 +76,7 @@ run_hooks(Dir, pre, Command, Opts, State) -> run_hooks(Dir, post, Command, Opts, State) -> run_hooks(Dir, post_hooks, Command, Opts, State); run_hooks(Dir, Type, Command, Opts, State) -> - case rebar_utils:get(Opts, Type, []) of + case rebar_opts:get(Opts, Type, []) of [] -> ok; Hooks -> diff --git a/src/rebar_opts.erl b/src/rebar_opts.erl new file mode 100644 index 0000000..47451c5 --- /dev/null +++ b/src/rebar_opts.erl @@ -0,0 +1,155 @@ +-module(rebar_opts). + +-export([get/2, + get/3, + set/3, + + erl_opts/1, + + apply_overrides/3, + add_to_profile/3, + merge_opts/2, + merge_opts/3]). + +-include("rebar.hrl"). + +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). + +%% @doc Return list of erl_opts +-spec erl_opts(rebar_dict()) -> list(). +erl_opts(Opts) -> + RawErlOpts = filter_defines(?MODULE:get(Opts, erl_opts, []), []), + Defines = [{d, list_to_atom(D)} || + D <- ?MODULE: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. + +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) -> + set(OptsAcc1, {deps,default}, Value); + ({Key, Value}, OptsAcc1) -> + 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) -> + set(OptsAcc1, {deps,default}, Value); + ({Key, Value}, OptsAcc1) -> + 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 = ?MODULE:get(OptsAcc1, {deps,default}, []), + set(OptsAcc1, {deps,default}, Value++OldValue); + ({Key, Value}, OptsAcc1) -> + OldValue = ?MODULE:get(OptsAcc1, Key, []), + 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 = ?MODULE:get(Opts, profiles, []), + ProfileOpts = dict:from_list(proplists:get_value(Profile, Profiles, [])), + NewOpts = merge_opts(Profile, dict:from_list(KVs), ProfileOpts), + NewProfiles = [{Profile, dict:to_list(NewOpts)}|lists:keydelete(Profile, 1, Profiles)], + 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(NewValue, OldValue) + end; + (_Key, NewValue, _OldValue) -> + NewValue + end, NewOpts, OldOpts). + +%% Internal functions + +%% +%% 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]). diff --git a/src/rebar_state.erl b/src/rebar_state.erl index 4a95a9a..2a5aa01 100644 --- a/src/rebar_state.erl +++ b/src/rebar_state.erl @@ -105,7 +105,7 @@ new(ParentState, Config, Deps, Dir) -> true = rebar_config:verify_config_format(Terms), LocalOpts = dict:from_list(Terms), - NewOpts = rebar_utils:merge_opts(LocalOpts, Opts), + NewOpts = rebar_opts:merge_opts(LocalOpts, Opts), ParentState#state_t{dir=Dir ,opts=NewOpts @@ -241,7 +241,7 @@ command_parsed_args(State, CmdArgs) -> State#state_t{command_parsed_args=CmdArgs}. add_to_profile(State, Profile, KVs) when is_atom(Profile), is_list(KVs) -> - Opts = rebar_utils:add_to_profile(opts(State), Profile, KVs), + Opts = rebar_opts:add_to_profile(opts(State), Profile, KVs), State#state_t{opts=Opts}. apply_profiles(State, Profile) when not is_list(Profile) -> @@ -268,7 +268,7 @@ apply_profiles(State=#state_t{default = Defaults, current_profiles=CurrentProfil case proplists:get_value(Profile, ConfigProfiles, []) of OptsList when is_list(OptsList) -> ProfileOpts = dict:from_list(OptsList), - rebar_utils:merge_opts(Profile, ProfileOpts, OptsAcc); + rebar_opts:merge_opts(Profile, ProfileOpts, OptsAcc); Other -> throw(?PRV_ERROR({profile_not_list, Profile, Other})) end 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