%% plugins
{plugins, [rebar3_hex, rebar3_elixir]}.

{deps, [{gpb, "4.18.0"}]}.

{post_hooks, [
    % In order to properly compile clproto, the following order of events must be achieved:
    %   1. compile (rebar3) the gpb dependency (it is required to build the messages.erl file in second step),
    %   2. run 'make' to build the messages.erl artifact,
    %   3. compile (rebar3) clproto to compile messages.erl into messages.beam that can be used in the parent app.
    % This order cannot be achieved using standard pre_hooks and post_hooks as it is
    % impossible to squeeze the make inbetween the compiles. Hence the below script
    % that does that in two runs of compile (recursively calling './rebar3 compile' when the artifact is not found).
    {compile,
        % consider compilation already done if the messages.beam is in the ebin directory (parent apps
        % need the artifact in the clproto/ebin directory, but the ./rebar compile call causes local
        % clproto compilation, which produces the artifact in _build/${REBAR_PROFILE}/lib/clproto/ebin)
        "sh -c 'if [ ! -f ebin/messages.beam ]; then "
            "REBAR_PROFILE=${REBAR_PROFILE:-${REBAR3_PROFILE:-default}}; "
            "if [ -d _build/${REBAR_PROFILE}/lib/clproto/ebin ]; then "
                "rm -rf ./ebin; "
                "cp -R _build/${REBAR_PROFILE}/lib/clproto/ebin ./; "
            "else "
                "make; "
                "./rebar3 compile; "
                % this will trigger the posthook itself and the artifact should
                % already be there to be copied
            "fi "
        "fi'"
    },
    {clean, "make clean"}
]}.
