-module(olive@config). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([read_config/1]). -export_type([config/0]). -type config() :: {config, integer(), integer(), binary(), binary(), binary(), list(binary())}. -file("src/olive/config.gleam", 94). -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", 106). -spec get_full_path(binary(), binary()) -> binary(). get_full_path(Root, Path) -> _assert_subject = begin _pipe = filepath:join(Root, Path), _pipe@1 = filepath:join(_pipe, <<"src"/utf8>>), filepath:expand(_pipe@1) end, {ok, Full_path} = 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/config"/utf8>>, function => <<"get_full_path"/utf8>>, line => 109}) end, Full_path. -file("src/olive/config.gleam", 128). -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", 88). -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:then(_pipe@1, fun get_root/1). -file("src/olive/config.gleam", 132). -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", 136). -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", 116). -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", 146). -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", 61). -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", 65). -spec read_project_dependencies(gleam@dict:dict(binary(), tom:toml()), binary()) -> {ok, list(binary())} | {error, binary()}. read_project_dependencies(Toml, 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, gleam@dict:fold( _pipe@2, [filepath:join(Root, <<"src"/utf8>>)], fun(Acc, _, Value) -> case Value of {inline_table, Table} -> case gleam_stdlib:map_get(Table, <<"path"/utf8>>) of {error, _} -> Acc; {ok, {string, Path}} -> [get_full_path(Root, Path) | Acc]; {ok, _} -> Acc end; _ -> Acc end end ) end). -file("src/olive/config.gleam", 33). -spec read_config(olive@cli:cli_options()) -> {ok, config()} | {error, binary()}. read_config(Options) -> olive@logging:notice( <<"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:notice( <<"Olive will watch for changes in those folders:\n"/utf8, (gleam@string:join( Deps, <<"\n"/utf8>> ))/binary>> ), {ok, {config, erlang:element(2, Options), erlang:element(3, Options), erlang:element(4, Options), Root, Name, Deps}} end ) end ) end ) end ).