<commit_before>-module(erlson_rebar_plugin).

% rebar commands
-export([
    pre_compile/2,
    pre_eunit/2,
    pre_ct/2,
    pre_doc/2,
    pre_xref/2
]).


pre_compile(Config, X) ->
    common(Config, X).

pre_eunit(Config, X) ->
    common(Config, X).

pre_ct(Config, X) ->
    common(Config, X).

pre_doc(Config, X) ->
    common(Config, X).

pre_xref(Config, X) ->
    common(Config, X).


common(_Config, _X) ->
    code:purge(erl_parse),
    code:delete(erl_parse),
    code:load_file(erl_parse),
    rebar_log:log(debug,
        "erlson_rebar_plugin: repointed erl_parse to: ~s~n",
        [code:which(erl_parse)]),
    ok.

<commit_msg>More reliable and efficient Erlson rebar plugin<commit_after>-module(erlson_rebar_plugin).

% rebar commands
-export([
    pre_compile/2,
    pre_eunit/2,
    pre_ct/2,
    pre_doc/2,
    pre_xref/2
]).


pre_compile(Config, X) ->
    common(Config, X).

pre_eunit(Config, X) ->
    common(Config, X).

pre_ct(Config, X) ->
    common(Config, X).

pre_doc(Config, X) ->
    common(Config, X).

pre_xref(Config, X) ->
    common(Config, X).


common(_Config, _X) ->
    ErlParsePath = code:which(erl_parse),
    case {string:str(ErlParsePath, "erlson"), string:str(ErlParsePath, "stdlib")} of
        {N, 0} when N =/= 0 ->
            % Erlson version of the Erlang parser is already loaded
            ok;
        {0, N} when N =/= 0 ->
            % for loading the custom "erl_parse" module, we rely on the fact
            % that it is located next to this rebar plugin
            code:purge(erl_parse),
            code:delete(erl_parse),
            code:load_file(erl_parse),
            rebar_log:log(debug,
                "erlson_rebar_plugin: repointed erl_parse to: ~s~n",
                [code:which(erl_parse)]),
            true = (string:str(code:which(erl_parse), "erlson") =/= 0),
            ok
    end.


{'fraction_non_alphanumeric': 0.14163548904329235, 'fraction_numerical': 0.008017103153393906, 'mean_word_length': 2.5121951219512195, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1525159', 'n_tokens_mistral': 724, 'n_tokens_neox': 692, 'n_words': 143}