summaryrefslogtreecommitdiff
path: root/src/rebar_agent.erl
diff options
context:
space:
mode:
authorFred Hebert <mononcqc@ferd.ca>2018-10-22 19:46:09 -0400
committerFred Hebert <mononcqc@ferd.ca>2018-10-22 20:35:44 -0400
commit9b03dacf2b7829b584d26a999f80c315ae8ce897 (patch)
tree582cac5837e98a614b345bbd1cbf0e99c00038b7 /src/rebar_agent.erl
parent78e0d7923d78d912844e413bc82aa24ff1484d6e (diff)
Allow Breakpoints during task runs
This is mostly useful for tests, where a test suite of any kind can be interrupted halfway through so that the user can probe the running system to see what is happening. This is done as follows: 1. the user must call `r3:break()` in a test suite 2. the user runs the task as `r3:async_do(ct)` 3. the test holds up and the user can do whatever 4. the user calls `r3:resume()` and the test proceeds as normal A safeguard is added so that breakpoints are only triggered in the shell in async mode Sample session: $ rebar3 shell ... 1> rebar_agent:async_do(ct). ok ... Running Common Test suites... %%% rebar_alias_SUITE: . === BREAK === 2> % <do some checks> 2> r3:resume(). ok 3> ..... %%% rebar_as_SUITE: ........... %%% rebar_compile_SUITE: ...... ...
Diffstat (limited to 'src/rebar_agent.erl')
-rw-r--r--src/rebar_agent.erl38
1 files changed, 37 insertions, 1 deletions
diff --git a/src/rebar_agent.erl b/src/rebar_agent.erl
index 445ae54..b4734f1 100644
--- a/src/rebar_agent.erl
+++ b/src/rebar_agent.erl
@@ -1,7 +1,7 @@
%%% @doc Runs a process that holds a rebar3 state and can be used
%%% to statefully maintain loaded project state into a running VM.
-module(rebar_agent).
--export([start_link/1, do/1, do/2]).
+-export([start_link/1, do/1, do/2, async_do/1, async_do/2]).
-export(['$handle_undefined_function'/2]).
-export([init/1,
handle_call/3, handle_cast/2, handle_info/2,
@@ -35,6 +35,18 @@ do(Namespace, Command) when is_atom(Namespace), is_atom(Command) ->
do(Namespace, Args) when is_atom(Namespace), is_list(Args) ->
gen_server:call(?MODULE, {cmd, Namespace, do, Args}, infinity).
+-spec async_do(atom()) -> ok | {error, term()}.
+async_do(Command) when is_atom(Command) ->
+ gen_server:cast(?MODULE, {cmd, Command});
+async_do(Args) when is_list(Args) ->
+ gen_server:cast(?MODULE, {cmd, default, do, Args}).
+
+-spec async_do(atom(), atom()) -> ok.
+async_do(Namespace, Command) when is_atom(Namespace), is_atom(Command) ->
+ gen_server:cast(?MODULE, {cmd, Namespace, Command});
+async_do(Namespace, Args) when is_atom(Namespace), is_list(Args) ->
+ gen_server:cast(?MODULE, {cmd, Namespace, do, Args}).
+
'$handle_undefined_function'(Cmd, [Namespace, Args]) ->
gen_server:call(?MODULE, {cmd, Namespace, Cmd, Args}, infinity);
'$handle_undefined_function'(Cmd, [Args]) ->
@@ -54,20 +66,44 @@ init(State) ->
%% @private
handle_call({cmd, Command}, _From, State=#state{state=RState, cwd=Cwd}) ->
MidState = maybe_show_warning(State),
+ put(cmd_type, sync),
{Res, NewRState} = run(default, Command, "", RState, Cwd),
+ put(cmd_type, undefined),
{reply, Res, MidState#state{state=NewRState}, hibernate};
handle_call({cmd, Namespace, Command}, _From, State = #state{state=RState, cwd=Cwd}) ->
MidState = maybe_show_warning(State),
+ put(cmd_type, sync),
{Res, NewRState} = run(Namespace, Command, "", RState, Cwd),
+ put(cmd_type, undefined),
{reply, Res, MidState#state{state=NewRState}, hibernate};
handle_call({cmd, Namespace, Command, Args}, _From, State = #state{state=RState, cwd=Cwd}) ->
MidState = maybe_show_warning(State),
+ put(cmd_type, sync),
{Res, NewRState} = run(Namespace, Command, Args, RState, Cwd),
+ put(cmd_type, undefined),
{reply, Res, MidState#state{state=NewRState}, hibernate};
handle_call(_Call, _From, State) ->
{noreply, State}.
%% @private
+handle_cast({cmd, Command}, State=#state{state=RState, cwd=Cwd}) ->
+ MidState = maybe_show_warning(State),
+ put(cmd_type, async),
+ {_, NewRState} = run(default, Command, "", RState, Cwd),
+ put(cmd_type, undefined),
+ {noreply, MidState#state{state=NewRState}, hibernate};
+handle_cast({cmd, Namespace, Command}, State = #state{state=RState, cwd=Cwd}) ->
+ MidState = maybe_show_warning(State),
+ put(cmd_type, async),
+ {_, NewRState} = run(Namespace, Command, "", RState, Cwd),
+ put(cmd_type, undefined),
+ {noreply, MidState#state{state=NewRState}, hibernate};
+handle_cast({cmd, Namespace, Command, Args}, State = #state{state=RState, cwd=Cwd}) ->
+ MidState = maybe_show_warning(State),
+ put(cmd_type, async),
+ {_, NewRState} = run(Namespace, Command, Args, RState, Cwd),
+ put(cmd_type, undefined),
+ {noreply, MidState#state{state=NewRState}, hibernate};
handle_cast(_Cast, State) ->
{noreply, State}.