<commit_before>-module(builderl_process).

-export([run/4]).

run(Command, Dir, Env, console) ->
    run(Command, Dir, Env, fun output_to_console/1);
run(Command, Dir, Env, {file, Path}) ->
    Output = create_output_to_newfile(Path),
    run(Command, Dir, Env, Output);
run(Command, Dir, Env, Func) ->
    [Cmd|Args] = string:tokens(Command, " "),
    Exec = os:find_executable(Cmd),
    io:format("Command ~p~n", [Exec]),
    Port = erlang:open_port({spawn_executable, Exec},
        [stream, stderr_to_stdout, binary, exit_status,
         {args, Args}, {cd, Dir}, {env, Env}]),
    Status = loop(Port, Func),
    io:format("Status ~p~n",[Status]),
    Status.



loop(Port, Fun) ->
    receive
        {Port, {exit_status, Status}} -> Status;
        {Port, Msg} -> Fun(Msg),
                       loop(Port, Fun)
    end.


output_to_console({data, {eol, Data}}) ->
    io:format("eol ~p~n", [Data]);
output_to_console({data, {noeol, Data}}) ->
    io:format("noeol ~p", [Data]);
output_to_console({data, Data}) ->
    io:format("boop data ~p~n", [Data]).

create_output_to_newfile(Path) ->
    {ok, IoD} = file:open(Path, [write]),

    Output = fun({data, {eol, Data}}) ->
                file:write(IoD, ["eol", Data, "\n"]);
             ({data, {noeol, Data}}) ->
                file:write(IoD, ["noeol", Data]);
             ({data, Data}) ->
                file:write(IoD, ["data ", Data])
    end,
    Output.
<commit_msg>Use step PATH for finding executable<commit_after>-module(builderl_process).

-export([run/4]).

run(Command, Dir, Env, console) ->
    run(Command, Dir, Env, fun output_to_console/1);
run(Command, Dir, Env, {file, Path}) ->
    Output = create_output_to_newfile(Path),
    run(Command, Dir, Env, Output);
run(Command, Dir, Env, Func) ->
    [Cmd|Args] = string:tokens(Command, " "),
    % Use the Env PATH to find the executable!
    PATH = proplists:get_value("PATH", Env),
    io:format("Finding command with PATH of ~p~n", [PATH]),
    Exec = os:find_executable(Cmd, PATH),
    io:format("Command ~p~n", [Exec]),
    Port = erlang:open_port({spawn_executable, Exec},
        [stream, stderr_to_stdout, binary, exit_status,
         {args, Args}, {cd, Dir}, {env, Env}]),
    Status = loop(Port, Func),
    io:format("Status ~p~n",[Status]),
    Status.



loop(Port, Fun) ->
    receive
        {Port, {exit_status, Status}} -> Status;
        {Port, Msg} -> Fun(Msg),
                       loop(Port, Fun)
    end.


output_to_console({data, {eol, Data}}) ->
    io:format("eol ~p~n", [Data]);
output_to_console({data, {noeol, Data}}) ->
    io:format("noeol ~p", [Data]);
output_to_console({data, Data}) ->
    io:format("boop data ~p~n", [Data]).

create_output_to_newfile(Path) ->
    {ok, IoD} = file:open(Path, [write]),

    Output = fun({data, {eol, Data}}) ->
                file:write(IoD, ["eol", Data, "\n"]);
             ({data, {noeol, Data}}) ->
                file:write(IoD, ["noeol", Data]);
             ({data, Data}) ->
                file:write(IoD, ["data ", Data])
    end,
    Output.

{'fraction_non_alphanumeric': 0.1956166175989532, 'fraction_numerical': 0.0013084723585214263, 'mean_word_length': 2.915492957746479, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '6310904', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1082, 'n_words': 242}