Current section

Files

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

src/libero@cli@new.erl

-module(libero@cli@new).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/libero/cli/new.gleam").
-export([scaffold/2]).
-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 new` — scaffold a new Libero project at the given path.\n").
-file("src/libero/cli/new.gleam", 63).
-spec scaffold_files(
binary(),
binary(),
binary(),
gleam@option:option(libero@cli:database())
) -> {ok, nil} | {error, binary()}.
scaffold_files(Name, Path, Server_dir, Database) ->
{Db_deps, Extra_toml, Db_readme} = case Database of
none ->
{<<""/utf8>>, <<""/utf8>>, <<""/utf8>>};
{some, Db} ->
{libero@cli@templates@db:deps(Db),
libero@cli@templates@db:extra_toml(Db),
libero@cli@templates@db:readme_section(Db)}
end,
libero@cli@helpers:map_err(
simplifile:create_directory_all(Server_dir),
fun(_) ->
libero@cli@helpers:map_err(
simplifile:write(
<<Path/binary, "/gleam.toml"/utf8>>,
libero@cli@templates:gleam_toml(Name, Db_deps, Extra_toml)
),
fun(_) ->
libero@cli@helpers:map_err(
libero@cli@helpers:write_formatted(
<<Server_dir/binary, "/handler.gleam"/utf8>>,
libero@cli@templates:starter_handler()
),
fun(_) ->
libero@cli@helpers:map_err(
libero@cli@helpers:write_formatted(
<<Server_dir/binary,
"/shared_state.gleam"/utf8>>,
case Database of
none ->
libero@cli@templates:starter_shared_state(
);
{some, Db@1} ->
libero@cli@templates@db:shared_state(
Db@1
)
end
),
fun(_) ->
libero@cli@helpers:map_err(
libero@cli@helpers:write_formatted(
<<Server_dir/binary,
"/app_error.gleam"/utf8>>,
libero@cli@templates:starter_app_error(
)
),
fun(_) -> (fun(Next) -> case Database of
none ->
Next(nil);
{some, Db@2} ->
libero@cli@helpers:map_err(
libero@cli@helpers:write_formatted(
<<Server_dir/binary,
"/db.gleam"/utf8>>,
libero@cli@templates@db:db_module(
Db@2
)
),
fun(_) ->
libero@cli@helpers:map_err(
simplifile:create_directory_all(
<<Server_dir/binary,
"/sql"/utf8>>
),
fun(_) ->
Next(
nil
)
end
)
end
)
end end)(
fun(_) ->
Shared_dir = <<Path/binary,
"/shared/src/shared"/utf8>>,
libero@cli@helpers:map_err(
simplifile:create_directory_all(
Shared_dir
),
fun(_) ->
libero@cli@helpers:map_err(
simplifile:write(
<<Path/binary,
"/shared/gleam.toml"/utf8>>,
libero@cli@templates:shared_gleam_toml(
)
),
fun(_) ->
libero@cli@helpers:map_err(
libero@cli@helpers:write_formatted(
<<Shared_dir/binary,
"/messages.gleam"/utf8>>,
libero@cli@templates:starter_messages(
)
),
fun(_) ->
Test_dir = <<Path/binary,
"/test"/utf8>>,
libero@cli@helpers:map_err(
simplifile:create_directory_all(
Test_dir
),
fun(
_
) ->
libero@cli@helpers:map_err(
libero@cli@helpers:write_formatted(
<<<<<<Test_dir/binary,
"/"/utf8>>/binary,
Name/binary>>/binary,
"_test.gleam"/utf8>>,
libero@cli@templates:starter_test(
)
),
fun(
_
) ->
libero@cli@helpers:map_err(
simplifile:write(
<<Path/binary,
"/README.md"/utf8>>,
libero@cli@templates:starter_readme(
Name,
Db_readme
)
),
fun(
_
) ->
{ok,
nil}
end
)
end
)
end
)
end
)
end
)
end
)
end
) end
)
end
)
end
)
end
)
end
).
-file("src/libero/cli/new.gleam", 44).
-spec scaffold_validated(
binary(),
binary(),
gleam@option:option(libero@cli:database())
) -> {ok, nil} | {error, binary()}.
scaffold_validated(Name, Path, Database) ->
case simplifile_erl:is_file(<<Path/binary, "/gleam.toml"/utf8>>) of
{ok, true} ->
{error,
<<<<"error: Project already exists
\x{250c}\x{2500} "/utf8,
Path/binary>>/binary,
"/gleam.toml
\x{2502}
\x{2502} A gleam.toml already exists at this path"/utf8>>};
_ ->
Server_dir = <<Path/binary, "/src/server"/utf8>>,
scaffold_files(Name, Path, Server_dir, Database)
end.
-file("src/libero/cli/new.gleam", 22).
?DOC(
" Scaffold a new project under `path`.\n"
"\n"
" The project name is derived from the last segment of the path\n"
" (e.g. \"tmp/test_app\" → \"test_app\").\n"
"\n"
" Creates the directory tree and writes starter source files so the\n"
" project compiles and runs out of the box.\n"
" nolint: stringly_typed_error -- CLI module, String errors are user-facing messages\n"
).
-spec scaffold(binary(), gleam@option:option(libero@cli:database())) -> {ok,
nil} |
{error, binary()}.
scaffold(Path, Database) ->
Name = begin
_pipe = gleam@string:split(Path, <<"/"/utf8>>),
_pipe@1 = gleam@list:last(_pipe),
gleam@result:unwrap(_pipe@1, Path)
end,
case libero@cli@validation:validate_name(
Name,
<<"project"/utf8>>,
<<"gleam run -m libero -- new my_app"/utf8>>
) of
{error, Msg} ->
{error, Msg};
{ok, nil} ->
scaffold_validated(Name, Path, Database)
end.