Packages

A gleam dev proxy to enable live reload and smoother DX

Current section

Files

Jump to
olive src olive@server_run.erl
Raw

src/olive@server_run.erl

-module(olive@server_run).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([reload_server_code/1, start_server/1]).
-export_type([module_/0, what/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(
" the `server_run` module runs the \"main\" server and also handles the rebuilding\n"
" of the main server code every time it is needed.\n"
).
-type module_() :: any().
-type what() :: any().
-file("src/olive/server_run.gleam", 42).
-spec build_server(olive@config:config()) -> {ok, binary()} | {error, binary()}.
build_server(Config) ->
olive@logging:notice(
erlang:element(2, Config),
<<"Launching new build via `gleam build`"/utf8>>
),
_pipe = shellout:command(
<<"gleam"/utf8>>,
[<<"build"/utf8>>],
erlang:element(6, Config),
[let_be_stderr, let_be_stdout]
),
gleam@result:replace_error(
_pipe,
<<"Error while building gleam project, see gleam output ☝️"/utf8>>
).
-file("src/olive/server_run.gleam", 34).
-spec reload_server_code(olive@config:config()) -> {ok, nil} | {error, binary()}.
reload_server_code(Config) ->
_pipe = build_server(Config),
gleam@result:'try'(_pipe, fun(_) -> _pipe@1 = olive_ffi:reload_modules(),
gleam@result:map_error(
_pipe@1,
fun(_) -> <<"Error while reloading Erlang modules"/utf8>> end
) end).
-file("src/olive/server_run.gleam", 53).
-spec get_atom(binary()) -> {ok, gleam@erlang@atom:atom_()} | {error, binary()}.
get_atom(Name) ->
_pipe = gleam_erlang_ffi:atom_from_string(Name),
gleam@result:replace_error(
_pipe,
<<"Cannot find loaded atom "/utf8, Name/binary>>
).
-file("src/olive/server_run.gleam", 58).
-spec posix_error_to_string(olive@config:config(), gleam@erlang@atom:atom_()) -> binary().
posix_error_to_string(Config, Posix) ->
<<<<<<"Cannot launch server in "/utf8, (erlang:element(6, Config))/binary>>/binary,
" because of the following: "/utf8>>/binary,
(erlang:atom_to_binary(Posix))/binary>>.
-file("src/olive/server_run.gleam", 26).
-spec start_server(olive@config:config()) -> {ok, gleam@erlang@process:pid_()} |
{error, binary()}.
start_server(Config) ->
gleam@result:'try'(
build_server(Config),
fun(_) ->
gleam@result:'try'(
get_atom(<<(erlang:element(7, Config))/binary, "@@main"/utf8>>),
fun(Fully_qualified_module) ->
gleam@result:'try'(
get_atom(erlang:element(7, Config)),
fun(Module) ->
_pipe = olive_ffi:spawn_main_server(
erlang:element(6, Config),
Fully_qualified_module,
Module
),
gleam@result:map_error(
_pipe,
fun(_capture) ->
posix_error_to_string(Config, _capture)
end
)
end
)
end
)
end
).