You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. -module(rebar_prv_do).
  4. -behaviour(provider).
  5. -export([init/1,
  6. do/1,
  7. format_error/1]).
  8. -include("rebar.hrl").
  9. -define(PROVIDER, do).
  10. -define(DEPS, []).
  11. %% ===================================================================
  12. %% Public API
  13. %% ===================================================================
  14. -spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
  15. init(State) ->
  16. State1 = rebar_state:add_provider(State, providers:create([{name, ?PROVIDER},
  17. {module, ?MODULE},
  18. {bare, false},
  19. {deps, ?DEPS},
  20. {example, "rebar3 do <task1>, <task2>, ..."},
  21. {short_desc, "Higher order provider for running multiple tasks in a sequence."},
  22. {desc, ""},
  23. {opts, []}])),
  24. {ok, State1}.
  25. -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
  26. do(State) ->
  27. Tasks = args_to_tasks(rebar_state:command_args(State)),
  28. do_tasks(Tasks, State).
  29. do_tasks([], State) ->
  30. {ok, State};
  31. do_tasks([TaskArgs | Tail], State) ->
  32. [TaskStr | Args] = string:tokens(TaskArgs, " "),
  33. Task = list_to_atom(TaskStr),
  34. State1 = rebar_state:set(State, task, Task),
  35. State2 = rebar_state:command_args(State1, Args),
  36. case rebar_core:process_command(State2, Task) of
  37. {ok, State3} ->
  38. do_tasks(Tail, State3);
  39. Error ->
  40. Error
  41. end.
  42. -spec format_error(any()) -> iolist().
  43. format_error(Reason) ->
  44. io_lib:format("~p", [Reason]).
  45. args_to_tasks(Args) ->
  46. [string:strip(T) || T <- string:tokens(string:join(Args, " "), ",")].