Packages

A gleam dev proxy to enable live reload and smoother DX

Current section

Files

Jump to
olive src olive@config.erl
Raw

src/olive@config.erl

-module(olive@config).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/olive/config.gleam").
-export([read_config/2]).
-export_type([directory/0, config/0]).
-type directory() :: {source_directory, binary()} | {priv_directory, binary()}.
-type config() :: {config,
olive@logging:logger(),
integer(),
integer(),
binary(),
binary(),
binary(),
list(directory()),
integer()}.
-file("src/olive/config.gleam", 124).
-spec check_directory(binary(), fun((binary()) -> directory())) -> gleam@option:option(directory()).
check_directory(Path, Ctor) ->
case filepath:expand(Path) of
{ok, Full_path} ->
case simplifile_erl:is_directory(Full_path) of
{ok, true} ->
{some, Ctor(Full_path)};
{ok, false} ->
none;
{error, _} ->
none
end;
{error, nil} ->
none
end.
-file("src/olive/config.gleam", 116).
-spec check_source_directory(binary()) -> gleam@option:option(directory()).
check_source_directory(Root) ->
check_directory(
filepath:join(Root, <<"src"/utf8>>),
fun(Field@0) -> {source_directory, Field@0} end
).
-file("src/olive/config.gleam", 120).
-spec check_priv_directory(binary()) -> gleam@option:option(directory()).
check_priv_directory(Root) ->
check_directory(
filepath:join(Root, <<"priv"/utf8>>),
fun(Field@0) -> {priv_directory, Field@0} end
).
-file("src/olive/config.gleam", 142).
-spec get_root(binary()) -> {ok, binary()} | {error, binary()}.
get_root(Path) ->
case simplifile_erl:is_file(filepath:join(Path, <<"gleam.toml"/utf8>>)) of
{ok, true} ->
{ok, Path};
{ok, false} ->
case filepath:expand(filepath:join(Path, <<".."/utf8>>)) of
{ok, Path@1} ->
get_root(Path@1);
{error, _} ->
{error,
<<"Could not locate root dir where gleam.toml is"/utf8>>}
end;
{error, _} ->
case filepath:expand(filepath:join(Path, <<".."/utf8>>)) of
{ok, Path@1} ->
get_root(Path@1);
{error, _} ->
{error,
<<"Could not locate root dir where gleam.toml is"/utf8>>}
end
end.
-file("src/olive/config.gleam", 164).
-spec print_deps(list(directory())) -> binary().
print_deps(Deps) ->
gleam@string:join(gleam@list:map(Deps, fun(Dir) -> case Dir of
{priv_directory, Path} ->
<<"Any changes in: "/utf8, Path/binary>>;
{source_directory, Path@1} ->
<<"Code changes in: "/utf8, Path@1/binary>>
end end), <<"\n"/utf8>>).
-file("src/olive/config.gleam", 178).
-spec cwd_error_to_string(gleam@erlang@atom:atom_()) -> binary().
cwd_error_to_string(Posix_error) ->
<<"Could not get current working dir, error is: "/utf8,
(erlang:atom_to_binary(Posix_error))/binary>>.
-file("src/olive/config.gleam", 136).
-spec get_gleam_toml_path() -> {ok, binary()} | {error, binary()}.
get_gleam_toml_path() ->
_pipe = olive_ffi:get_cwd(),
_pipe@1 = gleam@result:map_error(_pipe, fun cwd_error_to_string/1),
gleam@result:'try'(_pipe@1, fun get_root/1).
-file("src/olive/config.gleam", 182).
-spec simplifile_error_to_string(simplifile:file_error()) -> binary().
simplifile_error_to_string(Err) ->
<<"Could not read gleam.toml: "/utf8,
(simplifile:describe_error(Err))/binary>>.
-file("src/olive/config.gleam", 186).
-spec tom_error_to_string(tom:parse_error()) -> binary().
tom_error_to_string(Parse_error) ->
case Parse_error of
{key_already_in_use, Keys} ->
<<"Could not parse gleam.toml: Key already in use: "/utf8,
(gleam@string:join(Keys, <<", "/utf8>>))/binary>>;
{unexpected, Char, _} ->
<<"Could not parse gleam.toml: Unexpected character: "/utf8,
Char/binary>>
end.
-file("src/olive/config.gleam", 154).
-spec read_gleam_toml(binary()) -> {ok, gleam@dict:dict(binary(), tom:toml())} |
{error, binary()}.
read_gleam_toml(Path) ->
_pipe = filepath:join(Path, <<"gleam.toml"/utf8>>),
_pipe@1 = simplifile:read(_pipe),
_pipe@2 = gleam@result:map_error(_pipe@1, fun simplifile_error_to_string/1),
gleam@result:'try'(_pipe@2, fun(Content) -> _pipe@3 = tom:parse(Content),
gleam@result:map_error(_pipe@3, fun tom_error_to_string/1) end).
-file("src/olive/config.gleam", 196).
-spec tom_field_error_to_string(tom:get_error()) -> binary().
tom_field_error_to_string(Parse_error) ->
case Parse_error of
{not_found, Keys} ->
<<<<"Could not find "/utf8,
(gleam@string:join(Keys, <<"."/utf8>>))/binary>>/binary,
" in gleam.toml"/utf8>>;
{wrong_type, Keys@1, Expected, Got} ->
<<<<<<<<<<"Could not read "/utf8,
(gleam@string:join(Keys@1, <<"."/utf8>>))/binary>>/binary,
". Expected "/utf8>>/binary,
Expected/binary>>/binary,
" but got "/utf8>>/binary,
Got/binary>>
end.
-file("src/olive/config.gleam", 77).
-spec read_project_name(gleam@dict:dict(binary(), tom:toml())) -> {ok, binary()} |
{error, binary()}.
read_project_name(Toml) ->
_pipe = tom:get_string(Toml, [<<"name"/utf8>>]),
gleam@result:map_error(_pipe, fun tom_field_error_to_string/1).
-file("src/olive/config.gleam", 81).
-spec read_project_dependencies(gleam@dict:dict(binary(), tom:toml()), binary()) -> {ok,
list(directory())} |
{error, binary()}.
read_project_dependencies(Toml, Root) ->
Source_directory = check_source_directory(Root),
Priv_directory = check_priv_directory(Root),
_pipe = tom:get_table(Toml, [<<"dependencies"/utf8>>]),
_pipe@1 = gleam@result:map_error(_pipe, fun tom_field_error_to_string/1),
gleam@result:map(_pipe@1, fun(Deps) -> _pipe@2 = Deps,
_pipe@3 = gleam@dict:fold(
_pipe@2,
[Source_directory, Priv_directory],
fun(Acc, _, Value) -> case Value of
{inline_table, Table} ->
case gleam_stdlib:map_get(Table, <<"path"/utf8>>) of
{error, _} ->
Acc;
{ok, {string, Path}} ->
Full_path = filepath:join(Root, Path),
Source_dir = check_source_directory(
Full_path
),
Priv_dir = check_priv_directory(Full_path),
[Source_dir, Priv_dir | Acc];
{ok, _} ->
Acc
end;
_ ->
Acc
end end
),
gleam@option:values(_pipe@3) end).
-file("src/olive/config.gleam", 43).
-spec read_config(olive@logging:logger(), olive@cli:cli_options()) -> {ok,
config()} |
{error, binary()}.
read_config(Logger, Options) ->
olive@logging:notice(
Logger,
<<"Reading gleam.toml to retrieve project's name and directories to watch"/utf8>>
),
gleam@result:'try'(
get_gleam_toml_path(),
fun(Root) ->
gleam@result:'try'(
read_gleam_toml(Root),
fun(Gleam_toml) ->
gleam@result:'try'(
read_project_name(Gleam_toml),
fun(Name) ->
gleam@result:'try'(
read_project_dependencies(Gleam_toml, Root),
fun(Deps) ->
olive@logging:debug(
Logger,
<<"Olive will watch for changes in those folders:\n"/utf8,
(print_deps(Deps))/binary>>
),
{ok,
{config,
Logger,
erlang:element(2, Options),
erlang:element(3, Options),
erlang:element(4, Options),
Root,
Name,
Deps,
erlang:element(6, Options)}}
end
)
end
)
end
)
end
).