Current section
Files
Jump to
Current section
Files
src/ghtml@scanner.erl
-module(ghtml@scanner).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/ghtml/scanner.gleam").
-export([to_output_path/1, to_source_path/1, find_ghtml_files/1, find_generated_files/1, find_orphans/1, cleanup_orphans/1]).
-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/ghtml/scanner.gleam", 29).
?DOC(false).
-spec to_output_path(binary()) -> binary().
to_output_path(Ghtml_path) ->
gleam@string:replace(Ghtml_path, <<".ghtml"/utf8>>, <<".gleam"/utf8>>).
-file("src/ghtml/scanner.gleam", 34).
?DOC(false).
-spec to_source_path(binary()) -> binary().
to_source_path(Gleam_path) ->
gleam@string:replace(Gleam_path, <<".gleam"/utf8>>, <<".ghtml"/utf8>>).
-file("src/ghtml/scanner.gleam", 64).
?DOC(false).
-spec is_orphaned_generated_file(binary()) -> boolean().
is_orphaned_generated_file(Gleam_path) ->
case simplifile:read(Gleam_path) of
{ok, Content} ->
case ghtml@cache:is_generated(Content) of
true ->
Source_path = to_source_path(Gleam_path),
case simplifile_erl:is_file(Source_path) of
{ok, true} ->
false;
_ ->
true
end;
false ->
false
end;
{error, _} ->
false
end.
-file("src/ghtml/scanner.gleam", 84).
?DOC(false).
-spec find_recursive(binary(), list(binary()), binary()) -> list(binary()).
find_recursive(Dir, Acc, Extension) ->
case simplifile_erl:read_directory(Dir) of
{ok, Entries} ->
gleam@list:fold(
Entries,
Acc,
fun(Acc@1, Entry) ->
case gleam@list:contains(
[<<"build"/utf8>>,
<<".git"/utf8>>,
<<"node_modules"/utf8>>,
<<"_build"/utf8>>,
<<".claude"/utf8>>,
<<"fixtures"/utf8>>,
<<"examples"/utf8>>,
<<"generated"/utf8>>],
Entry
) of
true ->
Acc@1;
false ->
Path = <<<<Dir/binary, "/"/utf8>>/binary,
Entry/binary>>,
case simplifile_erl:is_directory(Path) of
{ok, true} ->
find_recursive(Path, Acc@1, Extension);
_ ->
case gleam_stdlib:string_ends_with(
Entry,
Extension
) of
true ->
[Path | Acc@1];
false ->
Acc@1
end
end
end
end
);
{error, _} ->
Acc
end.
-file("src/ghtml/scanner.gleam", 19).
?DOC(false).
-spec find_ghtml_files(binary()) -> list(binary()).
find_ghtml_files(Root) ->
find_recursive(Root, [], <<".ghtml"/utf8>>).
-file("src/ghtml/scanner.gleam", 24).
?DOC(false).
-spec find_generated_files(binary()) -> list(binary()).
find_generated_files(Root) ->
find_recursive(Root, [], <<".gleam"/utf8>>).
-file("src/ghtml/scanner.gleam", 40).
?DOC(false).
-spec find_orphans(binary()) -> list(binary()).
find_orphans(Root) ->
_pipe = find_generated_files(Root),
gleam@list:filter(_pipe, fun is_orphaned_generated_file/1).
-file("src/ghtml/scanner.gleam", 47).
?DOC(false).
-spec cleanup_orphans(binary()) -> integer().
cleanup_orphans(Root) ->
_pipe = find_orphans(Root),
gleam@list:fold(
_pipe,
0,
fun(Count, Path) -> case simplifile_erl:delete(Path) of
{ok, _} ->
gleam_stdlib:println(
<<"Removed orphan: "/utf8, Path/binary>>
),
Count + 1;
{error, _} ->
gleam_stdlib:println(
<<"Failed to remove: "/utf8, Path/binary>>
),
Count
end end
).