Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

99 wiersze
3.7 KiB

10 lat temu
10 lat temu
10 lat temu
10 lat temu
10 lat temu
10 lat temu
  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. do_tasks/2,
  8. format_error/1]).
  9. -include("rebar.hrl").
  10. -define(PROVIDER, do).
  11. -define(DEPS, []).
  12. %% ===================================================================
  13. %% Public API
  14. %% ===================================================================
  15. -spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
  16. init(State) ->
  17. State1 = rebar_state:add_provider(State, providers:create([{name, ?PROVIDER},
  18. {module, ?MODULE},
  19. {bare, true},
  20. {deps, ?DEPS},
  21. {example, "rebar3 do <task1>, <task2>, ..."},
  22. {short_desc, "Higher order provider for running multiple tasks in a sequence."},
  23. {desc, "Higher order provider for running multiple tasks in a sequence."},
  24. {opts, []}])),
  25. {ok, State1}.
  26. -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
  27. do(State) ->
  28. case rebar_utils:args_to_tasks(rebar_state:command_args(State)) of
  29. [] ->
  30. AllProviders = rebar_state:providers(State),
  31. Namespace = rebar_state:namespace(State),
  32. Providers = providers:get_providers_by_namespace(Namespace, AllProviders),
  33. providers:help(Providers),
  34. {ok, State};
  35. Tasks ->
  36. do_tasks(Tasks, State)
  37. end.
  38. do_tasks([], State) ->
  39. {ok, State};
  40. do_tasks([{TaskStr, Args}|Tail], State) ->
  41. Task = list_to_atom(TaskStr),
  42. State1 = rebar_state:set(State, task, Task),
  43. State2 = rebar_state:command_args(State1, Args),
  44. Namespace = rebar_state:namespace(State2),
  45. case Namespace of
  46. default ->
  47. %% The first task we hit might be a namespace!
  48. case maybe_namespace(State2, Task, Args) of
  49. {ok, FinalState} when Tail =:= [] ->
  50. {ok, FinalState};
  51. {ok, _} ->
  52. do_tasks(Tail, State);
  53. {error, Reason} ->
  54. {error, Reason}
  55. end;
  56. _ ->
  57. %% We're already in a non-default namespace, check the
  58. %% task directly.
  59. case rebar_core:process_command(State2, Task) of
  60. {ok, FinalState} when Tail =:= [] ->
  61. {ok, FinalState};
  62. {ok, _} ->
  63. do_tasks(Tail, State);
  64. {error, Reason} ->
  65. {error, Reason}
  66. end
  67. end.
  68. -spec format_error(any()) -> iolist().
  69. format_error(Reason) ->
  70. io_lib:format("~p", [Reason]).
  71. maybe_namespace(State, Task, Args) ->
  72. case rebar_core:process_namespace(State, Task) of
  73. {ok, State2, Task} ->
  74. %% The task exists after all.
  75. rebar_core:process_command(State2, Task);
  76. {ok, State2, do} when Args =/= [] ->
  77. %% We are in 'do' so we can't apply it directly.
  78. [NewTaskStr | NewArgs] = Args,
  79. NewTask = list_to_atom(NewTaskStr),
  80. State3 = rebar_state:command_args(State2, NewArgs),
  81. rebar_core:process_command(State3, NewTask);
  82. {ok, _, _} ->
  83. %% No arguments to consider as a command. Woops.
  84. {error, io_lib:format("Command ~p not found", [Task])};
  85. {error, Reason} ->
  86. {error, Reason}
  87. end.