Current section

Files

Jump to
libero src libero@cli@build.erl
Raw

src/libero@cli@build.erl

-module(libero@cli@build).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/libero/cli/build.gleam").
-export([run/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(
" `libero build` — gen + build server + build all clients.\n"
"\n"
" NOTE: The build is not atomic. `gen` writes generated files first,\n"
" then the server and each client are built sequentially. If a client\n"
" build fails, the server artifacts and generated files remain on\n"
" disk. This is acceptable for a development CLI — production\n"
" deployments should use CI pipelines with rollback capability.\n"
).
-file("src/libero/cli/build.gleam", 78).
-spec try_ok(
{ok, nil} | {error, binary()},
fun((nil) -> {ok, nil} | {error, binary()})
) -> {ok, nil} | {error, binary()}.
try_ok(Result, Next) ->
case Result of
{ok, nil} ->
Next(nil);
{error, Msg} ->
{error, Msg}
end.
-file("src/libero/cli/build.gleam", 89).
-spec try_read(binary(), fun((binary()) -> {ok, nil} | {error, binary()})) -> {ok,
nil} |
{error, binary()}.
try_read(Path, Next) ->
case simplifile:read(Path) of
{ok, Content} ->
Next(Content);
{error, _} ->
{error,
<<"error: Cannot read file
\x{250c}\x{2500} "/utf8,
Path/binary>>}
end.
-file("src/libero/cli/build.gleam", 101).
-spec try_parse(
binary(),
fun((libero@toml_config:toml_config()) -> {ok, nil} | {error, binary()})
) -> {ok, nil} | {error, binary()}.
try_parse(Content, Next) ->
case libero@toml_config:parse(Content) of
{ok, Cfg} ->
Next(Cfg);
{error, Msg} ->
{error, Msg}
end.
-file("src/libero/cli/build.gleam", 111).
-spec gleam_build(binary()) -> integer().
gleam_build(Dir) ->
libero_cli_ffi:run_command(Dir, [<<"build"/utf8>>]).
-file("src/libero/cli/build.gleam", 17).
?DOC(
" Run gen, then build the server and each client package.\n"
" nolint: stringly_typed_error -- CLI module, String errors are user-facing messages\n"
).
-spec run(binary()) -> {ok, nil} | {error, binary()}.
run(Project_path) ->
try_ok(
libero@cli@gen:run(Project_path),
fun(_) ->
gleam_stdlib:println(<<"libero: building server..."/utf8>>),
Exit_code = gleam_build(Project_path),
try_ok(case Exit_code of
0 ->
{ok, nil};
_ ->
{error,
<<"error: Server build failed
\x{2502}
\x{2502} `gleam build` exited with an error
\x{2502}
hint: Check the compiler output above for details"/utf8>>}
end, fun(_) ->
try_read(
<<Project_path/binary, "/gleam.toml"/utf8>>,
fun(Toml_content) ->
try_parse(
Toml_content,
fun(Toml_cfg) ->
Client_results = gleam@list:try_map(
erlang:element(5, Toml_cfg),
fun(Client) ->
Client_dir = <<<<Project_path/binary,
"/clients/"/utf8>>/binary,
(erlang:element(2, Client))/binary>>,
case simplifile_erl:is_directory(
Client_dir
) of
{ok, true} ->
gleam_stdlib:println(
<<<<"libero: building client: "/utf8,
(erlang:element(
2,
Client
))/binary>>/binary,
"..."/utf8>>
),
Code = gleam_build(
Client_dir
),
case Code of
0 ->
{ok, nil};
_ ->
{error,
<<<<"error: Client build failed
\x{250c}\x{2500} clients/"/utf8,
(erlang:element(
2,
Client
))/binary>>/binary,
"/
\x{2502}
\x{2502} `gleam build` exited with an error
\x{2502}
hint: Check the compiler output above for details"/utf8>>}
end;
_ ->
gleam_stdlib:println(
<<<<"libero: skipping client "/utf8,
(erlang:element(
2,
Client
))/binary>>/binary,
" (directory not found)"/utf8>>
),
{ok, nil}
end
end
),
try_ok(case Client_results of
{ok, _} ->
{ok, nil};
{error, Msg} ->
{error, Msg}
end, fun(_) ->
gleam_stdlib:println(
<<"libero: build complete"/utf8>>
),
{ok, nil}
end)
end
)
end
)
end)
end
).