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.

58 line
2.1 KiB

  1. -module(rebar_prv_new).
  2. -behaviour(provider).
  3. -export([init/1,
  4. do/1,
  5. format_error/2]).
  6. -include("rebar.hrl").
  7. -define(PROVIDER, new).
  8. -define(DEPS, []).
  9. %% ===================================================================
  10. %% Public API
  11. %% ===================================================================
  12. -spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
  13. init(State) ->
  14. State1 = rebar_state:add_provider(State, providers:create([{name, ?PROVIDER},
  15. {module, ?MODULE},
  16. {bare, false},
  17. {deps, ?DEPS},
  18. {example, "rebar new <template>"},
  19. {short_desc, "Create new project from templates."},
  20. {desc, info()},
  21. {opts, []}])),
  22. {ok, State1}.
  23. -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
  24. do(State) ->
  25. case rebar_state:command_args(State) of
  26. [TemplateName] ->
  27. Template = list_to_atom(TemplateName),
  28. rebar_templater:new(Template, "", State),
  29. {ok, State};
  30. [TemplateName, DirName] ->
  31. Template = list_to_atom(TemplateName),
  32. rebar_templater:new(Template, DirName, State),
  33. {ok, State};
  34. [] ->
  35. {ok, State}
  36. end.
  37. -spec format_error(any(), rebar_state:t()) -> {iolist(), rebar_state:t()}.
  38. format_error(Reason, State) ->
  39. {io_lib:format("~p", [Reason]), State}.
  40. %% ===================================================================
  41. %% Internal functions
  42. %% ===================================================================
  43. info() ->
  44. io_lib:format(
  45. "Create rebar project based on template and vars.~n"
  46. "~n"
  47. "Valid command line options:~n"
  48. " template= [var=foo,...]~n", []).