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.

83 lines
3.2 KiB

  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. -module(rebar_prv_clean).
  4. -behaviour(provider).
  5. -export([init/1,
  6. do/1,
  7. format_error/1]).
  8. -include("rebar.hrl").
  9. -define(PROVIDER, clean).
  10. -define(DEPS, [app_discovery]).
  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 clean"},
  21. {short_desc, "Remove compiled beam files from apps."},
  22. {desc, "Remove compiled beam files from apps."},
  23. {opts, [{all, $a, "all", undefined, "Clean all apps include deps"}]}])),
  24. {ok, State1}.
  25. -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
  26. do(State) ->
  27. Providers = rebar_state:providers(State),
  28. ProjectApps = rebar_state:project_apps(State),
  29. {all, All} = handle_args(State),
  30. case All of
  31. true ->
  32. DepsDir = rebar_dir:deps_dir(State),
  33. DepApps = rebar_app_discover:find_apps([DepsDir], all);
  34. false ->
  35. DepApps = []
  36. end,
  37. %% Need to allow global config vars used on deps
  38. %% Right now no way to differeniate and just give deps a new state
  39. EmptyState = rebar_state:new(),
  40. clean_apps(EmptyState, Providers, DepApps),
  41. Cwd = rebar_dir:get_cwd(),
  42. rebar_hooks:run_all_hooks(Cwd, pre, ?PROVIDER, Providers, State),
  43. clean_apps(State, Providers, ProjectApps),
  44. rebar_hooks:run_all_hooks(Cwd, post, ?PROVIDER, Providers, State),
  45. {ok, State}.
  46. -spec format_error(any()) -> iolist().
  47. format_error(Reason) ->
  48. io_lib:format("~p", [Reason]).
  49. %% ===================================================================
  50. %% Internal functions
  51. %% ===================================================================
  52. clean_apps(State, Providers, Apps) ->
  53. lists:foreach(fun(AppInfo) ->
  54. AppDir = rebar_app_info:dir(AppInfo),
  55. C = rebar_config:consult(AppDir),
  56. S = rebar_state:new(State, C, AppDir),
  57. ?INFO("Cleaning out ~s...", [rebar_app_info:name(AppInfo)]),
  58. %% Legacy hook support
  59. rebar_hooks:run_all_hooks(AppDir, pre, ?PROVIDER, Providers, S),
  60. rebar_erlc_compiler:clean(State, rebar_app_info:out_dir(AppInfo)),
  61. rebar_hooks:run_all_hooks(AppDir, post, ?PROVIDER, Providers, S)
  62. end, Apps).
  63. handle_args(State) ->
  64. {Args, _} = rebar_state:command_parsed_args(State),
  65. All = proplists:get_value(all, Args, false),
  66. {all, All}.