Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

86 righe
3.3 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, true},
  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. {profile, $p, "profile", string, "Clean under profile. Equivalent to `rebar3 as <profile> clean`"}]}])),
  25. {ok, State1}.
  26. -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
  27. do(State) ->
  28. Providers = rebar_state:providers(State),
  29. {All, Profiles} = handle_args(State),
  30. State1 = rebar_state:apply_profiles(State, [list_to_atom(X) || X <- Profiles]),
  31. Cwd = rebar_dir:get_cwd(),
  32. rebar_hooks:run_all_hooks(Cwd, pre, ?PROVIDER, Providers, State1),
  33. case All of
  34. true ->
  35. DepsDir = rebar_dir:deps_dir(State1),
  36. DepsDirs = filelib:wildcard(filename:join(DepsDir, "*")),
  37. AllApps = rebar_app_discover:find_apps(DepsDirs, all),
  38. clean_apps(State1, Providers, AllApps);
  39. false ->
  40. ProjectApps = rebar_state:project_apps(State1),
  41. clean_apps(State1, Providers, ProjectApps)
  42. end,
  43. clean_extras(State1),
  44. rebar_hooks:run_all_hooks(Cwd, post, ?PROVIDER, Providers, State1),
  45. {ok, State1}.
  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. [begin
  54. ?INFO("Cleaning out ~s...", [rebar_app_info:name(AppInfo)]),
  55. AppDir = rebar_app_info:dir(AppInfo),
  56. AppInfo1 = rebar_hooks:run_all_hooks(AppDir, pre, ?PROVIDER, Providers, AppInfo, State),
  57. rebar_erlc_compiler:clean(AppInfo1),
  58. rebar_hooks:run_all_hooks(AppDir, post, ?PROVIDER, Providers, AppInfo1, State)
  59. end || AppInfo <- Apps].
  60. clean_extras(State) ->
  61. BaseDir = rebar_dir:base_dir(State),
  62. rebar_file_utils:rm_rf(filename:join([BaseDir, "extras"])).
  63. handle_args(State) ->
  64. {Args, _} = rebar_state:command_parsed_args(State),
  65. All = proplists:get_value(all, Args, false),
  66. Profiles = proplists:get_all_values(profile, Args),
  67. {All, Profiles}.