diff options
-rw-r--r-- | src/rebar_prv_alias.erl | 15 | ||||
-rw-r--r-- | src/rebar_prv_do.erl | 24 | ||||
-rw-r--r-- | test/rebar_alias_SUITE.erl | 31 |
3 files changed, 64 insertions, 6 deletions
diff --git a/src/rebar_prv_alias.erl b/src/rebar_prv_alias.erl index 736417b..ce56f29 100644 --- a/src/rebar_prv_alias.erl +++ b/src/rebar_prv_alias.erl @@ -73,8 +73,12 @@ desc(Cmds) -> "Equivalent to running: rebar3 do " ++ rebar_string:join(lists:map(fun to_desc/1, Cmds), ","). -to_desc({Cmd, Args}) -> +to_desc({Cmd, Args}) when is_list(Args) -> atom_to_list(Cmd) ++ " " ++ Args; +to_desc({Namespace, Cmd}) -> + atom_to_list(Namespace) ++ " " ++ atom_to_list(Cmd); +to_desc({Namespace, Cmd, Args}) -> + atom_to_list(Namespace) ++ " " ++ atom_to_list(Cmd) ++ " " ++ Args; to_desc(Cmd) -> atom_to_list(Cmd). @@ -98,6 +102,12 @@ make_args(Cmds) -> lists:map(fun make_tuple/1, lists:map(fun make_arg/1, Cmds))). +make_arg({Namespace, Command, Args}) when is_atom(Namespace), is_atom(Command) -> + {make_atom(Namespace), + make_atom(Command), + make_list([make_string(A) || A <- split_args(Args)])}; +make_arg({Namespace, Command}) when is_atom(Namespace), is_atom(Command) -> + {make_atom(Namespace), make_atom(Command)}; make_arg({Cmd, Args}) -> {make_string(Cmd), make_list([make_string(A) || A <- split_args(Args)])}; make_arg(Cmd) -> @@ -117,6 +127,9 @@ make_string(Atom) when is_atom(Atom) -> make_string(String) when is_list(String) -> {string, 1, String}. +make_atom(Atom) when is_atom(Atom) -> + {atom, 1, Atom}. + %% In case someone used the long option format, the option needs to get %% separated from its value. split_args(Args) -> diff --git a/src/rebar_prv_do.erl b/src/rebar_prv_do.erl index f850135..5f7aa12 100644 --- a/src/rebar_prv_do.erl +++ b/src/rebar_prv_do.erl @@ -44,13 +44,31 @@ do(State) -> do_tasks(Tasks, State) end. +-spec do_tasks(list(Task), State) -> Res when + Task :: {string(), string()} | + {string(), atom()} | + {atom(), atom(), string()}, + State :: rebar_state:t(), + Res :: {ok, rebar_state:t()} | + {error, term()}. do_tasks([], State) -> {ok, State}; -do_tasks([{TaskStr, Args}|Tail], State) -> +do_tasks([{TaskStr, Args} | Tail], State) when is_list(Args) -> Task = list_to_atom(TaskStr), State1 = rebar_state:set(State, task, Task), State2 = rebar_state:command_args(State1, Args), Namespace = rebar_state:namespace(State2), + do_task(TaskStr, Args, Tail, State, Namespace); +do_tasks([{Namespace, Task} | Tail], State) -> + do_task(atom_to_list(Task), [], Tail, State, Namespace); +do_tasks([{Namespace, Task, Args} | Tail], State) + when is_atom(Namespace), is_atom(Task) -> + do_task(atom_to_list(Task), Args, Tail, State, Namespace). + +do_task(TaskStr, Args, Tail, State, Namespace) -> + Task = list_to_atom(TaskStr), + State1 = rebar_state:set(State, task, Task), + State2 = rebar_state:command_args(State1, Args), case Namespace of default -> %% The first task we hit might be a namespace! @@ -65,7 +83,8 @@ do_tasks([{TaskStr, Args}|Tail], State) -> _ -> %% We're already in a non-default namespace, check the %% task directly. - case rebar_core:process_command(State2, Task) of + State3 = rebar_state:namespace(State2, Namespace), + case rebar_core:process_command(State3, Task) of {ok, FinalState} when Tail =:= [] -> {ok, FinalState}; {ok, _} -> @@ -75,7 +94,6 @@ do_tasks([{TaskStr, Args}|Tail], State) -> end end. - -spec format_error(any()) -> iolist(). format_error(Reason) -> io_lib:format("~p", [Reason]). diff --git a/test/rebar_alias_SUITE.erl b/test/rebar_alias_SUITE.erl index 2544aac..d6e27d9 100644 --- a/test/rebar_alias_SUITE.erl +++ b/test/rebar_alias_SUITE.erl @@ -13,8 +13,8 @@ init_per_testcase(_, Config) -> end_per_testcase(_, _Config) -> ok. -all() -> [command, args, many, override_default, no_circular, release]. - %% namespaces: unsupported, untested. +all() -> [command, args, many, override_default, no_circular, release, + check_namespaces, create_lib]. command() -> [{doc, "Runs multiple regular commands as one alias"}]. @@ -136,3 +136,30 @@ release(Config) -> ["the_rel2"], {ok, [{release, the_release, Vsn, false}]}), ok. + +check_namespaces() -> + [{doc, "Test calling commands with namespaces from rebar3"}]. +check_namespaces(Config) -> + AppDir = ?config(apps, Config), + Name = rebar_test_utils:create_random_name("alias_args_"), + Vsn = rebar_test_utils:create_random_vsn(), + rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), + RebarConfig = [{alias, [{test, [{eunit,"-c"}, {plugins, list}]}]}], + rebar_test_utils:run_and_check(Config, RebarConfig, + ["test"], {ok, [{app, Name}]}), + ok. + +create_lib() -> + [{doc, "Test calling commands with namespaces from rebar3"}]. +create_lib(Config) -> + AppDir = ?config(apps, Config), + Name = rebar_test_utils:create_random_name("create_lib_"), + Vsn = rebar_test_utils:create_random_vsn(), + rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), + RebarConfig = [{alias, [{test, [compile, {do, "new lib shouldexist"}]}]}], + rebar_test_utils:run_and_check(Config, RebarConfig, + ["test"], {ok, [{app, Name}]}), + AppFile = filename:join(?config(apps, Config), + "../../../../shouldexist/src/shouldexist.app.src"), + ?assert(filelib:is_file(AppFile)), + ok. |