Current section

Files

Jump to
mote src mote.erl
Raw

src/mote.erl

-module(mote).
-compile([no_auto_import, nowarn_unused_vars]).
-export([main/0]).
-spec get_project_config() -> gleam@map:map_(bitstring(), mote@toml:value()).
get_project_config() ->
_assert_subject = gleam@erlang@file:read(<<"gleam.toml"/utf8>>),
{ok, Data} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"mote"/utf8>>,
function => <<"get_project_config"/utf8>>,
line => 38})
end,
_assert_subject@1 = tomerl:parse(Data),
{ok, Data@1} = case _assert_subject@1 of
{ok, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@1,
module => <<"mote"/utf8>>,
function => <<"get_project_config"/utf8>>,
line => 39})
end,
Data@1.
-spec launch_warp(binary(), binary(), binary()) -> nil.
launch_warp(Project_name, Pkg_dir, Target) ->
case gleam_erlang_ffi:os_family() of
windows_nt ->
case shellout:command(
<<"warp-packer"/utf8>>,
[<<"-a"/utf8>>,
Target,
<<"-i"/utf8>>,
<<"."/utf8>>,
<<"-e"/utf8>>,
<<"minimal_erl\\bin\\launch.exe"/utf8>>,
<<"-o"/utf8>>,
<<Project_name/binary, ".exe"/utf8>>],
Pkg_dir,
[]
) of
{ok, _} ->
nil;
{error, {_, Err}} ->
gleam@io:println_error(
<<<<"failed to launch warp packer with error `"/utf8,
Err/binary>>/binary,
"`. Have you installed warp and added it to your path?"/utf8>>
),
erlang:error(#{gleam_error => panic,
message => <<"panic expression evaluated"/utf8>>,
module => <<"mote"/utf8>>,
function => <<"launch_warp"/utf8>>,
line => 189})
end;
darwin ->
case shellout:command(
<<"warp-packer"/utf8>>,
[<<"-a"/utf8>>,
Target,
<<"-i"/utf8>>,
<<"."/utf8>>,
<<"-e"/utf8>>,
<<"minimal_erl/bin/launch.exe"/utf8>>,
<<"-o"/utf8>>,
<<Project_name/binary, ".exe"/utf8>>],
Pkg_dir,
[]
) of
{ok, _} ->
nil;
{error, {_, Err@1}} ->
gleam@io:println_error(
<<<<"failed to launch warp packer with error `"/utf8,
Err@1/binary>>/binary,
"`. Have you installed warp and added it to your path?"/utf8>>
),
erlang:error(#{gleam_error => panic,
message => <<"panic expression evaluated"/utf8>>,
module => <<"mote"/utf8>>,
function => <<"launch_warp"/utf8>>,
line => 215})
end;
_ ->
case shellout:command(
<<"warp-packer"/utf8>>,
[<<"-a"/utf8>>,
Target,
<<"-i"/utf8>>,
<<"."/utf8>>,
<<"-e"/utf8>>,
<<"minimal_erl/bin/launch.exe"/utf8>>,
<<"-o"/utf8>>,
<<Project_name/binary, ".exe"/utf8>>],
Pkg_dir,
[]
) of
{ok, _} ->
nil;
{error, {_, Err@2}} ->
gleam@io:println_error(
<<<<"failed to launch warp packer with error `"/utf8,
Err@2/binary>>/binary,
"`. Have you installed warp and added it to your path?"/utf8>>
),
erlang:error(#{gleam_error => panic,
message => <<"panic expression evaluated"/utf8>>,
module => <<"mote"/utf8>>,
function => <<"launch_warp"/utf8>>,
line => 241})
end
end.
-spec apply_whitelists(
binary(),
binary(),
gleam@map:map_(bitstring(), mote@toml:value())
) -> nil.
apply_whitelists(Runtime, Erts_dir, Config) ->
Bin_dir = <<Runtime/binary, "/bin/"/utf8>>,
case mote@toml:get(Config, [<<"mote"/utf8>>, <<"bin_whitelist"/utf8>>]) of
{ok, {value_list, Files}} ->
Files@1 = begin
_pipe = Files,
gleam@list:filter_map(_pipe, fun(Item) -> case Item of
{binary, Bitstring} ->
gleam@bit_string:to_string(Bitstring);
_ ->
{error, nil}
end end)
end,
_pipe@1 = gleam_erlang_ffi:list_directory(Bin_dir),
_pipe@2 = gleam@result:unwrap(_pipe@1, []),
gleam@list:map(
_pipe@2,
fun(File) ->
case begin
_pipe@3 = Files@1,
gleam@list:contains(_pipe@3, File)
end of
true ->
nil;
false ->
_assert_subject = gleam_erlang_ffi:delete_file(
<<Bin_dir/binary, File/binary>>
),
{ok, nil} = case _assert_subject of
{ok, nil} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"mote"/utf8>>,
function => <<"apply_whitelists"/utf8>>,
line => 293})
end,
nil
end
end
),
nil;
_ ->
gleam@io:println(<<"no bin whitelist found, skipping"/utf8>>)
end,
Erts_bin_dir = <<Erts_dir/binary, "/bin/"/utf8>>,
case mote@toml:get(Config, [<<"mote"/utf8>>, <<"erts_bin_whitelist"/utf8>>]) of
{ok, {value_list, Files@2}} ->
Files@3 = begin
_pipe@4 = Files@2,
gleam@list:filter_map(_pipe@4, fun(Item@1) -> case Item@1 of
{binary, Bitstring@1} ->
gleam@bit_string:to_string(Bitstring@1);
_ ->
{error, nil}
end end)
end,
_pipe@5 = gleam_erlang_ffi:list_directory(Erts_bin_dir),
_pipe@6 = gleam@result:unwrap(_pipe@5, []),
gleam@list:map(
_pipe@6,
fun(File@1) ->
case begin
_pipe@7 = Files@3,
gleam@list:contains(_pipe@7, File@1)
end of
true ->
nil;
false ->
_assert_subject@1 = gleam_erlang_ffi:delete_file(
<<Erts_bin_dir/binary, File@1/binary>>
),
{ok, nil} = case _assert_subject@1 of
{ok, nil} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@1,
module => <<"mote"/utf8>>,
function => <<"apply_whitelists"/utf8>>,
line => 325})
end,
nil
end
end
),
nil;
_ ->
gleam@io:println(<<"no erts bin whitelist found, skipping"/utf8>>)
end,
Lib_dir = <<Runtime/binary, "/lib/"/utf8>>,
case mote@toml:get(Config, [<<"mote"/utf8>>, <<"lib_whitelist"/utf8>>]) of
{ok, {value_list, Files@4}} ->
Files@5 = begin
_pipe@8 = Files@4,
gleam@list:filter_map(_pipe@8, fun(Item@2) -> case Item@2 of
{binary, Bitstring@2} ->
gleam@bit_string:to_string(Bitstring@2);
_ ->
{error, nil}
end end)
end,
_pipe@9 = gleam_erlang_ffi:list_directory(Lib_dir),
_pipe@10 = gleam@result:unwrap(_pipe@9, []),
gleam@list:map(
_pipe@10,
fun(File@2) ->
case begin
_pipe@11 = Files@5,
gleam@list:any(
_pipe@11,
fun(_capture) ->
gleam@string:starts_with(File@2, _capture)
end
)
end of
true ->
nil;
false ->
_assert_subject@2 = gleam_erlang_ffi:recursive_delete(
<<Lib_dir/binary, File@2/binary>>
),
{ok, nil} = case _assert_subject@2 of
{ok, nil} -> _assert_subject@2;
_assert_fail@2 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@2,
module => <<"mote"/utf8>>,
function => <<"apply_whitelists"/utf8>>,
line => 357})
end,
nil
end
end
),
nil;
_ ->
gleam@io:println(<<"no lib whitelist found, skipping"/utf8>>)
end.
-spec kill_inis(binary(), binary()) -> {ok, nil} |
{error, gleam@erlang@file:reason()}.
kill_inis(Runtime, Erts_dir) ->
_ = gleam_erlang_ffi:delete_file(<<Runtime/binary, "/bin/erl.ini"/utf8>>),
_ = gleam_erlang_ffi:delete_file(<<Erts_dir/binary, "/bin/erl.ini"/utf8>>).
-spec copy_rec(binary(), binary()) -> {ok, nil} |
{error, gleam@erlang@file:reason()}.
copy_rec(Source, Dest) ->
case gleam@erlang@file:is_directory(Source) of
{ok, true} ->
Source@1 = <<Source/binary, "/"/utf8>>,
_ = gleam_erlang_ffi:make_directory(Dest),
Dest@1 = <<Dest/binary, "/"/utf8>>,
gleam@result:'try'(
(gleam_erlang_ffi:list_directory(Source@1)),
fun(Files) ->
_pipe@1 = gleam@result:all(
begin
_pipe = Files,
gleam@list:map(
_pipe,
fun(Item) ->
copy_rec(
<<Source@1/binary, Item/binary>>,
<<Dest@1/binary, Item/binary>>
)
end
)
end
),
gleam@result:map(_pipe@1, fun(_) -> nil end)
end
);
{ok, false} ->
_pipe@2 = file:copy(Source, Dest),
gleam@result:map(_pipe@2, fun(_) -> nil end);
_ ->
{ok, nil}
end.
-spec walk_directory(binary(), fun((binary(), binary()) -> boolean())) -> nil.
walk_directory(Dir, For_each) ->
_pipe = Dir,
_pipe@1 = gleam_erlang_ffi:list_directory(_pipe),
_pipe@2 = gleam@result:unwrap(_pipe@1, []),
gleam@list:map(
_pipe@2,
fun(File) ->
Filepath = <<<<Dir/binary, "/"/utf8>>/binary, File/binary>>,
case gleam@erlang@file:is_directory(Filepath) of
{ok, true} ->
case For_each(File, Filepath) of
true ->
walk_directory(Filepath, For_each);
false ->
nil
end;
{ok, false} ->
For_each(File, Filepath),
nil;
_ ->
gleam@io:println_error(
<<<<<<<<<<"failed to walk file "/utf8, File/binary>>/binary,
" at path "/utf8>>/binary,
Dir/binary>>/binary,
"/"/utf8>>/binary,
File/binary>>
),
erlang:error(#{gleam_error => panic,
message => <<"panic expression evaluated"/utf8>>,
module => <<"mote"/utf8>>,
function => <<"walk_directory"/utf8>>,
line => 408})
end
end
),
nil.
-spec clean_libs(binary()) -> nil.
clean_libs(Runtime) ->
Lib_dir = <<Runtime/binary, "/lib"/utf8>>,
Blacklist = [<<"doc"/utf8>>,
<<"include"/utf8>>,
<<"src"/utf8>>,
<<"examples"/utf8>>],
walk_directory(
Lib_dir,
fun(File, Path) -> case gleam@list:contains(Blacklist, File) of
true ->
_assert_subject = gleam_erlang_ffi:recursive_delete(Path),
{ok, nil} = case _assert_subject of
{ok, nil} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"mote"/utf8>>,
function => <<"clean_libs"/utf8>>,
line => 378})
end,
false;
false ->
true
end end
).
-spec pack(
glint:command_input(),
gleam@map:map_(bitstring(), mote@toml:value())
) -> nil.
pack(Input, Config) ->
gleam@io:println(<<"mote packaging starting..."/utf8>>),
_assert_subject = glint@flag:get(
erlang:element(3, Input),
<<"runtime"/utf8>>
),
{ok, {s, Runtime}} = case _assert_subject of
{ok, {s, _}} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"mote"/utf8>>,
function => <<"pack"/utf8>>,
line => 46})
end,
case Runtime of
<<""/utf8>> ->
gleam@io:println_error(
<<"you must set a runtime to pack with"/utf8>>
),
erlang:error(#{gleam_error => panic,
message => <<"panic expression evaluated"/utf8>>,
module => <<"mote"/utf8>>,
function => <<"pack"/utf8>>,
line => 51});
_ ->
nil
end,
_assert_subject@1 = shellout:command(
<<"gleam"/utf8>>,
[<<"export"/utf8>>, <<"erlang-shipment"/utf8>>],
<<"."/utf8>>,
[]
),
{ok, _} = case _assert_subject@1 of
{ok, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@1,
module => <<"mote"/utf8>>,
function => <<"pack"/utf8>>,
line => 56})
end,
_assert_subject@2 = mote@toml:get_string(Config, [<<"name"/utf8>>]),
{ok, Project_name} = case _assert_subject@2 of
{ok, _} -> _assert_subject@2;
_assert_fail@2 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@2,
module => <<"mote"/utf8>>,
function => <<"pack"/utf8>>,
line => 64})
end,
Shipment_folder = <<"build/erlang-shipment"/utf8>>,
Pkg_dir = <<"build/mote_pkg"/utf8>>,
_ = gleam_erlang_ffi:recursive_delete(Pkg_dir),
_assert_subject@3 = copy_rec(Shipment_folder, Pkg_dir),
{ok, _} = case _assert_subject@3 of
{ok, _} -> _assert_subject@3;
_assert_fail@3 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@3,
module => <<"mote"/utf8>>,
function => <<"pack"/utf8>>,
line => 70})
end,
_assert_subject@4 = gleam_erlang_ffi:delete_file(
<<Pkg_dir/binary, "/entrypoint.sh"/utf8>>
),
{ok, _} = case _assert_subject@4 of
{ok, _} -> _assert_subject@4;
_assert_fail@4 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@4,
module => <<"mote"/utf8>>,
function => <<"pack"/utf8>>,
line => 71})
end,
Runtimes = case gleam_erlang_ffi:get_env(<<"GLEAM_MOTE_RUNTIMES"/utf8>>) of
{ok, R} ->
R;
_ ->
gleam@io:println_error(
<<"please set the `GLEAM_MOTE_RUNTIMES` environment variable to a directory containing your erlang runtimes to pack"/utf8>>
),
erlang:error(#{gleam_error => panic,
message => <<"panic expression evaluated"/utf8>>,
module => <<"mote"/utf8>>,
function => <<"pack"/utf8>>,
line => 79})
end,
Runtime_src = <<<<Runtimes/binary, "/"/utf8>>/binary, Runtime/binary>>,
case gleam@erlang@file:is_directory(Runtime_src) of
{ok, true} ->
nil;
_ ->
gleam@io:println_error(
<<Runtime_src/binary, " is not a valid runtime directory"/utf8>>
),
erlang:error(#{gleam_error => panic,
message => <<"panic expression evaluated"/utf8>>,
module => <<"mote"/utf8>>,
function => <<"pack"/utf8>>,
line => 88})
end,
_assert_subject@5 = gleam_erlang_ffi:list_directory(Pkg_dir),
{ok, Modules} = case _assert_subject@5 of
{ok, _} -> _assert_subject@5;
_assert_fail@5 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@5,
module => <<"mote"/utf8>>,
function => <<"pack"/utf8>>,
line => 92})
end,
Runtime@1 = <<Pkg_dir/binary, "/minimal_erl"/utf8>>,
_assert_subject@6 = copy_rec(Runtime_src, Runtime@1),
{ok, nil} = case _assert_subject@6 of
{ok, nil} -> _assert_subject@6;
_assert_fail@6 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@6,
module => <<"mote"/utf8>>,
function => <<"pack"/utf8>>,
line => 95})
end,
_assert_subject@7 = gleam_erlang_ffi:list_directory(Runtime@1),
{ok, Runtime_dir} = case _assert_subject@7 of
{ok, _} -> _assert_subject@7;
_assert_fail@7 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@7,
module => <<"mote"/utf8>>,
function => <<"pack"/utf8>>,
line => 97})
end,
_assert_subject@8 = begin
_pipe = Runtime_dir,
gleam@list:find(
_pipe,
fun(_capture) ->
gleam@string:starts_with(_capture, <<"erts"/utf8>>)
end
)
end,
{ok, Erts_dir} = case _assert_subject@8 of
{ok, _} -> _assert_subject@8;
_assert_fail@8 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@8,
module => <<"mote"/utf8>>,
function => <<"pack"/utf8>>,
line => 99})
end,
Erts_dir@1 = <<<<Runtime@1/binary, "/"/utf8>>/binary, Erts_dir/binary>>,
_pipe@1 = Runtime_dir,
gleam@list:map(
_pipe@1,
fun(Item) ->
case ((begin
_pipe@2 = Item,
gleam@string:ends_with(_pipe@2, <<".exe"/utf8>>)
end
orelse begin
_pipe@3 = Item,
gleam@string:ends_with(_pipe@3, <<".ini"/utf8>>)
end)
orelse begin
_pipe@4 = Item,
gleam@string:ends_with(_pipe@4, <<".md"/utf8>>)
end)
orelse begin
_pipe@5 = Item,
gleam@string:ends_with(_pipe@5, <<".template"/utf8>>)
end of
true ->
_pipe@6 = gleam_erlang_ffi:delete_file(
<<<<Runtime@1/binary, "/"/utf8>>/binary, Item/binary>>
),
gleam@result:unwrap(_pipe@6, nil);
false ->
nil
end
end
),
_ = gleam_erlang_ffi:recursive_delete(<<Runtime@1/binary, "/doc"/utf8>>),
_ = gleam_erlang_ffi:recursive_delete(
<<Runtime@1/binary, "/releases"/utf8>>
),
_ = gleam_erlang_ffi:recursive_delete(<<Runtime@1/binary, "/usr"/utf8>>),
_pipe@7 = [<<"doc"/utf8>>,
<<"include"/utf8>>,
<<"man"/utf8>>,
<<"src"/utf8>>,
<<"info"/utf8>>],
gleam@list:map(
_pipe@7,
fun(Dir) ->
_ = gleam_erlang_ffi:recursive_delete(
<<<<Erts_dir@1/binary, "/"/utf8>>/binary, Dir/binary>>
)
end
),
Module_include = begin
_pipe@8 = Modules,
_pipe@9 = gleam@list:map(
_pipe@8,
fun(Mod) ->
<<<<"\t\tunicode:characters_to_list([RootPath, \""/utf8,
Mod/binary>>/binary,
"/ebin\"])"/utf8>>
end
),
gleam@string:join(_pipe@9, <<",\n"/utf8>>)
end,
_assert_subject@9 = gleam@erlang@file:write(
<<<<<<<<<<<<<<<<<<<<"\n-module(launch).\n"/utf8,
"-mode(compile).\n"/utf8>>/binary,
"main(_) ->\n"/utf8>>/binary,
"\tErlPath = code:root_dir(),"/utf8>>/binary,
"\tTrimLength = string:length(\"minimal_erl\"),"/utf8>>/binary,
"\tRootPath = string:reverse(string:slice(string:reverse(ErlPath), TrimLength)),"/utf8>>/binary,
"\tcode:add_paths([\n"/utf8>>/binary,
Module_include/binary>>/binary,
"\n\t]),\n\t"/utf8>>/binary,
Project_name/binary>>/binary,
":main()."/utf8>>,
<<Runtime@1/binary, "/bin/launch.escript"/utf8>>
),
{ok, nil} = case _assert_subject@9 of
{ok, nil} -> _assert_subject@9;
_assert_fail@9 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@9,
module => <<"mote"/utf8>>,
function => <<"pack"/utf8>>,
line => 136})
end,
_assert_subject@10 = file:copy(
<<Runtime@1/binary, "/bin/escript.exe"/utf8>>,
<<Runtime@1/binary, "/bin/launch.exe"/utf8>>
),
{ok, _} = case _assert_subject@10 of
{ok, _} -> _assert_subject@10;
_assert_fail@10 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@10,
module => <<"mote"/utf8>>,
function => <<"pack"/utf8>>,
line => 142})
end,
_ = kill_inis(Runtime@1, Erts_dir@1),
apply_whitelists(Runtime@1, Erts_dir@1, Config),
clean_libs(Runtime@1),
_assert_subject@11 = glint@flag:get(
erlang:element(3, Input),
<<"target"/utf8>>
),
{ok, {s, Target}} = case _assert_subject@11 of
{ok, {s, _}} -> _assert_subject@11;
_assert_fail@11 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@11,
module => <<"mote"/utf8>>,
function => <<"pack"/utf8>>,
line => 151})
end,
case Target of
<<"windows-x64"/utf8>> ->
nil;
<<"macos-x64"/utf8>> ->
nil;
<<"linux-x64"/utf8>> ->
nil;
_ ->
gleam@io:println_error(
<<<<"invalid target `"/utf8, Target/binary>>/binary, "`"/utf8>>
),
erlang:error(#{gleam_error => panic,
message => <<"panic expression evaluated"/utf8>>,
module => <<"mote"/utf8>>,
function => <<"pack"/utf8>>,
line => 156})
end,
launch_warp(Project_name, Pkg_dir, Target),
gleam@io:println(<<"mote packaging completed."/utf8>>).
-spec main() -> nil.
main() ->
Config = get_project_config(),
Target_default = case gleam_erlang_ffi:os_family() of
windows_nt ->
<<"windows-x64"/utf8>>;
darwin ->
<<"macos-x64"/utf8>>;
linux ->
<<"linux-x64"/utf8>>;
_ ->
<<"linux-x64"/utf8>>
end,
_pipe = glint:new(),
_pipe@1 = glint:add_command(
_pipe,
[],
fun(_capture) -> pack(_capture, Config) end,
[glint@flag:string(
<<"runtime"/utf8>>,
<<""/utf8>>,
<<"Decide which runtime to pack with"/utf8>>
),
glint@flag:string(
<<"target"/utf8>>,
Target_default,
<<"Decide with target to pack for"/utf8>>
)],
<<"Packs your project into an executable"/utf8>>
),
glint:run(_pipe@1, gleam@erlang:start_arguments()).