summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFred Hebert <mononcqc@ferd.ca>2017-04-25 11:13:46 -0400
committerGitHub <noreply@github.com>2017-04-25 11:13:46 -0400
commit29623b59efb75fce62c18a3e22553aadfcccec9c (patch)
tree9f54fdb69bf4c37e5a6888bab5a4d8f17e9267cd /src
parentd284b72c615bcccaea7d2c48b801a67962ccfe17 (diff)
parent03425c788c0e1ac38a3172f6c13a42cd1ffa4b4a (diff)
Merge pull request #1534 from ferd/abuse-undef-functions
Abuse error_handler to get free metacalls in r3
Diffstat (limited to 'src')
-rw-r--r--src/r3.erl5
-rw-r--r--src/rebar_agent.erl31
2 files changed, 29 insertions, 7 deletions
diff --git a/src/r3.erl b/src/r3.erl
index d0d6c47..bbf9eea 100644
--- a/src/r3.erl
+++ b/src/r3.erl
@@ -2,6 +2,7 @@
%%% calls from a shell.
-module(r3).
-export([do/1, do/2]).
+-export(['$handle_undefined_function'/2]).
%% @doc alias for `rebar_agent:do/1'
-spec do(atom()) -> ok | {error, term()}.
@@ -10,3 +11,7 @@ do(Command) -> rebar_agent:do(Command).
%% @doc alias for `rebar_agent:do/2'
-spec do(atom(), atom()) -> ok | {error, term()}.
do(Namespace, Command) -> rebar_agent:do(Namespace, Command).
+
+%% @private defer to rebar_agent
+'$handle_undefined_function'(Cmd, Args) ->
+ rebar_agent:'$handle_undefined_function'(Cmd, Args).
diff --git a/src/rebar_agent.erl b/src/rebar_agent.erl
index ed9e45d..627ed96 100644
--- a/src/rebar_agent.erl
+++ b/src/rebar_agent.erl
@@ -2,6 +2,7 @@
%%% to statefully maintain loaded project state into a running VM.
-module(rebar_agent).
-export([start_link/1, do/1, do/2]).
+-export(['$handle_undefined_function'/2]).
-export([init/1,
handle_call/3, handle_cast/2, handle_info/2,
code_change/3, terminate/2]).
@@ -22,13 +23,24 @@ start_link(State) ->
%% @doc runs a given command in the agent's context.
-spec do(atom()) -> ok | {error, term()}.
do(Command) when is_atom(Command) ->
- gen_server:call(?MODULE, {cmd, Command}, infinity).
+ gen_server:call(?MODULE, {cmd, Command}, infinity);
+do(Args) when is_list(Args) ->
+ gen_server:call(?MODULE, {cmd, default, do, Args}, infinity).
%% @doc runs a given command in the agent's context, under a given
%% namespace.
-spec do(atom(), atom()) -> ok | {error, term()}.
do(Namespace, Command) when is_atom(Namespace), is_atom(Command) ->
- gen_server:call(?MODULE, {cmd, Namespace, Command}, infinity).
+ gen_server:call(?MODULE, {cmd, Namespace, Command}, infinity);
+do(Namespace, Args) when is_atom(Namespace), is_list(Args) ->
+ gen_server:call(?MODULE, {cmd, Namespace, do, Args}, infinity).
+
+'$handle_undefined_function'(Cmd, [Namespace, Args]) ->
+ gen_server:call(?MODULE, {cmd, Namespace, Cmd, Args}, infinity);
+'$handle_undefined_function'(Cmd, [Args]) ->
+ gen_server:call(?MODULE, {cmd, default, Cmd, Args}, infinity);
+'$handle_undefined_function'(Cmd, []) ->
+ gen_server:call(?MODULE, {cmd, default, Cmd}, infinity).
%%%%%%%%%%%%%%%%%
%%% CALLBACKS %%%
@@ -42,11 +54,15 @@ init(State) ->
%% @private
handle_call({cmd, Command}, _From, State=#state{state=RState, cwd=Cwd}) ->
MidState = maybe_show_warning(State),
- {Res, NewRState} = run(default, Command, RState, Cwd),
+ {Res, NewRState} = run(default, Command, "", RState, Cwd),
{reply, Res, MidState#state{state=NewRState}, hibernate};
handle_call({cmd, Namespace, Command}, _From, State = #state{state=RState, cwd=Cwd}) ->
MidState = maybe_show_warning(State),
- {Res, NewRState} = run(Namespace, Command, RState, Cwd),
+ {Res, NewRState} = run(Namespace, Command, "", RState, Cwd),
+ {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),
+ {Res, NewRState} = run(Namespace, Command, Args, RState, Cwd),
{reply, Res, MidState#state{state=NewRState}, hibernate};
handle_call(_Call, _From, State) ->
{noreply, State}.
@@ -72,13 +88,14 @@ terminate(_Reason, _State) ->
%%%%%%%%%%%%%%%
%% @private runs the actual command and maintains the state changes
--spec run(atom(), atom(), rebar_state:t(), file:filename()) ->
+-spec run(atom(), atom(), string(), rebar_state:t(), file:filename()) ->
{ok, rebar_state:t()} | {{error, term()}, rebar_state:t()}.
-run(Namespace, Command, RState, Cwd) ->
+run(Namespace, Command, StrArgs, RState, Cwd) ->
try
case rebar_dir:get_cwd() of
Cwd ->
- Args = [atom_to_list(Namespace), atom_to_list(Command)],
+ PArgs = getopt:tokenize(StrArgs),
+ Args = [atom_to_list(Namespace), atom_to_list(Command)] ++ PArgs,
CmdState0 = refresh_state(RState, Cwd),
CmdState1 = rebar_state:set(CmdState0, task, atom_to_list(Command)),
CmdState = rebar_state:set(CmdState1, caller, api),