Packages
lustre
4.0.0-rc1
5.7.1
5.7.0
5.6.0
5.5.2
5.5.1
5.5.0
5.4.0
5.3.5
5.3.4
5.3.3
5.3.2
5.3.1
5.3.0
5.2.1
5.2.0
5.1.1
5.1.0
5.0.3
5.0.2
5.0.1
5.0.0
4.6.4
4.6.3
4.6.2
4.6.1
4.6.0
4.5.1
4.5.0
4.4.4
4.4.3
4.4.1
4.4.0
4.3.6
4.3.5
4.3.4
4.3.3
4.3.2
4.3.1
4.3.0
4.2.6
4.2.5
4.2.4
4.2.3
4.2.2
4.2.1
4.2.0
4.1.8
4.1.7
4.1.6
4.1.5
4.1.4
4.1.3
4.1.2
4.1.1
4.1.0
4.0.0
4.0.0-rc1
4.0.0-rc.2
3.1.4
3.1.3
3.1.2
3.1.1
3.1.0
3.0.12
3.0.11
3.0.10
3.0.9
3.0.8
3.0.7
3.0.6
3.0.5
3.0.4
3.0.3
3.0.2
3.0.1
3.0.0
3.0.0-rc.8
3.0.0-rc.7
3.0.0-rc.6
3.0.0-rc.5
3.0.0-rc.4
3.0.0-rc.3
3.0.0-rc.2
3.0.0-rc.1
2.0.1
2.0.0
1.3.0
1.2.0
1.1.0
1.0.0
Create HTML templates, single page applications, Web Components, and real-time server components in Gleam!
Current section
Files
Jump to
Current section
Files
src/lustre@cli@project.erl
-module(lustre@cli@project).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([build/0, explain/1, root/0, config/0, type_to_string/1, interface/0]).
-export_type([config/0, interface/0, module_/0, function_/0, type/0, error/0]).
-type config() :: {config,
binary(),
binary(),
gleam@dict:dict(binary(), tom:toml())}.
-type interface() :: {interface,
binary(),
binary(),
gleam@dict:dict(binary(), module_())}.
-type module_() :: {module,
gleam@dict:dict(binary(), type()),
gleam@dict:dict(binary(), function_())}.
-type function_() :: {function, list(type()), type()}.
-type type() :: {named, binary(), binary(), binary(), list(type())} |
{variable, integer()} |
{fn, list(type()), type()} |
{tuple, list(type())}.
-type error() :: build_error.
-spec build() -> {ok, nil} | {error, binary()}.
build() ->
lustre@cli@utils:'try'(
shellout:command(
<<"gleam"/utf8>>,
[<<"build"/utf8>>, <<"--target=js"/utf8>>],
<<"."/utf8>>,
[]
),
lustre@cli@utils:map(fun gleam@pair:second/1),
fun(_) -> {ok, nil} end
).
-spec explain(error()) -> nil.
explain(Error) ->
case Error of
build_error ->
_pipe = <<"
It looks like your project has some compilation errors that need to be addressed
before I can do anything."/utf8>>,
gleam@io:println(_pipe)
end.
-spec find_root(binary()) -> binary().
find_root(Path) ->
Toml = filepath:join(Path, <<"gleam.toml"/utf8>>),
case simplifile:verify_is_file(Toml) of
{ok, false} ->
find_root(filepath:join(<<".."/utf8>>, Path));
{error, _} ->
find_root(filepath:join(<<".."/utf8>>, Path));
{ok, true} ->
Path
end.
-spec root() -> binary().
root() ->
find_root(<<"."/utf8>>).
-spec config() -> {ok, config()} | {error, binary()}.
config() ->
gleam@result:'try'(
build(),
fun(_) ->
Configuration_path = filepath:join(root(), <<"gleam.toml"/utf8>>),
_assert_subject = simplifile:read(Configuration_path),
{ok, Configuration} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"lustre/cli/project"/utf8>>,
function => <<"config"/utf8>>,
line => 93})
end,
_assert_subject@1 = tom:parse(Configuration),
{ok, Toml} = case _assert_subject@1 of
{ok, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@1,
module => <<"lustre/cli/project"/utf8>>,
function => <<"config"/utf8>>,
line => 94})
end,
_assert_subject@2 = tom:get_string(Toml, [<<"name"/utf8>>]),
{ok, Name} = case _assert_subject@2 of
{ok, _} -> _assert_subject@2;
_assert_fail@2 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@2,
module => <<"lustre/cli/project"/utf8>>,
function => <<"config"/utf8>>,
line => 95})
end,
_assert_subject@3 = tom:get_string(Toml, [<<"version"/utf8>>]),
{ok, Version} = case _assert_subject@3 of
{ok, _} -> _assert_subject@3;
_assert_fail@3 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@3,
module => <<"lustre/cli/project"/utf8>>,
function => <<"config"/utf8>>,
line => 96})
end,
{ok, {config, Name, Version, Toml}}
end
).
-spec type_to_string(type()) -> binary().
type_to_string(Type_) ->
case Type_ of
{tuple, Elements} ->
Elements@1 = gleam@list:map(Elements, fun type_to_string/1),
<<<<"#("/utf8,
(gleam@string:join(Elements@1, <<", "/utf8>>))/binary>>/binary,
")"/utf8>>;
{fn, Params, Return} ->
Params@1 = gleam@list:map(Params, fun type_to_string/1),
Return@1 = type_to_string(Return),
<<<<<<"fn("/utf8,
(gleam@string:join(Params@1, <<", "/utf8>>))/binary>>/binary,
") -> "/utf8>>/binary,
Return@1/binary>>;
{named, Name, _, _, []} ->
Name;
{named, Name@1, _, _, Params@2} ->
Params@3 = gleam@list:map(Params@2, fun type_to_string/1),
<<<<<<Name@1/binary, "("/utf8>>/binary,
(gleam@string:join(Params@3, <<", "/utf8>>))/binary>>/binary,
")"/utf8>>;
{variable, Id} ->
<<"a_"/utf8, (gleam@int:to_string(Id))/binary>>
end.
-spec decode_variable_type(gleam@dynamic:dynamic_()) -> {ok, type()} |
{error, list(gleam@dynamic:decode_error())}.
decode_variable_type(Dyn) ->
(gleam@dynamic:decode1(
fun(Field@0) -> {variable, Field@0} end,
gleam@dynamic:field(<<"id"/utf8>>, fun gleam@dynamic:int/1)
))(Dyn).
-spec decode_named_type(gleam@dynamic:dynamic_()) -> {ok, type()} |
{error, list(gleam@dynamic:decode_error())}.
decode_named_type(Dyn) ->
(gleam@dynamic:decode4(
fun(Field@0, Field@1, Field@2, Field@3) -> {named, Field@0, Field@1, Field@2, Field@3} end,
gleam@dynamic:field(<<"name"/utf8>>, fun gleam@dynamic:string/1),
gleam@dynamic:field(<<"package"/utf8>>, fun gleam@dynamic:string/1),
gleam@dynamic:field(<<"module"/utf8>>, fun gleam@dynamic:string/1),
gleam@dynamic:field(
<<"parameters"/utf8>>,
gleam@dynamic:list(fun decode_type/1)
)
))(Dyn).
-spec decode_type(gleam@dynamic:dynamic_()) -> {ok, type()} |
{error, list(gleam@dynamic:decode_error())}.
decode_type(Dyn) ->
gleam@result:'try'(
(gleam@dynamic:field(<<"kind"/utf8>>, fun gleam@dynamic:string/1))(Dyn),
fun(Kind) -> case Kind of
<<"named"/utf8>> ->
decode_named_type(Dyn);
<<"variable"/utf8>> ->
decode_variable_type(Dyn);
<<"fn"/utf8>> ->
decode_fn_type(Dyn);
<<"tuple"/utf8>> ->
decode_tuple_type(Dyn);
_ ->
{error,
[{decode_error,
<<"'named' | 'variable' | 'fn'"/utf8>>,
Kind,
[<<"kind"/utf8>>]}]}
end end
).
-spec decode_fn_type(gleam@dynamic:dynamic_()) -> {ok, type()} |
{error, list(gleam@dynamic:decode_error())}.
decode_fn_type(Dyn) ->
(gleam@dynamic:decode2(
fun(Field@0, Field@1) -> {fn, Field@0, Field@1} end,
gleam@dynamic:field(
<<"parameters"/utf8>>,
gleam@dynamic:list(fun decode_type/1)
),
gleam@dynamic:field(<<"return"/utf8>>, fun decode_type/1)
))(Dyn).
-spec decode_tuple_type(gleam@dynamic:dynamic_()) -> {ok, type()} |
{error, list(gleam@dynamic:decode_error())}.
decode_tuple_type(Dyn) ->
(gleam@dynamic:decode1(
fun(Field@0) -> {tuple, Field@0} end,
gleam@dynamic:field(
<<"elements"/utf8>>,
gleam@dynamic:list(fun decode_type/1)
)
))(Dyn).
-spec decode_labelled_argument(gleam@dynamic:dynamic_()) -> {ok, type()} |
{error, list(gleam@dynamic:decode_error())}.
decode_labelled_argument(Dyn) ->
(gleam@dynamic:field(<<"type"/utf8>>, fun decode_type/1))(Dyn).
-spec decode_function(gleam@dynamic:dynamic_()) -> {ok, function_()} |
{error, list(gleam@dynamic:decode_error())}.
decode_function(Dyn) ->
(gleam@dynamic:decode2(
fun(Field@0, Field@1) -> {function, Field@0, Field@1} end,
gleam@dynamic:field(
<<"parameters"/utf8>>,
gleam@dynamic:list(fun decode_labelled_argument/1)
),
gleam@dynamic:field(<<"return"/utf8>>, fun decode_type/1)
))(Dyn).
-spec decode_module(gleam@dynamic:dynamic_()) -> {ok, module_()} |
{error, list(gleam@dynamic:decode_error())}.
decode_module(Dyn) ->
(gleam@dynamic:decode2(
fun(Field@0, Field@1) -> {module, Field@0, Field@1} end,
gleam@dynamic:field(
<<"constants"/utf8>>,
gleam@dynamic:dict(
fun gleam@dynamic:string/1,
gleam@dynamic:field(<<"type"/utf8>>, fun decode_type/1)
)
),
gleam@dynamic:field(
<<"functions"/utf8>>,
gleam@dynamic:dict(
fun gleam@dynamic:string/1,
fun decode_function/1
)
)
))(Dyn).
-spec decode_interface(gleam@dynamic:dynamic_()) -> {ok, interface()} |
{error, list(gleam@dynamic:decode_error())}.
decode_interface(Dyn) ->
(gleam@dynamic:decode3(
fun(Field@0, Field@1, Field@2) -> {interface, Field@0, Field@1, Field@2} end,
gleam@dynamic:field(<<"name"/utf8>>, fun gleam@dynamic:string/1),
gleam@dynamic:field(<<"version"/utf8>>, fun gleam@dynamic:string/1),
gleam@dynamic:field(
<<"modules"/utf8>>,
gleam@dynamic:dict(fun gleam@dynamic:string/1, fun decode_module/1)
)
))(Dyn).
-spec interface() -> {ok, interface()} | {error, binary()}.
interface() ->
Dir = filepath:join(root(), <<"build/.lustre"/utf8>>),
Out = filepath:join(Dir, <<"package-interface.json"/utf8>>),
lustre@cli@utils:'try'(
shellout:command(
<<"gleam"/utf8>>,
[<<"export"/utf8>>,
<<"package-interface"/utf8>>,
<<"--out"/utf8>>,
Out],
<<"."/utf8>>,
[]
),
lustre@cli@utils:map(fun gleam@pair:second/1),
fun(_) ->
_assert_subject = simplifile:read(Out),
{ok, Json} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"lustre/cli/project"/utf8>>,
function => <<"interface"/utf8>>,
line => 75})
end,
_assert_subject@1 = gleam@json:decode(Json, fun decode_interface/1),
{ok, Interface} = case _assert_subject@1 of
{ok, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@1,
module => <<"lustre/cli/project"/utf8>>,
function => <<"interface"/utf8>>,
line => 76})
end,
{ok, Interface}
end
).