<commit_before>%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 et

-module(rebar_plugins).

-export([install/1]).

-include("rebar.hrl").

%% ===================================================================
%% Public API
%% ===================================================================

install(State) ->
    %% Set deps_dir to a different dir for plugin so they don't collide
    OldDepsDir = rebar_state:get(State, deps_dir, ?DEFAULT_DEPS_DIR),
    State1 = rebar_state:set(State, deps_dir, ?DEFAULT_PLUGINS_DIR),
    DepsDir = rebar_dir:deps_dir(State1),
    expand_plugins(DepsDir),
    Plugins = rebar_state:get(State1, plugins, []),
    PluginProviders = lists:flatten(rebar_utils:filtermap(fun(Plugin) ->
                                                                  handle_plugin(Plugin, State1)
                                                          end, Plugins)),

    State2 = rebar_state:set(State1, deps_dir, OldDepsDir),
    {ok, PluginProviders, State2}.

-spec handle_plugin(rebar_prv_install_deps:dep(), rebar_state:t()) -> {true, any()} | false.
handle_plugin(Plugin, State) ->
    try
        {ok, _, State1} = rebar_prv_install_deps:handle_deps(default, State, [Plugin]),

        Apps = rebar_state:all_deps(State1),
        ToBuild = lists:dropwhile(fun rebar_app_info:valid/1, Apps),
        [build_plugin(AppInfo) || AppInfo <- ToBuild],
        plugin_providers(Plugin)
    catch
        C:T ->
            ?DEBUG("~p ~p", [C, T]),
            ?WARN("Plugin ~p not available. It will not be used.~n", [Plugin]),
            false
    end.

build_plugin(AppInfo) ->
    AppDir = rebar_app_info:dir(AppInfo),
    C = rebar_config:consult(AppDir),
    S = rebar_state:new(rebar_state:new(), C, AppDir),
    rebar_prv_compile:compile(S, AppInfo),
    true = code:add_patha(filename:join(AppDir, "ebin")).

plugin_providers({Plugin, _, _}) when is_atom(Plugin) ->
    validate_plugin(Plugin);
plugin_providers({Plugin, _}) when is_atom(Plugin) ->
    validate_plugin(Plugin);
plugin_providers(Plugin) when is_atom(Plugin) ->
    validate_plugin(Plugin).

validate_plugin(Plugin) ->
    ok = application:load(Plugin),
    case application:get_env(Plugin, providers) of
        {ok, Providers} ->
            {true, Providers};
        undefined ->
            Exports = sets:from_list(Plugin:module_info(exports)),
            Required = sets:from_list([{init,1},
                                       {do,1},
                                       {format_error,1}]),
            case sets:is_subset(Required,  Exports) of
                false ->
                    ?WARN("Plugin ~p is not a provider. It will not be used.~n", [Plugin]),
                    false;
                true ->
                    {true, Plugin}
            end
    end.

expand_plugins(Dir) -><commit_msg>main module of a plugin just has to export init/1<commit_after>%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 et

-module(rebar_plugins).

-export([install/1]).

-include("rebar.hrl").

%% ===================================================================
%% Public API
%% ===================================================================

install(State) ->
    %% Set deps_dir to a different dir for plugin so they don't collide
    OldDepsDir = rebar_state:get(State, deps_dir, ?DEFAULT_DEPS_DIR),
    State1 = rebar_state:set(State, deps_dir, ?DEFAULT_PLUGINS_DIR),
    DepsDir = rebar_dir:deps_dir(State1),
    expand_plugins(DepsDir),
    Plugins = rebar_state:get(State1, plugins, []),
    PluginProviders = lists:flatten(rebar_utils:filtermap(fun(Plugin) ->
                                                                  handle_plugin(Plugin, State1)
                                                          end, Plugins)),

    State2 = rebar_state:set(State1, deps_dir, OldDepsDir),
    {ok, PluginProviders, State2}.

-spec handle_plugin(rebar_prv_install_deps:dep(), rebar_state:t()) -> {true, any()} | false.
handle_plugin(Plugin, State) ->
    try
        {ok, _, State1} = rebar_prv_install_deps:handle_deps(default, State, [Plugin]),

        Apps = rebar_state:all_deps(State1),
        ToBuild = lists:dropwhile(fun rebar_app_info:valid/1, Apps),
        [build_plugin(AppInfo) || AppInfo <- ToBuild],
        plugin_providers(Plugin)
    catch
        C:T ->
            ?DEBUG("~p ~p", [C, T]),
            ?WARN("Plugin ~p not available. It will not be used.~n", [Plugin]),
            false
    end.

build_plugin(AppInfo) ->
    AppDir = rebar_app_info:dir(AppInfo),
    C = rebar_config:consult(AppDir),
    S = rebar_state:new(rebar_state:new(), C, AppDir),
    rebar_prv_compile:compile(S, AppInfo),
    true = code:add_patha(filename:join(AppDir, "ebin")).

plugin_providers({Plugin, _, _}) when is_atom(Plugin) ->
    validate_plugin(Plugin);
plugin_providers({Plugin, _}) when is_atom(Plugin) ->
    validate_plugin(Plugin);
plugin_providers(Plugin) when is_atom(Plugin) ->
    validate_plugin(Plugin).

validate_plugin(Plugin) ->
    ok = application:load(Plugin),
    case application:get_env(Plugin, providers) of
        {ok, Providers} ->
            {true, Providers};
        undefined ->
            Exports = Plugin:module_info(exports),
            case lists:member({init,1}, Exports) of
                false ->
                    ?WARN("Plugin ~p does not export init/1. It will not be used.~n", [Plugin]),
                    false;
                true ->
                    {true, Plugin}
            end
    end.

expand_plugins(Dir) ->
{'fraction_non_alphanumeric': 0.17430368373764601, 'fraction_numerical': 0.006109613656783468, 'mean_word_length': 2.609597924773022, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6053461', 'n_tokens_mistral': 1740, 'n_tokens_neox': 1698, 'n_words': 331}