Current section
Files
Jump to
Current section
Files
src/checkmark.erl
-module(checkmark).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new/0, snippets_in/2, filtering/2, using/2, print_failures/2, check_in_current_package/2, check_in_tmp_package/2]).
-export_type([operation/0, check_error/0, check_config/0]).
-type operation() :: check | build | run.
-type check_error() :: {run_failed, binary()} | {check_failed, binary()}.
-opaque check_config() :: {check_config,
binary(),
fun((binary()) -> boolean()),
operation()}.
-file("/home/sbergen/source/checkmark/src/checkmark.gleam", 48).
-spec new() -> check_config().
new() ->
{check_config, <<"README.md"/utf8>>, fun(_) -> true end, check}.
-file("/home/sbergen/source/checkmark/src/checkmark.gleam", 53).
-spec snippets_in(check_config(), binary()) -> check_config().
snippets_in(Config, Filename) ->
erlang:setelement(2, Config, Filename).
-file("/home/sbergen/source/checkmark/src/checkmark.gleam", 58).
-spec filtering(check_config(), fun((binary()) -> boolean())) -> check_config().
filtering(Config, Filter) ->
erlang:setelement(3, Config, Filter).
-file("/home/sbergen/source/checkmark/src/checkmark.gleam", 63).
-spec using(check_config(), operation()) -> check_config().
using(Config, Operation) ->
erlang:setelement(4, Config, Operation).
-file("/home/sbergen/source/checkmark/src/checkmark.gleam", 131).
-spec summarize_results(list({ok, nil} | {error, check_error()})) -> gleam@option:option(binary()).
summarize_results(Results) ->
{some,
begin
Indexed = begin
_pipe = gleam@list:range(1, erlang:length(Results)),
gleam@list:zip(_pipe, Results)
end,
_pipe@1 = (gleam@list:flat_map(
Indexed,
fun(_use0) ->
{Index, Result} = _use0,
{Status, Error} = case Result of
{error, E} ->
case E of
{check_failed, E@1} ->
{<<"Check failed!"/utf8>>, {some, E@1}};
{run_failed, E@2} ->
{<<"Failed to run check!"/utf8>>,
{some, E@2}}
end;
{ok, _} ->
{<<"Check passed!"/utf8>>, none}
end,
[{some, <<""/utf8>>},
{some,
<<<<<<"Snippet "/utf8,
(gleam@string:inspect(Index))/binary>>/binary,
": "/utf8>>/binary,
Status/binary>>},
{some,
<<"-----------------------------------------"/utf8>>},
Error]
end
)),
_pipe@2 = gleam@option:values(_pipe@1),
gleam@string:join(_pipe@2, <<"\n"/utf8>>)
end}.
-file("/home/sbergen/source/checkmark/src/checkmark.gleam", 105).
-spec print_failures(
{ok, list({ok, nil} | {error, check_error()})} | {error, binary()},
boolean()
) -> nil.
print_failures(Result, Panic_if_failed) ->
Error_messages = case Result of
{ok, Results} ->
case gleam@list:any(Results, fun gleam@result:is_error/1) of
true ->
summarize_results(Results);
false ->
none
end;
{error, Error} ->
{some, <<"Failed to run: "/utf8, Error/binary>>}
end,
case Error_messages of
{some, Errors} ->
gleam@io:println_error(Errors),
case Panic_if_failed of
true ->
erlang:error(#{gleam_error => panic,
message => <<"checkmark checks failed!"/utf8>>,
module => <<"checkmark"/utf8>>,
function => <<"print_failures"/utf8>>,
line => 123});
false ->
nil
end;
none ->
nil
end.
-file("/home/sbergen/source/checkmark/src/checkmark.gleam", 196).
-spec extract_gleam_code(binary(), fun((binary()) -> boolean())) -> list(binary()).
extract_gleam_code(Markdown, Filter) ->
Tokens = kirala@markdown@parser:parse_all(Markdown),
gleam@list:filter_map(Tokens, fun(Token) -> case Token of
{code_block, <<"gleam"/utf8>>, _, Code} ->
case Filter(Code) of
true ->
{ok, Code};
_ ->
{error, nil}
end;
_ ->
{error, nil}
end end).
-file("/home/sbergen/source/checkmark/src/checkmark.gleam", 184).
-spec check_snippets_in(
binary(),
fun((binary()) -> boolean()),
fun((binary()) -> {ok, nil} | {error, check_error()})
) -> {ok, list({ok, nil} | {error, check_error()})} | {error, binary()}.
check_snippets_in(Filename, Filter, Check) ->
gleam@result:'try'(
begin
_pipe = simplifile:read(Filename),
gleam@result:map_error(_pipe, fun gleam@string:inspect/1)
end,
fun(Content) ->
{ok,
begin
_pipe@1 = extract_gleam_code(Content, Filter),
gleam@list:map(_pipe@1, Check)
end}
end
).
-file("/home/sbergen/source/checkmark/src/checkmark.gleam", 242).
-spec with_tempdir(
fun((binary()) -> {ok, list({ok, nil} | {error, check_error()})} |
{error, binary()})
) -> {ok, list({ok, nil} | {error, check_error()})} | {error, binary()}.
with_tempdir(Operation) ->
_pipe = (temporary:create(
temporary:directory(),
fun(Temp_dir) -> Operation(Temp_dir) end
)),
_pipe@1 = gleam@result:map_error(_pipe, fun gleam@string:inspect/1),
gleam@result:flatten(_pipe@1).
-file("/home/sbergen/source/checkmark/src/checkmark.gleam", 263).
-spec create_file(binary(), boolean()) -> {ok, nil} | {error, check_error()}.
create_file(Path, Allow_overwrite) ->
case simplifile:create_file(Path) of
{error, eexist} when Allow_overwrite ->
{ok, nil};
{ok, _} ->
{ok, nil};
{error, E} ->
{error, {run_failed, gleam@string:inspect(E)}}
end.
-file("/home/sbergen/source/checkmark/src/checkmark.gleam", 253).
-spec with_tempfile(
binary(),
boolean(),
fun((binary()) -> {ok, nil} | {error, check_error()})
) -> {ok, nil} | {error, check_error()}.
with_tempfile(Path, Allow_overwrite, Operation) ->
gleam@result:'try'(
create_file(Path, Allow_overwrite),
fun(_) ->
exception_ffi:defer(
fun() -> simplifile_erl:delete(Path) end,
fun() -> Operation(Path) end
)
end
).
-file("/home/sbergen/source/checkmark/src/checkmark.gleam", 271).
-spec run_gleam(binary(), list(binary()), fun((binary()) -> HKI)) -> {ok, nil} |
{error, HKI}.
run_gleam(Directory, Args, Make_error) ->
case shellout:command(<<"gleam"/utf8>>, Args, Directory, []) of
{ok, _} ->
{ok, nil};
{error, {_, E}} ->
{error, Make_error(E)}
end.
-file("/home/sbergen/source/checkmark/src/checkmark.gleam", 282).
-spec to_args(operation(), binary()) -> list(binary()).
to_args(Op, Module) ->
case Op of
build ->
[<<"build"/utf8>>];
check ->
[<<"check"/utf8>>];
run ->
[<<"run"/utf8>>, <<"--module"/utf8>>, Module]
end.
-file("/home/sbergen/source/checkmark/src/checkmark.gleam", 215).
-spec check_in_dir(binary(), binary(), binary(), boolean(), operation()) -> {ok,
nil} |
{error, check_error()}.
check_in_dir(Code, Package_dir, Filename, Allow_overwrite, Operation) ->
Source_dir = filepath:join(Package_dir, <<"src"/utf8>>),
Source_file = filepath:join(Source_dir, Filename),
with_tempfile(
Source_file,
Allow_overwrite,
fun(File) ->
gleam@result:'try'(
begin
_pipe = simplifile:write(File, Code),
gleam@result:map_error(
_pipe,
fun(E) -> {run_failed, gleam@string:inspect(E)} end
)
end,
fun(_) -> case gleam@string:split(Filename, <<"."/utf8>>) of
[] ->
{error,
{check_failed,
<<"Invalid source file name: "/utf8,
Filename/binary>>}};
[Module | _] ->
run_gleam(
Package_dir,
to_args(Operation, Module),
fun(Field@0) -> {check_failed, Field@0} end
)
end end
)
end
).
-file("/home/sbergen/source/checkmark/src/checkmark.gleam", 71).
-spec check_in_current_package(check_config(), binary()) -> {ok,
list({ok, nil} | {error, check_error()})} |
{error, binary()}.
check_in_current_package(Config, Filename) ->
check_snippets_in(
erlang:element(2, Config),
erlang:element(3, Config),
fun(Snippet) ->
check_in_dir(
Snippet,
<<"."/utf8>>,
Filename,
false,
erlang:element(4, Config)
)
end
).
-file("/home/sbergen/source/checkmark/src/checkmark.gleam", 160).
-spec set_up_package(binary(), list(binary())) -> {ok, binary()} |
{error, binary()}.
set_up_package(Tmp_dir, Dependencies) ->
gleam@result:'try'(
run_gleam(
Tmp_dir,
[<<"new"/utf8>>, <<"checkmark_tmp"/utf8>>],
fun gleam@function:identity/1
),
fun(_) ->
Package_dir = filepath:join(Tmp_dir, <<"checkmark_tmp"/utf8>>),
gleam@result:'try'(case Dependencies of
[] ->
{ok, nil};
_ ->
run_gleam(
Package_dir,
[<<"add"/utf8>> | Dependencies],
fun gleam@function:identity/1
)
end, fun(_) -> {ok, Package_dir} end)
end
).
-file("/home/sbergen/source/checkmark/src/checkmark.gleam", 83).
-spec check_in_tmp_package(check_config(), list(binary())) -> {ok,
list({ok, nil} | {error, check_error()})} |
{error, binary()}.
check_in_tmp_package(Config, Dependencies) ->
with_tempdir(
fun(Temp_dir) ->
gleam@result:'try'(
set_up_package(Temp_dir, Dependencies),
fun(Package_dir) ->
check_snippets_in(
erlang:element(2, Config),
erlang:element(3, Config),
fun(Snippet) ->
check_in_dir(
Snippet,
Package_dir,
<<"checkmark_tmp"/utf8, ".gleam"/utf8>>,
true,
erlang:element(4, Config)
)
end
)
end
)
end
).