src/glipt@internal@cache.erl

-module(glipt@internal@cache).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glipt/internal/cache.gleam").
-export([cache_dir/0, cached_project_path/1, is_cached/1, ensure_cache_dir/0, clear_cache/0, cache_key/2]).

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

?MODULEDOC(false).

-file("src/glipt/internal/cache.gleam", 6).
?DOC(false).
-spec cache_dir() -> binary().
cache_dir() ->
    Home = glipt_ffi:get_home_dir(),
    <<Home/binary, "/.cache/glipt"/utf8>>.

-file("src/glipt/internal/cache.gleam", 21).
?DOC(false).
-spec cached_project_path(binary()) -> binary().
cached_project_path(Key) ->
    <<<<(cache_dir())/binary, "/"/utf8>>/binary, Key/binary>>.

-file("src/glipt/internal/cache.gleam", 25).
?DOC(false).
-spec is_cached(binary()) -> boolean().
is_cached(Key) ->
    Path = <<(cached_project_path(Key))/binary, "/build"/utf8>>,
    case simplifile_erl:is_directory(Path) of
        {ok, true} ->
            true;

        _ ->
            false
    end.

-file("src/glipt/internal/cache.gleam", 33).
?DOC(false).
-spec ensure_cache_dir() -> {ok, nil} | {error, simplifile:file_error()}.
ensure_cache_dir() ->
    simplifile:create_directory_all(cache_dir()).

-file("src/glipt/internal/cache.gleam", 37).
?DOC(false).
-spec clear_cache() -> {ok, nil} | {error, simplifile:file_error()}.
clear_cache() ->
    Dir = cache_dir(),
    case simplifile_erl:is_directory(Dir) of
        {ok, true} ->
            simplifile_erl:delete(Dir);

        _ ->
            {ok, nil}
    end.

-file("src/glipt/internal/cache.gleam", 11).
?DOC(false).
-spec cache_key(binary(), glipt@parser:script_meta()) -> binary().
cache_key(Source, Meta) ->
    Dep_str = begin
        _pipe = erlang:element(4, Meta),
        _pipe@1 = gleam@list:sort(
            _pipe,
            fun(A, B) ->
                gleam@string:compare(erlang:element(2, A), erlang:element(2, B))
            end
        ),
        _pipe@2 = gleam@list:map(
            _pipe@1,
            fun(D) ->
                <<<<(erlang:element(2, D))/binary, ":"/utf8>>/binary,
                    (erlang:element(3, D))/binary>>
            end
        ),
        gleam@string:join(_pipe@2, <<","/utf8>>)
    end,
    Input = <<<<Source/binary, "\n"/utf8>>/binary, Dep_str/binary>>,
    glipt_ffi:sha256_hex(Input).