Current section
Files
Jump to
Current section
Files
src/libero@cli@templates.erl
-module(libero@cli@templates).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/libero/cli/templates.gleam").
-export([gleam_toml/1, shared_gleam_toml/0, starter_messages/0, starter_handler/0, starter_shared_state/0, starter_app_error/0, starter_test/0, starter_spa/1, client_gleam_toml/3, starter_cli/0]).
-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(
" Template strings for `libero new` scaffolding.\n"
"\n"
" Each function returns a file's content as a String. The generated\n"
" files give a new project a minimal skeleton to build from.\n"
).
-file("src/libero/cli/templates.gleam", 8).
?DOC(
" Returns gleam.toml content for a new project (the server package).\n"
" Libero config lives under the [tools.libero] section.\n"
).
-spec gleam_toml(binary()) -> binary().
gleam_toml(Name) ->
<<<<"name = \""/utf8, Name/binary>>/binary,
"\"
version = \"0.1.0\"
target = \"erlang\"
[dependencies]
gleam_stdlib = \">= 0.69.0 and < 1.0.0\"
gleam_erlang = \"~> 1.0\"
gleam_http = \"~> 4.0\"
mist = \"~> 6.0\"
lustre = \"~> 5.6\"
shared = { path = \"shared\" }
libero = \"~> 4.2\"
[dev-dependencies]
gleeunit = \"~> 1.0\"
[tools.libero]
port = 8080
"/utf8>>.
-file("src/libero/cli/templates.gleam", 33).
?DOC(
" Returns gleam.toml content for the shared package.\n"
" Target-agnostic so both the Erlang server and JS clients can import\n"
" messages and types from it.\n"
).
-spec shared_gleam_toml() -> binary().
shared_gleam_toml() ->
<<"name = \"shared\"
version = \"0.1.0\"
# No target specified - compiles to both Erlang and JavaScript.
[dependencies]
gleam_stdlib = \">= 0.69.0 and < 1.0.0\"
libero = \"~> 4.2\"
[dev-dependencies]
gleeunit = \"~> 1.0\"
"/utf8>>.
-file("src/libero/cli/templates.gleam", 53).
?DOC(
" Returns a skeleton messages module.\n"
"\n"
" Defines the typed RPC boundary between client and server.\n"
" Add your message types here — libero scans for MsgFromClient\n"
" and MsgFromServer to generate dispatch and client stubs.\n"
).
-spec starter_messages() -> binary().
starter_messages() ->
<<"/// Define your message types here.
/// Libero scans for MsgFromClient and MsgFromServer to generate
/// dispatch and client stubs.
pub type MsgFromClient {
Ping
}
pub type MsgFromServer {
Pong(String)
}
"/utf8>>.
-file("src/libero/cli/templates.gleam", 69).
?DOC(" Returns a skeleton handler module.\n").
-spec starter_handler() -> binary().
starter_handler() ->
<<"import server/app_error.{type AppError}
import server/shared_state.{type SharedState}
import shared/messages.{type MsgFromClient, type MsgFromServer, Ping, Pong}
pub fn update_from_client(
msg msg: MsgFromClient,
state state: SharedState,
) -> Result(#(MsgFromServer, SharedState), AppError) {
case msg {
Ping -> Ok(#(Pong(\"pong\"), state))
}
}
"/utf8>>.
-file("src/libero/cli/templates.gleam", 86).
?DOC(" Returns a skeleton SharedState module.\n").
-spec starter_shared_state() -> binary().
starter_shared_state() ->
<<"pub type SharedState {
SharedState
}
pub fn new() -> SharedState {
SharedState
}
"/utf8>>.
-file("src/libero/cli/templates.gleam", 98).
?DOC(" Returns a skeleton AppError module.\n").
-spec starter_app_error() -> binary().
starter_app_error() ->
<<"pub type AppError {
AppError(reason: String)
}
"/utf8>>.
-file("src/libero/cli/templates.gleam", 106).
?DOC(" Returns a skeleton test that verifies the handler works.\n").
-spec starter_test() -> binary().
starter_test() ->
<<"import server/handler
import server/shared_state
import shared/messages.{Ping, Pong}
import gleeunit
pub fn main() {
gleeunit.main()
}
pub fn ping_test() {
let state = shared_state.new()
let assert Ok(#(Pong(\"pong\"), _)) =
handler.update_from_client(msg: Ping, state:)
}
"/utf8>>.
-file("src/libero/cli/templates.gleam", 125).
?DOC(" Returns a starter Lustre SPA app module.\n").
-spec starter_spa(binary()) -> binary().
starter_spa(Name) ->
<<<<"import lustre
import lustre/element
import lustre/element/html
pub fn main() {
let app = lustre.element(view())
let assert Ok(_) = lustre.start(app, \"#app\", Nil)
Nil
}
fn view() -> element.Element(msg) {
html.div([], [
html.h1([], [html.text(\""/utf8,
Name/binary>>/binary,
"\")]),
html.p([], [html.text(\"Edit this file to get started.\")]),
])
}
"/utf8>>.
-file("src/libero/cli/templates.gleam", 146).
?DOC(" Returns a gleam.toml for a client package.\n").
-spec client_gleam_toml(binary(), binary(), binary()) -> binary().
client_gleam_toml(Name, Target, Root_package) ->
<<<<<<<<<<<<<<"name = \""/utf8, Name/binary>>/binary,
"\"
version = \"0.1.0\"
target = \""/utf8>>/binary,
Target/binary>>/binary,
"\"
[dependencies]
gleam_stdlib = \">= 0.69.0 and < 1.0.0\"
shared = { path = \"../../shared\" }
"/utf8>>/binary,
Root_package/binary>>/binary,
" = { path = \"../../\" }
libero = \"~> 4.2\"
"/utf8>>/binary,
(case Target of
<<"javascript"/utf8>> ->
<<"lustre = \"~> 5.6\"\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>.
-file("src/libero/cli/templates.gleam", 167).
?DOC(" Returns a starter CLI main module.\n").
-spec starter_cli() -> binary().
starter_cli() ->
<<"import gleam/io
pub fn main() -> Nil {
io.println(\"Hello from your Libero app!\")
}
"/utf8>>.