From cf03345486f5e3b09960deb861c07e2d1dcca3a1 Mon Sep 17 00:00:00 2001 From: Serge Aleynikov Date: Fri, 18 Dec 2015 08:38:54 -0500 Subject: Add a state display provider The provider is used for debugging to help displaying current rebar's state. Usage: rebar3 state --- README.md | 1 + src/rebar.app.src | 1 + src/rebar3.erl | 9 +++++---- src/rebar_hooks.erl | 1 + src/rebar_prv_state.erl | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/rebar_state.erl | 10 ++++++++++ 6 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 src/rebar_prv_state.erl diff --git a/README.md b/README.md index cddb843..873a990 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ locations ([hex.pm](http://hex.pm), git, hg, and so on). | relup | Creates relup from 2 releases | | report | Report on environment and versions for bug reports | | shell | Run shell with project apps in path | +| state | Display configuration state | | tar | Package release into tarball | | tree | Print dependency tree | | unlock | Unlock dependencies | diff --git a/src/rebar.app.src b/src/rebar.app.src index 58fee02..bd0f871 100644 --- a/src/rebar.app.src +++ b/src/rebar.app.src @@ -66,6 +66,7 @@ rebar_prv_relup, rebar_prv_report, rebar_prv_shell, + rebar_prv_state, rebar_prv_tar, rebar_prv_unlock, rebar_prv_update, diff --git a/src/rebar3.erl b/src/rebar3.erl index 9106bb8..01d3d7f 100644 --- a/src/rebar3.erl +++ b/src/rebar3.erl @@ -247,10 +247,11 @@ set_global_flag(State, Options, Flag) -> %% global_option_spec_list() -> [ - %% {Name, ShortOpt, LongOpt, ArgSpec, HelpMsg} - {help, $h, "help", undefined, "Print this help."}, - {version, $v, "version", undefined, "Show version information."}, - {task, undefined, undefined, string, "Task to run."} + %% {Name, ShortOpt, LongOpt, ArgSpec, HelpMsg} + {help, $h, "help", undefined, "Print this help."}, + {version, $v, "version", undefined, "Show version information."}, + {task, undefined, undefined, string, "Task to run."}, + {state, undefined, "state", undefined, "Display configuration state"} ]. handle_error(rebar_abort) -> diff --git a/src/rebar_hooks.erl b/src/rebar_hooks.erl index 4e6d486..a5ab048 100644 --- a/src/rebar_hooks.erl +++ b/src/rebar_hooks.erl @@ -81,6 +81,7 @@ run_hooks(Dir, post, Command, Opts, State) -> run_hooks(Dir, Type, Command, Opts, State) -> case rebar_opts:get(Opts, Type, []) of [] -> + ?DEBUG("run_hooks(~p, ~p, ~p) -> no hooks defined\n", [Dir, Type, Command]), ok; Hooks -> Env = create_env(State, Opts), diff --git a/src/rebar_prv_state.erl b/src/rebar_prv_state.erl new file mode 100644 index 0000000..0e2ca21 --- /dev/null +++ b/src/rebar_prv_state.erl @@ -0,0 +1,44 @@ +%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*- +%% ex: ts=4 sw=4 et + +-module(rebar_prv_state). + +-behaviour(provider). + +-export([init/1, + do/1, + format_error/1]). + +-include("rebar.hrl"). + +-define(PROVIDER, state). +-define(DEPS, []). + +%% =================================================================== +%% Public API +%% =================================================================== + +-spec init(rebar_state:t()) -> {ok, rebar_state:t()}. +init(State) -> + Provider = providers:create( + [{name, ?PROVIDER}, + {module, ?MODULE}, + {bare, true}, + {deps, ?DEPS}, + {example, "rebar3 state"}, + {short_desc, "Print current configuration state"}, + {desc, "Display rebar configuration for debugging purpose"}, + {opts, []}]), + State1 = rebar_state:add_provider(State, Provider), + {ok, State1}. + +-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}. +do(State) -> + L = rebar_state:to_list(State), + io:put_chars("State:\n"), + [io:format(" ~w: ~p\n", [K, V]) || {K,V} <- L], + {ok, State}. + +-spec format_error(any()) -> iolist(). +format_error(Reason) -> + io_lib:format("~p", [Reason]). diff --git a/src/rebar_state.erl b/src/rebar_state.erl index 0c07b2a..a97eca5 100644 --- a/src/rebar_state.erl +++ b/src/rebar_state.erl @@ -36,6 +36,7 @@ deps_names/1, + to_list/1, resources/1, resources/2, add_resource/2, providers/1, providers/2, add_provider/2]). @@ -406,6 +407,15 @@ create_logic_providers(ProviderModules, State0) -> throw({error, "Failed creating providers. Run with DEBUG=1 for stacktrace."}) end. +to_list(#state_t{opts=O, code_paths=CP, default=D} = State) -> + O1 = dict:to_list(O), + CP1 = dict:to_list(CP), + D1 = dict:to_list(D), + State1 = State#state_t{opts=O1, code_paths=CP1, default=D1}, + Fields = record_info(fields, state_t), + Values = tl(tuple_to_list(State1)), + lists:zip(Fields, Values). + %% =================================================================== %% Internal functions %% =================================================================== -- cgit v1.1 From 7b4ceecfdbf60be0d341319f4a675f08951fe8c1 Mon Sep 17 00:00:00 2001 From: Serge Aleynikov Date: Sat, 19 Dec 2015 00:14:17 -0500 Subject: Change output to use ?CONSOLE marco --- src/rebar_prv_state.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rebar_prv_state.erl b/src/rebar_prv_state.erl index 0e2ca21..244e0d0 100644 --- a/src/rebar_prv_state.erl +++ b/src/rebar_prv_state.erl @@ -36,7 +36,7 @@ init(State) -> do(State) -> L = rebar_state:to_list(State), io:put_chars("State:\n"), - [io:format(" ~w: ~p\n", [K, V]) || {K,V} <- L], + [?CONSOLE(" ~w: ~p\n", [K, V]) || {K,V} <- L], {ok, State}. -spec format_error(any()) -> iolist(). -- cgit v1.1 From 632db19470fadf698c36668ffe10f4a0d099a598 Mon Sep 17 00:00:00 2001 From: Serge Aleynikov Date: Sat, 19 Dec 2015 00:17:18 -0500 Subject: Mention in README that state provider is for debugging --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 873a990..14e8b61 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ locations ([hex.pm](http://hex.pm), git, hg, and so on). | relup | Creates relup from 2 releases | | report | Report on environment and versions for bug reports | | shell | Run shell with project apps in path | -| state | Display configuration state | +| state | Display configuration state for debugging purposes | | tar | Package release into tarball | | tree | Print dependency tree | | unlock | Unlock dependencies | -- cgit v1.1 From 8497a50fcef78e50a24518a10f5fb8b05015482d Mon Sep 17 00:00:00 2001 From: Serge Aleynikov Date: Sat, 19 Dec 2015 00:19:33 -0500 Subject: Change output to use ?CONSOLE macro --- src/rebar_prv_state.erl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rebar_prv_state.erl b/src/rebar_prv_state.erl index 244e0d0..dc75c7a 100644 --- a/src/rebar_prv_state.erl +++ b/src/rebar_prv_state.erl @@ -35,8 +35,8 @@ init(State) -> -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}. do(State) -> L = rebar_state:to_list(State), - io:put_chars("State:\n"), - [?CONSOLE(" ~w: ~p\n", [K, V]) || {K,V} <- L], + ?CONSOLE("State:", []), + [?CONSOLE(" ~w: ~p", [K, V]) || {K,V} <- L], {ok, State}. -spec format_error(any()) -> iolist(). -- cgit v1.1 From 9cd788006956d384edd91868fca906d2b7be799b Mon Sep 17 00:00:00 2001 From: Serge Aleynikov Date: Sat, 19 Dec 2015 11:48:55 -0500 Subject: Remove --state argument option per Tristan's comment --- src/rebar3.erl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/rebar3.erl b/src/rebar3.erl index 01d3d7f..90f2845 100644 --- a/src/rebar3.erl +++ b/src/rebar3.erl @@ -250,8 +250,7 @@ global_option_spec_list() -> %% {Name, ShortOpt, LongOpt, ArgSpec, HelpMsg} {help, $h, "help", undefined, "Print this help."}, {version, $v, "version", undefined, "Show version information."}, - {task, undefined, undefined, string, "Task to run."}, - {state, undefined, "state", undefined, "Display configuration state"} + {task, undefined, undefined, string, "Task to run."} ]. handle_error(rebar_abort) -> -- cgit v1.1 From ce1dba7f437a5970f97076c3ec0d209b757f104e Mon Sep 17 00:00:00 2001 From: Serge Aleynikov Date: Sat, 19 Dec 2015 12:15:03 -0500 Subject: Improve recursive display of dictionary values --- src/rebar_state.erl | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/rebar_state.erl b/src/rebar_state.erl index a97eca5..7d301ea 100644 --- a/src/rebar_state.erl +++ b/src/rebar_state.erl @@ -407,14 +407,21 @@ create_logic_providers(ProviderModules, State0) -> throw({error, "Failed creating providers. Run with DEBUG=1 for stacktrace."}) end. -to_list(#state_t{opts=O, code_paths=CP, default=D} = State) -> - O1 = dict:to_list(O), - CP1 = dict:to_list(CP), - D1 = dict:to_list(D), - State1 = State#state_t{opts=O1, code_paths=CP1, default=D1}, +to_list(#state_t{} = State) -> Fields = record_info(fields, state_t), - Values = tl(tuple_to_list(State1)), - lists:zip(Fields, Values). + Values = tl(tuple_to_list(State)), + DictSz = tuple_size(dict:new()), + Fun = fun + F({K,V}) when is_list(V) -> + {K, [F(I) || I <- V]}; + F(V) when is_tuple(V), element(1,V) =:= dict, tuple_size(V) =:= DictSz -> + [F(I) || I <- dict:to_list(V)]; + F({K,V}) when is_tuple(V), element(1,V) =:= dict, tuple_size(V) =:= DictSz -> + {K, [F(I) || I <- dict:to_list(V)]}; + F(Other) -> + Other + end, + lists:zip(Fields, [Fun(I) || I <- Values]). %% =================================================================== %% Internal functions -- cgit v1.1 From 43bca6d2973c4cf27d182e4dad7e260fac036314 Mon Sep 17 00:00:00 2001 From: Serge Aleynikov Date: Sat, 19 Dec 2015 12:23:34 -0500 Subject: Turn functor into a function to support older Erlang VM --- src/rebar_state.erl | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/rebar_state.erl b/src/rebar_state.erl index 7d301ea..9c293f5 100644 --- a/src/rebar_state.erl +++ b/src/rebar_state.erl @@ -411,17 +411,16 @@ to_list(#state_t{} = State) -> Fields = record_info(fields, state_t), Values = tl(tuple_to_list(State)), DictSz = tuple_size(dict:new()), - Fun = fun - F({K,V}) when is_list(V) -> - {K, [F(I) || I <- V]}; - F(V) when is_tuple(V), element(1,V) =:= dict, tuple_size(V) =:= DictSz -> - [F(I) || I <- dict:to_list(V)]; - F({K,V}) when is_tuple(V), element(1,V) =:= dict, tuple_size(V) =:= DictSz -> - {K, [F(I) || I <- dict:to_list(V)]}; - F(Other) -> - Other - end, - lists:zip(Fields, [Fun(I) || I <- Values]). + lists:zip(Fields, [reformat(I, DictSz) || I <- Values]). + +reformat({K,V}, DSz) when is_list(V) -> + {K, [reformat(I, DSz) || I <- V]}; +reformat(V, DSz) when is_tuple(V), element(1,V) =:= dict, tuple_size(V) =:= DSz -> + [reformat(I, DSz) || I <- dict:to_list(V)]; +reformat({K,V}, DSz) when is_tuple(V), element(1,V) =:= dict, tuple_size(V) =:= DSz -> + {K, [reformat(I, DSz) || I <- dict:to_list(V)]}; +reformat(Other, _DSz) -> + Other. %% =================================================================== %% Internal functions -- cgit v1.1 From 0f1df270c17d2c524005cbd2bdfc29a19660f8cf Mon Sep 17 00:00:00 2001 From: Serge Aleynikov Date: Mon, 8 Feb 2016 19:31:46 -0500 Subject: Make state provider {bare, false} --- src/rebar_prv_state.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rebar_prv_state.erl b/src/rebar_prv_state.erl index dc75c7a..4fbcb67 100644 --- a/src/rebar_prv_state.erl +++ b/src/rebar_prv_state.erl @@ -23,7 +23,7 @@ init(State) -> Provider = providers:create( [{name, ?PROVIDER}, {module, ?MODULE}, - {bare, true}, + {bare, false}, {deps, ?DEPS}, {example, "rebar3 state"}, {short_desc, "Print current configuration state"}, -- cgit v1.1