浏览代码

Merge pull request #310 from ferd/do-namespaces

Support namespace resolution in 'do' provider
pull/311/head
Fred Hebert 10 年前
父节点
当前提交
7df2ef4c84
共有 2 个文件被更改,包括 75 次插入6 次删除
  1. +38
    -5
      src/rebar_prv_do.erl
  2. +37
    -1
      test/rebar_namespace_SUITE.erl

+ 38
- 5
src/rebar_prv_do.erl 查看文件

@ -42,13 +42,46 @@ do_tasks([{TaskStr, Args}|Tail], State) ->
Task = list_to_atom(TaskStr),
State1 = rebar_state:set(State, task, Task),
State2 = rebar_state:command_args(State1, Args),
case rebar_core:process_command(State2, Task) of
{ok, _} ->
do_tasks(Tail, State);
Error ->
Error
Namespace = rebar_state:namespace(State2),
case Namespace of
default ->
%% The first task we hit might be a namespace!
case maybe_namespace(State2, Task, Args) of
{ok, _} ->
do_tasks(Tail, State);
{error, Reason} ->
{error, Reason}
end;
_ ->
%% We're already in a non-default namespace, check the
%% task directly.
case rebar_core:process_command(State2, Task) of
{ok, _} ->
do_tasks(Tail, State);
{error, Reason} ->
{error, Reason}
end
end.
-spec format_error(any()) -> iolist().
format_error(Reason) ->
io_lib:format("~p", [Reason]).
maybe_namespace(State, Task, Args) ->
case rebar_core:process_namespace(State, Task) of
{ok, State2, Task} ->
%% The task exists after all.
rebar_core:process_command(State2, Task);
{ok, State2, do} when Args =/= [] ->
%% We are in 'do' so we can't apply it directly.
[NewTaskStr | NewArgs] = Args,
NewTask = list_to_atom(NewTaskStr),
State3 = rebar_state:command_args(State2, NewArgs),
rebar_core:process_command(State3, NewTask);
{ok, _, _} ->
%% No arguments to consider as a command. Woops.
{error, io_lib:format("Command ~p not found", [Task])};
{error, Reason} ->
{error, Reason}
end.

+ 37
- 1
test/rebar_namespace_SUITE.erl 查看文件

@ -6,7 +6,8 @@
all() -> [implicit_compile, default_compile, do_compile,
as_default_compile, as_do_compile,
notfound, do_notfound, default_notfound, ns_notfound, ns_found,
as_ns_invalid].
as_ns_invalid,
do_ns_chain, do_ns_chain2, do_ns_noarg, do_ns_badcmd].
init_per_testcase(Case, Config0) ->
Config = rebar_test_utils:init_rebar_state(Config0),
@ -93,6 +94,41 @@ as_ns_invalid(Config) ->
{error, "Namespace 'as' is forbidden"}
).
do_ns_chain(Config) ->
%% `do` is also able to resolve namespaces on
%% commands not found
Command = ["do", "deps,", "ns", "fake_provider,", "deps"],
rebar_test_utils:run_and_check(
add_fake_ns_provider(Config), [], Command,
{ok, []}
).
do_ns_chain2(Config) ->
%% `do` is also able to resolve namespaces on
%% commands not found
Command = ["do", "ns", "fake_provider,", "deps,", "ns", "fake_provider"],
rebar_test_utils:run_and_check(
add_fake_ns_provider(Config), [], Command,
{ok, []}
).
do_ns_noarg(Config) ->
%% `do` is also able to resolve namespaces on
%% commands not found
Command = ["do", "ns"],
rebar_test_utils:run_and_check(
add_fake_ns_provider(Config), [], Command,
{error, io_lib:format("Command ~p not found", [ns])}
).
do_ns_badcmd(Config) ->
%% `do` is also able to resolve namespaces on
%% commands not found
Command = ["do", "ns", "badcmd"],
rebar_test_utils:run_and_check(
add_fake_ns_provider(Config), [], Command,
{error, io_lib:format("Command ~p not found in namespace ~p", [badcmd, ns])}
).
%%% Helpers %%%
add_fake_ns_provider(Config) ->

正在加载...
取消
保存