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([start_server/2, reload_server_code/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", 26).
-spec start_server(binary(), binary()) -> {ok, gleam@erlang@process:pid_()} |
{error, gleam@erlang@atom:atom_()}.
start_server(Root, Name) ->
_assert_subject = gleam_erlang_ffi:atom_from_string(
<<Name/binary, "@@main"/utf8>>
),
{ok, Fully_qualified_module} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"olive/server_run"/utf8>>,
function => <<"start_server"/utf8>>,
line => 27})
end,
_assert_subject@1 = gleam_erlang_ffi:atom_from_string(Name),
{ok, Module} = case _assert_subject@1 of
{ok, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@1,
module => <<"olive/server_run"/utf8>>,
function => <<"start_server"/utf8>>,
line => 28})
end,
olive_ffi:spawn_main_server(Root, Fully_qualified_module, Module).
-file("src/olive/server_run.gleam", 32).
-spec reload_server_code(binary()) -> {ok, nil} | {error, binary()}.
reload_server_code(Root) ->
_pipe = shellout:command(<<"gleam"/utf8>>, [<<"build"/utf8>>], Root, []),
_pipe@1 = gleam@result:map_error(
_pipe,
fun(Err) ->
{Status, Msg} = Err,
<<<<<<"Error while building gleam project:\n"/utf8,
(erlang:integer_to_binary(Status))/binary>>/binary,
" - "/utf8>>/binary,
Msg/binary>>
end
),
gleam@result:'try'(
_pipe@1,
fun(Output) ->
olive@logging:notice(
<<"Output of `gleam build`:\n"/utf8, Output/binary>>
),
_pipe@2 = olive_ffi:reload_modules(),
gleam@result:map_error(
_pipe@2,
fun(_) -> <<"Error while reloading Erlang modules"/utf8>> end
)
end
).