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.

67 rivejä
2.3 KiB

10 vuotta sitten
  1. -module(rebar_prv_plugins).
  2. -behaviour(provider).
  3. -export([init/1,
  4. do/1,
  5. format_error/1]).
  6. -include("rebar.hrl").
  7. -include_lib("providers/include/providers.hrl").
  8. -define(PROVIDER, list).
  9. -define(NAMESPACE, plugins).
  10. -define(DEPS, []).
  11. -spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
  12. init(State) ->
  13. State1 = rebar_state:add_provider(
  14. State,
  15. providers:create([
  16. {name, ?PROVIDER},
  17. {module, ?MODULE},
  18. {namespace, ?NAMESPACE},
  19. {bare, true},
  20. {deps, ?DEPS},
  21. {example, "rebar3 plugins list"},
  22. {short_desc, "List local and global plugins for this project"},
  23. {desc, "List local and global plugins for this project"},
  24. {opts, []}])),
  25. {ok, State1}.
  26. -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
  27. do(State) ->
  28. GlobalConfigFile = rebar_dir:global_config(),
  29. GlobalConfig = rebar_state:new(rebar_config:consult_file(GlobalConfigFile)),
  30. GlobalPlugins = rebar_state:get(GlobalConfig, plugins, []),
  31. GlobalPluginsDir = filename:join(rebar_dir:global_cache_dir(State), "plugins"),
  32. display_plugins("Global plugins", GlobalPluginsDir, GlobalPlugins),
  33. Plugins = rebar_state:get(State, plugins, []),
  34. PluginsDir =rebar_dir:plugins_dir(State),
  35. display_plugins("Local plugins", PluginsDir, Plugins),
  36. {ok, State}.
  37. -spec format_error(any()) -> iolist().
  38. format_error(Reason) ->
  39. io_lib:format("~p", [Reason]).
  40. display_plugins(_Header, _Dir, []) ->
  41. ok;
  42. display_plugins(Header, Dir, Plugins) ->
  43. ?CONSOLE("--- ~s ---", [Header]),
  44. display_plugins(Dir, Plugins),
  45. ?CONSOLE("", []).
  46. display_plugins(Dir, Plugins) ->
  47. lists:foreach(fun(Plugin) ->
  48. Name = if is_atom(Plugin) -> Plugin;
  49. is_tuple(Plugin) -> element(1, Plugin)
  50. end,
  51. case rebar_app_info:discover(filename:join(Dir, Name)) of
  52. {ok, _App} ->
  53. ?CONSOLE("~s", [Name]);
  54. not_found ->
  55. ?DEBUG("Unable to find plugin ~s", [Name])
  56. end
  57. end, Plugins).