Current section
Files
Jump to
Current section
Files
src/libero@format.erl
-module(libero@format).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/libero/format.gleam").
-export([format_gleam/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(
" Run `gleam format` on generated Gleam code.\n"
"\n"
" Writes code to a temp file, runs the formatter, reads back the result.\n"
" Falls back to the original string if formatting fails.\n"
).
-file("src/libero/format.gleam", 58).
-spec run_executable(binary(), list(binary())) -> integer().
run_executable(Executable, Args) ->
case libero_cli_ffi:find_executable(Executable) of
none ->
-1;
{some, Path} ->
libero_cli_ffi:run_executable(Path, Args)
end.
-file("src/libero/format.gleam", 41).
-spec run_format(binary(), binary()) -> binary().
run_format(Tmp, Fallback) ->
Exit_code = run_executable(<<"gleam"/utf8>>, [<<"format"/utf8>>, Tmp]),
case Exit_code of
0 ->
_pipe = simplifile:read(Tmp),
gleam@result:unwrap(_pipe, Fallback);
_ ->
gleam_stdlib:println_error(
<<<<"warning: gleam format failed (exit code "/utf8,
(erlang:integer_to_binary(Exit_code))/binary>>/binary,
"), using unformatted output"/utf8>>
),
Fallback
end.
-file("src/libero/format.gleam", 99).
-spec find_env(list(binary()), binary()) -> gleam@option:option(binary()).
find_env(Entries, Prefix) ->
case Entries of
[] ->
none;
[Entry | Rest] ->
case gleam_stdlib:string_starts_with(Entry, Prefix) of
true ->
{some,
gleam@string:drop_start(Entry, string:length(Prefix))};
false ->
find_env(Rest, Prefix)
end
end.
-file("src/libero/format.gleam", 91).
?DOC(" nolint: thrown_away_error -- env var lookup failure is expected (var not set)\n").
-spec get_env(binary()) -> gleam@option:option(binary()).
get_env(Name) ->
Raw = os:getenv(),
case gleam@dynamic@decode:run(
Raw,
gleam@dynamic@decode:list(
{decoder, fun gleam@dynamic@decode:decode_string/1}
)
) of
{ok, Entries} ->
find_env(Entries, <<Name/binary, "="/utf8>>);
{error, _} ->
none
end.
-file("src/libero/format.gleam", 77).
-spec get_tmp_dir() -> binary().
get_tmp_dir() ->
_pipe = get_env(<<"TMPDIR"/utf8>>),
_pipe@1 = gleam@option:lazy_or(_pipe, fun() -> get_env(<<"TMP"/utf8>>) end),
_pipe@2 = gleam@option:lazy_or(
_pipe@1,
fun() -> get_env(<<"TEMP"/utf8>>) end
),
gleam@option:unwrap(_pipe@2, <<"/tmp"/utf8>>).
-file("src/libero/format.gleam", 18).
?DOC(
" Format a string of Gleam code using `gleam format`.\n"
" Returns the formatted code, or the original if formatting fails.\n"
" nolint: thrown_away_error -- intentional fallback: formatting is best-effort\n"
).
-spec format_gleam(binary()) -> binary().
format_gleam(Code) ->
Suffix = <<<<(erlang:integer_to_binary(
gleam@int:absolute_value(erlang:unique_integer())
))/binary,
"_"/utf8>>/binary,
(erlang:integer_to_binary(rand:uniform(999999)))/binary>>,
Tmp_dir = get_tmp_dir(),
Tmp = <<<<<<Tmp_dir/binary, "/libero_fmt_"/utf8>>/binary, Suffix/binary>>/binary,
".gleam"/utf8>>,
case simplifile:write(Tmp, Code) of
{error, _} ->
gleam_stdlib:println_error(
<<"warning: could not write temp file for formatting, skipping gleam format"/utf8>>
),
Code;
{ok, _} ->
Formatted = run_format(Tmp, Code),
_ = simplifile_erl:delete(Tmp),
Formatted
end.