Current section
Files
Jump to
Current section
Files
src/libero@cli@add.erl
-module(libero@cli@add).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/libero/cli/add.gleam").
-export([add_client/3]).
-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 add` — scaffold a new client inside an existing Libero project.\n").
-file("src/libero/cli/add.gleam", 81).
-spec validate_target(binary(), fun((nil) -> {ok, nil} | {error, binary()})) -> {ok,
nil} |
{error, binary()}.
validate_target(Target, Next) ->
case Target of
<<"javascript"/utf8>> ->
Next(nil);
<<"erlang"/utf8>> ->
Next(nil);
_ ->
{error,
<<<<"error: Invalid target: `"/utf8, Target/binary>>/binary,
"`
\x{2502}
\x{2502} Target must be \"javascript\" or \"erlang\"
\x{2502}
hint: gleam run -m libero -- add <name> --target javascript"/utf8>>}
end.
-file("src/libero/cli/add.gleam", 96).
-spec try_read_root_name(
binary(),
fun((binary()) -> {ok, nil} | {error, binary()})
) -> {ok, nil} | {error, binary()}.
try_read_root_name(Path, Next) ->
case simplifile:read(<<Path/binary, "/gleam.toml"/utf8>>) of
{error, _} ->
Next(<<"app"/utf8>>);
{ok, Content} ->
case libero@toml_config:parse(Content) of
{ok, Cfg} ->
Next(erlang:element(2, Cfg));
{error, _} ->
Next(<<"app"/utf8>>)
end
end.
-file("src/libero/cli/add.gleam", 111).
-spec write_if_missing(
binary(),
binary(),
fun((nil) -> {ok, nil} | {error, binary()})
) -> {ok, nil} | {error, binary()}.
write_if_missing(Path, Content, Next) ->
case begin
_pipe = simplifile_erl:is_file(Path),
gleam@result:unwrap(_pipe, false)
end of
true ->
Next(nil);
false ->
case simplifile:write(Path, Content) of
{ok, nil} ->
Next(nil);
{error, Err} ->
{error, simplifile:describe_error(Err)}
end
end.
-file("src/libero/cli/add.gleam", 18).
?DOC(
" Add a client named `name` with the given `target` to the project at `path`.\n"
"\n"
" Creates `<path>/clients/<name>/src/` and writes a starter app file only\n"
" when the src directory contains no files yet. Generates a `gleam.toml`\n"
" for the client package if missing. Appends the `[tools.libero.clients.<name>]`\n"
" section to the root `gleam.toml` only when that section is absent.\n"
" nolint: stringly_typed_error -- CLI module, String errors are user-facing messages\n"
).
-spec add_client(binary(), binary(), binary()) -> {ok, nil} | {error, binary()}.
add_client(Path, Name, Target) ->
gleam@result:'try'(
libero@cli@validation:validate_name(
Name,
<<"client"/utf8>>,
<<"gleam run -m libero -- add my_client --target javascript"/utf8>>
),
fun(_) ->
validate_target(
Target,
fun(_) ->
Client_dir = <<<<Path/binary, "/clients/"/utf8>>/binary,
Name/binary>>,
Client_src = <<Client_dir/binary, "/src"/utf8>>,
libero@cli@helpers:map_err(
simplifile:create_directory_all(Client_src),
fun(_) ->
Client_toml_path = <<Client_dir/binary,
"/gleam.toml"/utf8>>,
try_read_root_name(
Path,
fun(Root_name) ->
write_if_missing(
Client_toml_path,
libero@cli@templates:client_gleam_toml(
Name,
Target,
Root_name
),
fun(_) ->
libero@cli@helpers:map_err(
simplifile:get_files(Client_src),
fun(Existing) ->
libero@cli@helpers:map_err(
case Existing of
[] ->
{Filename,
Content} = case Target of
<<"javascript"/utf8>> ->
{<<"app.gleam"/utf8>>,
libero@cli@templates:starter_spa(
Name
)};
_ ->
{<<"main.gleam"/utf8>>,
libero@cli@templates:starter_cli(
)}
end,
libero@cli@helpers:write_formatted(
<<<<Client_src/binary,
"/"/utf8>>/binary,
Filename/binary>>,
Content
);
_ ->
{ok, nil}
end,
fun(_) ->
libero@cli@helpers:map_err(
simplifile:read(
<<Path/binary,
"/gleam.toml"/utf8>>
),
fun(
Toml_content
) ->
Already_declared = case libero@toml_config:parse(
Toml_content
) of
{ok,
Cfg} ->
gleam@list:any(
erlang:element(
5,
Cfg
),
fun(
C
) ->
erlang:element(
2,
C
)
=:= Name
end
);
{error,
_} ->
false
end,
case Already_declared of
true ->
{ok,
nil};
false ->
Addition = <<<<<<<<"\n[tools.libero.clients."/utf8,
Name/binary>>/binary,
"]\ntarget = \""/utf8>>/binary,
Target/binary>>/binary,
"\"\n"/utf8>>,
libero@cli@helpers:map_err(
simplifile:append(
<<Path/binary,
"/gleam.toml"/utf8>>,
Addition
),
fun(
_
) ->
{ok,
nil}
end
)
end
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
).