src/gleedoc.erl

-module(gleedoc).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gleedoc.gleam").
-export([run/1, main/0]).
-export_type([gleedoc_config/0]).

-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.

-type gleedoc_config() :: {gleedoc_config, binary(), binary()}.

-file("src/gleedoc.gleam", 72).
-spec go_find_gleam_files(binary(), list(binary())) -> {ok, list(binary())} |
    {error, snag:snag()}.
go_find_gleam_files(Dir, Acc) ->
    gleam@result:'try'(
        begin
            _pipe = Dir,
            _pipe@1 = simplifile_erl:read_directory(_pipe),
            gleam@result:map_error(
                _pipe@1,
                fun(Err) ->
                    snag:new(
                        <<<<<<"Failed to read directory: "/utf8, Dir/binary>>/binary,
                                " - "/utf8>>/binary,
                            (gleam@string:inspect(Err))/binary>>
                    )
                end
            )
        end,
        fun(Entries) -> _pipe@2 = Entries,
            gleam@list:try_fold(
                _pipe@2,
                Acc,
                fun(Acc@1, Entry) ->
                    Path = <<<<Dir/binary, "/"/utf8>>/binary, Entry/binary>>,
                    gleam@result:'try'(
                        begin
                            _pipe@3 = Path,
                            _pipe@4 = simplifile_erl:is_directory(_pipe@3),
                            gleam@result:map_error(
                                _pipe@4,
                                fun(Err@1) ->
                                    snag:new(
                                        <<<<<<"Failed to stat: "/utf8,
                                                    Path/binary>>/binary,
                                                " - "/utf8>>/binary,
                                            (gleam@string:inspect(Err@1))/binary>>
                                    )
                                end
                            )
                        end,
                        fun(Is_dir) -> case Is_dir of
                                true ->
                                    go_find_gleam_files(Path, Acc@1);

                                false ->
                                    case gleam_stdlib:string_ends_with(
                                        Path,
                                        <<".gleam"/utf8>>
                                    ) of
                                        true ->
                                            {ok, [Path | Acc@1]};

                                        false ->
                                            {ok, Acc@1}
                                    end
                            end end
                    )
                end
            ) end
    ).

-file("src/gleedoc.gleam", 68).
-spec find_gleam_files(binary()) -> {ok, list(binary())} | {error, snag:snag()}.
find_gleam_files(Source_dir) ->
    go_find_gleam_files(Source_dir, []).

-file("src/gleedoc.gleam", 22).
?DOC(
    " Run gleedoc on a project, extracting doc tests from source files and generating\n"
    " test files in the output directory.\n"
).
-spec run(gleedoc_config()) -> {ok, nil} | {error, snag:snag()}.
run(Config) ->
    gleam@result:'try'(
        find_gleam_files(erlang:element(3, Config)),
        fun(Files) ->
            gleam@result:'try'(
                begin
                    _pipe = Files,
                    _pipe@1 = gleam@list:try_map(
                        _pipe,
                        fun gleedoc@extract:doc_blocks_from_file/1
                    ),
                    gleam@result:map(_pipe@1, fun lists:append/1)
                end,
                fun(Doc_blocks) ->
                    Code_blocks = begin
                        _pipe@2 = Doc_blocks,
                        _pipe@3 = gleedoc@parse:extract_code_blocks(_pipe@2),
                        gleedoc@parse:gleam_blocks(_pipe@3)
                    end,
                    case Code_blocks of
                        [] ->
                            {ok, nil};

                        Blocks ->
                            Gen_config = {config, erlang:element(2, Config)},
                            gleam@result:'try'(
                                gleedoc@generate:clean_generated(
                                    erlang:element(2, Config)
                                ),
                                fun(_) ->
                                    gleam@result:'try'(
                                        gleedoc@generate:generate_tests(
                                            Blocks,
                                            Gen_config
                                        ),
                                        fun(_) -> {ok, nil} end
                                    )
                                end
                            )
                    end
                end
            )
        end
    ).

-file("src/gleedoc.gleam", 59).
?DOC(" CLI entry point. Reads gleam.toml to infer module/package names.\n").
-spec main() -> nil.
main() ->
    Config = {gleedoc_config, <<"test"/utf8>>, <<"src"/utf8>>},
    case run(Config) of
        {ok, nil} ->
            nil;

        {error, Snag} ->
            erlang:error(#{gleam_error => panic,
                    message => erlang:element(2, Snag),
                    file => <<?FILEPATH/utf8>>,
                    module => <<"gleedoc"/utf8>>,
                    function => <<"main"/utf8>>,
                    line => 64})
    end.