summaryrefslogtreecommitdiff
path: root/src/rebar_prv_plugins.erl
blob: 328958e127c9c291f83e6d3c60d8d850cd8f93e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
-module(rebar_prv_plugins).

-behaviour(provider).

-export([init/1,
         do/1,
         format_error/1]).

-include("rebar.hrl").
-include_lib("providers/include/providers.hrl").

-define(PROVIDER, list).
-define(NAMESPACE, plugins).
-define(DEPS, []).

-spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
init(State) ->
    State1 = rebar_state:add_provider(
            State,
            providers:create([
                    {name, ?PROVIDER},
                    {module, ?MODULE},
                    {namespace, ?NAMESPACE},
                    {bare, true},
                    {deps, ?DEPS},
                    {example, "rebar3 plugins list"},
                    {short_desc, "List local and global plugins for this project"},
                    {desc, "List local and global plugins for this project"},
                    {opts, []}])),
    {ok, State1}.

-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
do(State) ->
    GlobalConfigFile = rebar_dir:global_config(),
    GlobalConfig = rebar_state:new(rebar_config:consult_file(GlobalConfigFile)),
    GlobalPlugins = rebar_state:get(GlobalConfig, plugins, []),
    GlobalPluginsDir = filename:join(rebar_dir:global_cache_dir(State), "plugins"),
    display_plugins("Global plugins", GlobalPluginsDir, GlobalPlugins),

    Plugins = rebar_state:get(State, plugins, []),
    PluginsDir =rebar_dir:plugins_dir(State),
    display_plugins("Local plugins", PluginsDir, Plugins),
    {ok, State}.

-spec format_error(any()) -> iolist().
format_error(Reason) ->
    io_lib:format("~p", [Reason]).

display_plugins(_Header, _Dir, []) ->
    ok;
display_plugins(Header, Dir, Plugins) ->
    ?CONSOLE("--- ~s ---", [Header]),
    display_plugins(Dir, Plugins),
    ?CONSOLE("", []).

display_plugins(Dir, Plugins) ->
    lists:foreach(fun(Plugin) ->
                          Name = if is_atom(Plugin) -> Plugin;
                                    is_tuple(Plugin) -> element(1, Plugin)
                                 end,
                          case rebar_app_info:discover(filename:join(Dir, Name)) of
                              {ok, _App} ->
                                  ?CONSOLE("~s", [Name]);
                              not_found ->
                                  ?DEBUG("Unable to find plugin ~s", [Name])
                          end
                  end, Plugins).