Packages

CLI tool to package a Gleam app as a FreeBSD package, with a service script.

Current section

Files

Jump to
glm_freebsd src glm_freebsd@packager.erl
Raw

src/glm_freebsd@packager.erl

-module(glm_freebsd@packager).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glm_freebsd/packager.gleam").
-export([gen_staging/6, get_string/2, get_strings/2, get_optional_strings/3, get_optional_string/3, new_config/1, load_toml/1, run/4, copy_raw_templates/1]).
-export_type([app_error/0, dependency_config/0, config_pair/0, plist_line/0, config/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(
" BFS - map function across entries first\n"
" BFS - now run the search\n"
).
-type app_error() :: {app_error, binary()} |
{unable_to_read_toml_file, simplifile:file_error()} |
{unable_to_parse_toml_text, tom:parse_error()} |
{key_not_found_in_toml, list(binary()), list(binary())} |
{unexpected_toml_type, tom:toml()} |
{unable_to_create_staging_dir, simplifile:file_error()} |
{unable_to_get_templates, simplifile:file_error()} |
{unable_to_read_template, binary(), simplifile:file_error()} |
{unable_to_prepare_template, binary(), handles@error:tokenizer_error()} |
{unable_to_project_template,
binary(),
binary(),
handles@error:runtime_error()} |
{unable_to_create_project_template_dir,
binary(),
binary(),
simplifile:file_error()} |
{unable_to_create_output_dir, simplifile:file_error()} |
{unable_to_append_plist_file, binary(), binary(), simplifile:file_error()} |
{unable_to_build_package, {integer(), binary()}} |
{unexpected_plist_line_type, binary()} |
{unable_to_get_plist_files, binary(), binary(), simplifile:file_error()} |
{unable_to_convert_to_string, tom:get_error()} |
{unable_to_compile_partial_template, handles@error:tokenizer_error()} |
{unable_to_copy_plist_directory,
binary(),
binary(),
binary(),
simplifile:file_error()} |
{unable_to_copy_templates_to_directory, binary(), simplifile:file_error()} |
{unable_to_get_templates_from_directory, binary(), simplifile:file_error()} |
{unable_to_write_projected_template,
binary(),
binary(),
simplifile:file_error()} |
{unable_to_copy_plist_file, binary(), binary(), simplifile:file_error()} |
{unable_to_create_metadat_dir, simplifile:file_error()}.
-type dependency_config() :: {dependency_config, binary(), binary(), binary()}.
-type config_pair() :: {config_pair, binary(), binary()}.
-type plist_line() :: {plist_file,
binary(),
binary(),
binary(),
binary(),
binary()} |
{plist_dir_directive, binary()} |
{plist_include_directive, binary()} |
{plist_directory, binary(), binary(), binary(), binary(), binary()}.
-type config() :: {config,
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
list(dependency_config()),
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
list(plist_line()),
binary(),
binary(),
binary(),
binary(),
binary(),
list(binary()),
list(config_pair())}.
-file("src/glm_freebsd/packager.gleam", 1027).
?DOC(" Invoke the FreeBSD 'pkg create' command via a shell. This creates the package in output_dir.\n").
-spec build_package(binary(), binary(), binary()) -> {ok, binary()} |
{error, app_error()}.
build_package(Metadata_dir, Staging_dir, Output_dir) ->
Args = [<<"create"/utf8>>,
<<"-m"/utf8>>,
Metadata_dir,
<<"-r"/utf8>>,
Staging_dir,
<<"-p"/utf8>>,
<<Metadata_dir/binary, "/pkg-plist"/utf8>>,
<<"-o"/utf8>>,
Output_dir],
gleam_stdlib:println(
<<"command: pkg "/utf8,
(begin
_pipe = Args,
gleam@string:join(_pipe, <<" "/utf8>>)
end)/binary>>
),
_pipe@1 = shellout:command(<<"pkg"/utf8>>, Args, <<"."/utf8>>, []),
gleam@result:map_error(
_pipe@1,
fun(Field@0) -> {unable_to_build_package, Field@0} end
).
-file("src/glm_freebsd/packager.gleam", 683).
?DOC(
" Recurse src directory and process all the files in that tree, copying the dir to staging, and\n"
" appending entries to the plist file.\n"
).
-spec process_plist_directory(
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary()
) -> {ok, binary()} | {error, app_error()}.
process_plist_directory(
App_dir,
Staging_dir,
Plist_file,
Src_dir,
Dest_dir,
Mode,
Owner,
Group
) ->
Header = <<<<<<<<<<<<<<<<"@mode "/utf8, Mode/binary>>/binary, "\n"/utf8>>/binary,
"@owner "/utf8>>/binary,
Owner/binary>>/binary,
"\n"/utf8>>/binary,
"@group "/utf8>>/binary,
Group/binary>>/binary,
"\n"/utf8>>,
_pipe = simplifile:append(Plist_file, Header),
_pipe@1 = gleam@result:map_error(
_pipe,
fun(_capture) ->
{unable_to_append_plist_file, Plist_file, Header, _capture}
end
),
gleam@result:'try'(
_pipe@1,
fun(_) ->
Source_dir_path = case filepath:is_absolute(Src_dir) of
false ->
filepath:join(App_dir, Src_dir);
true ->
Src_dir
end,
_pipe@2 = simplifile:get_files(Source_dir_path),
_pipe@3 = gleam@result:map_error(
_pipe@2,
fun(_capture@1) ->
{unable_to_get_plist_files,
Plist_file,
Source_dir_path,
_capture@1}
end
),
_pipe@10 = gleam@result:map(
_pipe@3,
fun(Files) ->
Start_len = string:length(Source_dir_path),
Raw_files = begin
_pipe@4 = Files,
gleam@list:map(
_pipe@4,
fun(_capture@2) ->
gleam@string:drop_start(_capture@2, Start_len)
end
)
end,
Plist_files = begin
_pipe@5 = Raw_files,
gleam@list:map(
_pipe@5,
fun(_capture@3) ->
filepath:join(Dest_dir, _capture@3)
end
)
end,
Plist_file_list = <<(gleam@string:join(
Plist_files,
<<"\n"/utf8>>
))/binary,
"\n"/utf8>>,
Staging_target_dir = filepath:join(Staging_dir, Dest_dir),
_pipe@6 = simplifile:append(Plist_file, Plist_file_list),
_pipe@7 = gleam@result:map_error(
_pipe@6,
fun(_capture@4) ->
{unable_to_append_plist_file,
Plist_file,
Plist_file_list,
_capture@4}
end
),
gleam@result:'try'(
_pipe@7,
fun(_) ->
_pipe@8 = simplifile:copy_directory(
Source_dir_path,
Staging_target_dir
),
_pipe@9 = gleam@result:map_error(
_pipe@8,
fun(_capture@5) ->
{unable_to_copy_plist_directory,
Plist_file,
Source_dir_path,
Staging_target_dir,
_capture@5}
end
),
gleam@result:map(
_pipe@9,
fun(_) -> Plist_file_list end
)
end
)
end
),
gleam@result:flatten(_pipe@10)
end
).
-file("src/glm_freebsd/packager.gleam", 618).
?DOC(" Copy the plist file to the staging directory, and append an entry to the plist file.\n").
-spec process_plist_file_directive(
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary()
) -> {ok, binary()} | {error, app_error()}.
process_plist_file_directive(
App_dir,
Staging_dir,
Plist_file,
Src,
Dest,
Mode,
Owner,
Group
) ->
Target_path = filepath:join(Staging_dir, Dest),
Target_path_dir = filepath:directory_name(Target_path),
_ = case simplifile:create_directory_all(Target_path_dir) of
{error, eexist} ->
{ok, nil};
{ok, _} ->
{ok, nil};
{error, E} ->
gleam_stdlib:println_error(
begin
_pipe = E,
gleam@string:inspect(_pipe)
end
),
erlang:error(#{gleam_error => panic,
message => <<"`panic` expression evaluated."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"glm_freebsd/packager"/utf8>>,
function => <<"process_plist_file_directive"/utf8>>,
line => 639})
end,
Source_file_path = case filepath:is_absolute(Src) of
true ->
Src;
false ->
filepath:join(App_dir, Src)
end,
_ = case simplifile_erl:is_file(Source_file_path) of
{ok, true} ->
{ok, nil};
{ok, false} ->
gleam_stdlib:println_error(
<<"missing source_file: "/utf8, Source_file_path/binary>>
),
erlang:error(#{gleam_error => panic,
message => <<"`panic` expression evaluated."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"glm_freebsd/packager"/utf8>>,
function => <<"process_plist_file_directive"/utf8>>,
line => 652});
{error, E@1} ->
gleam_stdlib:println_error(
begin
_pipe@1 = E@1,
gleam@string:inspect(_pipe@1)
end
),
erlang:error(#{gleam_error => panic,
message => <<"`panic` expression evaluated."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"glm_freebsd/packager"/utf8>>,
function => <<"process_plist_file_directive"/utf8>>,
line => 656})
end,
_ = begin
_pipe@2 = simplifile:copy_file(Source_file_path, Target_path),
_pipe@3 = gleam@result:map_error(
_pipe@2,
fun(_capture) ->
{unable_to_copy_plist_file,
Source_file_path,
Target_path,
_capture}
end
),
gleam@result:'try'(
_pipe@3,
fun(_) ->
Lines = <<<<<<<<<<<<<<<<<<<<"@mode "/utf8, Mode/binary>>/binary,
"\n"/utf8>>/binary,
"@owner "/utf8>>/binary,
Owner/binary>>/binary,
"\n"/utf8>>/binary,
"@group "/utf8>>/binary,
Group/binary>>/binary,
"\n"/utf8>>/binary,
Dest/binary>>/binary,
"\n"/utf8>>,
_pipe@4 = simplifile:append(Plist_file, Lines),
_pipe@5 = gleam@result:map_error(
_pipe@4,
fun(_capture@1) ->
{unable_to_append_plist_file,
Plist_file,
Lines,
_capture@1}
end
),
gleam@result:map(_pipe@5, fun(_) -> Lines end)
end
)
end.
-file("src/glm_freebsd/packager.gleam", 610).
?DOC(" Write the plist dir directive to the plist file.\n").
-spec process_plist_dir_directive(binary(), binary()) -> {ok, binary()} |
{error, app_error()}.
process_plist_dir_directive(Plist_file, Path) ->
Line = <<<<"@dir "/utf8, Path/binary>>/binary, "\n"/utf8>>,
_pipe = simplifile:append(Plist_file, Line),
_pipe@1 = gleam@result:map_error(
_pipe,
fun(_capture) ->
{unable_to_append_plist_file, Plist_file, Line, _capture}
end
),
gleam@result:map(_pipe@1, fun(_) -> Line end).
-file("src/glm_freebsd/packager.gleam", 602).
?DOC(" Write the plist include directive to the plist file.\n").
-spec process_plist_include_directive(binary(), binary()) -> {ok, binary()} |
{error, app_error()}.
process_plist_include_directive(Plist_file, Path) ->
Line = <<<<"@include "/utf8, Path/binary>>/binary, "\n"/utf8>>,
_pipe = simplifile:append(Plist_file, Line),
_pipe@1 = gleam@result:map_error(
_pipe,
fun(_capture) ->
{unable_to_append_plist_file, Plist_file, Line, _capture}
end
),
gleam@result:map(_pipe@1, fun(_) -> Line end).
-file("src/glm_freebsd/packager.gleam", 741).
?DOC(" Copy plist files into the staging dir.\n").
-spec copy_plist_files(config(), binary(), binary(), binary()) -> {ok,
list(binary())} |
{error, app_error()}.
copy_plist_files(Cfg, App_dir, Metadata_dir, Staging_dir) ->
Plist_file = filepath:join(Metadata_dir, <<"pkg-plist"/utf8>>),
_pipe = erlang:element(18, Cfg),
_pipe@1 = gleam@list:map(_pipe, fun(Plist_line) -> case Plist_line of
{plist_include_directive, Path} ->
process_plist_include_directive(Plist_file, Path);
{plist_dir_directive, Path@1} ->
process_plist_dir_directive(Plist_file, Path@1);
{plist_file, Src, Dest, Mode, Owner, Group} ->
process_plist_file_directive(
App_dir,
Staging_dir,
Plist_file,
Src,
Dest,
Mode,
Owner,
Group
);
{plist_directory, Src_dir, Dest_dir, Mode@1, Owner@1, Group@1} ->
process_plist_directory(
App_dir,
Staging_dir,
Plist_file,
Src_dir,
Dest_dir,
Mode@1,
Owner@1,
Group@1
)
end end),
gleam@result:all(_pipe@1).
-file("src/glm_freebsd/packager.gleam", 824).
?DOC(
" Add the erlang shipment directory to the config plist. This will cause this directory\n"
" to be copied to staging and entries appended to the plist file.\n"
).
-spec update_config_with_erlang_shipment_files(config(), binary()) -> {ok,
config()} |
{error, app_error()}.
update_config_with_erlang_shipment_files(Cfg, Erlang_shipment_dir) ->
Target_deployment_dir = filepath:join(
filepath:join(erlang:element(14, Cfg), <<"libexec"/utf8>>),
erlang:element(2, Cfg)
),
Plist_dir = {plist_directory,
Erlang_shipment_dir,
Target_deployment_dir,
<<"0554"/utf8>>,
erlang:element(4, Cfg),
<<"wheel"/utf8>>},
Updated_pkg_plist_lines = begin
_pipe = erlang:element(18, Cfg),
gleam@list:prepend(_pipe, Plist_dir)
end,
{ok,
{config,
erlang:element(2, Cfg),
erlang:element(3, Cfg),
erlang:element(4, Cfg),
erlang:element(5, Cfg),
erlang:element(6, Cfg),
erlang:element(7, Cfg),
erlang:element(8, Cfg),
erlang:element(9, Cfg),
erlang:element(10, Cfg),
erlang:element(11, Cfg),
erlang:element(12, Cfg),
erlang:element(13, Cfg),
erlang:element(14, Cfg),
erlang:element(15, Cfg),
erlang:element(16, Cfg),
erlang:element(17, Cfg),
Updated_pkg_plist_lines,
erlang:element(19, Cfg),
erlang:element(20, Cfg),
erlang:element(21, Cfg),
erlang:element(22, Cfg),
erlang:element(23, Cfg),
erlang:element(24, Cfg),
erlang:element(25, Cfg)}}.
-file("src/glm_freebsd/packager.gleam", 845).
?DOC(
" Add the FreeBSD service files to the config plist. This will cause the\n"
" /etc/rc.d/APP_NAME and /etc/rc.conf.d/APP_NAME service files to be copied to staging\n"
" and appended to the plist file.\n"
).
-spec update_config_with_rc_files(config(), binary()) -> {ok, config()} |
{error, app_error()}.
update_config_with_rc_files(Cfg, Metadata_dir) ->
Rc_conf_file = {plist_file,
filepath:join(Metadata_dir, <<"rc_conf"/utf8>>),
filepath:join(<<"/etc/rc.conf.d/"/utf8>>, erlang:element(2, Cfg)),
<<"0555"/utf8>>,
<<"root"/utf8>>,
<<"wheel"/utf8>>},
Rc_file = {plist_file,
filepath:join(Metadata_dir, <<"rc"/utf8>>),
filepath:join(<<"/etc/rc.d/"/utf8>>, erlang:element(2, Cfg)),
<<"0555"/utf8>>,
<<"root"/utf8>>,
<<"wheel"/utf8>>},
Updated_pkg_plist_lines = begin
_pipe = erlang:element(18, Cfg),
_pipe@1 = gleam@list:prepend(_pipe, Rc_conf_file),
gleam@list:prepend(_pipe@1, Rc_file)
end,
{ok,
{config,
erlang:element(2, Cfg),
erlang:element(3, Cfg),
erlang:element(4, Cfg),
erlang:element(5, Cfg),
erlang:element(6, Cfg),
erlang:element(7, Cfg),
erlang:element(8, Cfg),
erlang:element(9, Cfg),
erlang:element(10, Cfg),
erlang:element(11, Cfg),
erlang:element(12, Cfg),
erlang:element(13, Cfg),
erlang:element(14, Cfg),
erlang:element(15, Cfg),
erlang:element(16, Cfg),
erlang:element(17, Cfg),
Updated_pkg_plist_lines,
erlang:element(19, Cfg),
erlang:element(20, Cfg),
erlang:element(21, Cfg),
erlang:element(22, Cfg),
erlang:element(23, Cfg),
erlang:element(24, Cfg),
erlang:element(25, Cfg)}}.
-file("src/glm_freebsd/packager.gleam", 1012).
?DOC(
" Return the staging path for a reified template. This only works for templates.\n"
"\n"
" Example:\n"
" template_path: /a/b/c/foo.bar.template, staging_dir: /bing/baz\n"
" -> projected_template_path = /bing/baz/foo.bar\n"
).
-spec new_projected_template_path(binary(), binary()) -> binary().
new_projected_template_path(Template_path, Staging_dir) ->
_pipe = Template_path,
_pipe@1 = filepath:base_name(_pipe),
_pipe@2 = gleam@string:split(_pipe@1, <<"."/utf8>>),
_pipe@3 = lists:reverse(_pipe@2),
_pipe@4 = gleam@list:drop(_pipe@3, 1),
_pipe@5 = lists:reverse(_pipe@4),
_pipe@6 = gleam@string:join(_pipe@5, <<"."/utf8>>),
filepath:join(Staging_dir, _pipe@6).
-file("src/glm_freebsd/packager.gleam", 939).
?DOC(" Reify a template and a context and save as a file in the staging directory.\n").
-spec project_template(
binary(),
handles@ctx:value(),
binary(),
list({binary(), handles:template()})
) -> {ok, binary()} | {error, app_error()}.
project_template(Template_path, Context, Staging_dir, Partial_templates) ->
case simplifile:read(Template_path) of
{error, E} ->
{error, {unable_to_read_template, Template_path, E}};
{ok, Template_text} ->
case handles:prepare(Template_text) of
{error, E@1} ->
{error, {unable_to_prepare_template, Template_path, E@1}};
{ok, Template} ->
Projected_template_path = new_projected_template_path(
Template_path,
Staging_dir
),
Projected_template_dir_path = filepath:directory_name(
Projected_template_path
),
_ = case simplifile_erl:is_directory(
Projected_template_dir_path
) of
{error, E@2} ->
{error,
{unable_to_create_project_template_dir,
Template_path,
Projected_template_dir_path,
E@2}};
{ok, false} ->
_pipe = simplifile:create_directory_all(
Projected_template_dir_path
),
gleam@result:map_error(
_pipe,
fun(_capture) ->
{unable_to_create_project_template_dir,
Template_path,
Projected_template_dir_path,
_capture}
end
);
{ok, true} ->
{ok, nil}
end,
case handles:run(Template, Context, Partial_templates) of
{error, E@3} ->
{error,
{unable_to_project_template,
Template_path,
Projected_template_path,
E@3}};
{ok, Projected_text} ->
case simplifile:write(
Projected_template_path,
begin
_pipe@1 = Projected_text,
unicode:characters_to_binary(_pipe@1)
end
) of
{error, E@4} ->
{error,
{unable_to_write_projected_template,
Template_path,
Projected_template_path,
E@4}};
{ok, _} ->
{ok, Projected_template_path}
end
end
end
end.
-file("src/glm_freebsd/packager.gleam", 930).
?DOC(" Return a dependency config as a handles ctx value.\n").
-spec dependency_from_config(dependency_config()) -> handles@ctx:value().
dependency_from_config(Dep) ->
{dict,
[{prop, <<"dep_name"/utf8>>, {str, erlang:element(2, Dep)}},
{prop, <<"dep_version"/utf8>>, {str, erlang:element(3, Dep)}},
{prop, <<"dep_origin"/utf8>>, {str, erlang:element(4, Dep)}}]}.
-file("src/glm_freebsd/packager.gleam", 922).
?DOC(" Return a list of dependency configs as handles ctx values.\n").
-spec dependency_list_from_config(list(dependency_config())) -> handles@ctx:value().
dependency_list_from_config(Deps) ->
_pipe = Deps,
_pipe@1 = gleam@list:map(_pipe, fun dependency_from_config/1),
_pipe@2 = gleam@list:map(
_pipe@1,
fun(Dep) -> {dict, [{prop, <<"dep"/utf8>>, Dep}]} end
),
{list, _pipe@2}.
-file("src/glm_freebsd/packager.gleam", 874).
?DOC(" Push all the vars from the config into the handler context.\n").
-spec new_context(config()) -> handles@ctx:value().
new_context(Config) ->
Pairs_prop_list = begin
_pipe = erlang:element(25, Config),
gleam@list:map(
_pipe,
fun(P) ->
{prop,
<<"pair_"/utf8, (erlang:element(2, P))/binary>>,
{str, erlang:element(3, P)}}
end
)
end,
{dict,
lists:append(
[{prop, <<"app_name"/utf8>>, {str, erlang:element(2, Config)}},
{prop, <<"app_version"/utf8>>, {str, erlang:element(3, Config)}},
{prop,
<<"pkg_command_args"/utf8>>,
{str, erlang:element(16, Config)}},
{prop,
<<"pkg_command"/utf8>>,
{str, erlang:element(15, Config)}},
{prop, <<"pkg_arch"/utf8>>, {str, erlang:element(21, Config)}},
{prop, <<"pkg_www"/utf8>>, {str, erlang:element(22, Config)}},
{prop,
<<"pkg_license_logic"/utf8>>,
{str, erlang:element(23, Config)}},
{prop,
<<"pkg_licenses"/utf8>>,
begin
_pipe@1 = erlang:element(24, Config),
_pipe@2 = gleam@list:map(
_pipe@1,
fun(X) ->
<<<<"\""/utf8, X/binary>>/binary, "\""/utf8>>
end
),
_pipe@3 = gleam@string:join(_pipe@2, <<","/utf8>>),
{str, _pipe@3}
end},
{prop,
<<"pkg_comment"/utf8>>,
{str, erlang:element(20, Config)}},
{prop,
<<"pkg_app_name_uppercase"/utf8>>,
{str,
begin
_pipe@4 = erlang:element(2, Config),
string:uppercase(_pipe@4)
end}},
{prop,
<<"pkg_config_dir"/utf8>>,
{str, erlang:element(10, Config)}},
{prop,
<<"pkg_daemon_flags"/utf8>>,
{str, erlang:element(17, Config)}},
{prop,
<<"pkg_dependencies"/utf8>>,
dependency_list_from_config(erlang:element(8, Config))},
{prop,
<<"pkg_description"/utf8>>,
{str, erlang:element(6, Config)}},
{prop,
<<"pkg_env_file"/utf8>>,
{str, erlang:element(13, Config)}},
{prop,
<<"pkg_maintainer"/utf8>>,
{str, erlang:element(7, Config)}},
{prop, <<"pkg_origin"/utf8>>, {str, erlang:element(19, Config)}},
{prop,
<<"pkg_path_extensions"/utf8>>,
{str, erlang:element(11, Config)}},
{prop, <<"pkg_prefix"/utf8>>, {str, erlang:element(14, Config)}},
{prop,
<<"pkg_proc_name"/utf8>>,
{str, erlang:element(9, Config)}},
{prop,
<<"pkg_user_name"/utf8>>,
{str, erlang:element(4, Config)}},
{prop,
<<"pkg_user_uid"/utf8>>,
{str, erlang:element(5, Config)}},
{prop,
<<"pkg_var_dir"/utf8>>,
{str, erlang:element(12, Config)}}],
Pairs_prop_list
)}.
-file("src/glm_freebsd/packager.gleam", 576).
?DOC(" Generate the templates from the source templates and the config instance, and write into the metadata dir.\n").
-spec gen_templates(config(), binary(), binary()) -> {ok, config()} |
{error, app_error()}.
gen_templates(Cfg, Templates_dir, Metadata_dir) ->
Context = new_context(Cfg),
gleam@result:'try'(
begin
_pipe = handles:prepare(
<<" {{ dep_name }}: {origin: \"{{ dep_origin }}\", version: \"{{ dep_version }}\"}"/utf8>>
),
gleam@result:map_error(
_pipe,
fun(Field@0) -> {unable_to_compile_partial_template, Field@0} end
)
end,
fun(Dep_template) ->
Partial_templates = [{<<"dependency"/utf8>>, Dep_template}],
_pipe@1 = Templates_dir,
_pipe@2 = simplifile:get_files(_pipe@1),
_pipe@3 = gleam@result:map_error(
_pipe@2,
fun(Field@0) -> {unable_to_get_templates, Field@0} end
),
_pipe@6 = gleam@result:'try'(
_pipe@3,
fun(Templates) -> _pipe@4 = Templates,
_pipe@5 = gleam@list:map(
_pipe@4,
fun(_capture) ->
project_template(
_capture,
Context,
Metadata_dir,
Partial_templates
)
end
),
gleam@result:all(_pipe@5) end
),
gleam@result:map(_pipe@6, fun(_) -> Cfg end)
end
).
-file("src/glm_freebsd/packager.gleam", 802).
?DOC(
" Generate the files in the staging directory. The files in the staging directory will be packaged into\n"
" the final FreeBSD package.\n"
"\n"
" These files are:\n"
" 1. +MANIFEST: the top level file that describes this FreeBSD Package\n"
" 2. +[FILES]: +POST_INSTALL and +PRE_DEINSTALL scripts to execute as part of installing or removing package\n"
" 3. plist: a list of all the files to be copied to the target server as part of installation\n"
" 4. [FILES]: the files (listed in the `plist` above) to be copied to the target server\n"
).
-spec gen_staging(config(), binary(), binary(), binary(), binary(), binary()) -> {ok,
nil} |
{error, app_error()}.
gen_staging(
Config,
App_dir,
Metadata_dir,
Staging_dir,
Templates_dir,
Erlang_shipment_dir
) ->
_pipe = Config,
_pipe@1 = gen_templates(_pipe, Templates_dir, Metadata_dir),
_pipe@2 = gleam@result:'try'(
_pipe@1,
fun(Cfg) -> update_config_with_rc_files(Cfg, Metadata_dir) end
),
_pipe@3 = gleam@result:'try'(
_pipe@2,
fun(Cfg@1) ->
update_config_with_erlang_shipment_files(Cfg@1, Erlang_shipment_dir)
end
),
_pipe@4 = gleam@result:'try'(
_pipe@3,
fun(Cfg@2) ->
copy_plist_files(Cfg@2, App_dir, Metadata_dir, Staging_dir)
end
),
gleam@result:map(_pipe@4, fun(_) -> nil end).
-file("src/glm_freebsd/packager.gleam", 511).
?DOC(" Extract a string from a toml dict.\n").
-spec get_string(gleam@dict:dict(binary(), tom:toml()), binary()) -> {ok,
binary()} |
{error, app_error()}.
get_string(Toml, Key) ->
Key@1 = gleam@string:split(Key, <<"."/utf8>>),
_pipe = tom:get_string(Toml, Key@1),
gleam@result:map_error(
_pipe,
fun(_) -> {key_not_found_in_toml, Key@1, maps:keys(Toml)} end
).
-file("src/glm_freebsd/packager.gleam", 495).
?DOC(" Extract a list of elements from a toml dict.\n").
-spec new_toml_element_list(
gleam@dict:dict(binary(), tom:toml()),
list(binary()),
fun((tom:toml()) -> {ok, IZZ} | {error, app_error()})
) -> {ok, list(IZZ)} | {error, app_error()}.
new_toml_element_list(D, Key, Func) ->
case tom:get_array(D, Key) of
{error, _} ->
{ok, []};
{ok, Toml_list} ->
_pipe = Toml_list,
_pipe@1 = gleam@list:map(_pipe, Func),
gleam@result:all(_pipe@1)
end.
-file("src/glm_freebsd/packager.gleam", 537).
?DOC(" Extract list of config pairs from a toml dict.\n").
-spec get_pairs(gleam@dict:dict(binary(), tom:toml())) -> {ok,
list(config_pair())} |
{error, app_error()}.
get_pairs(D) ->
Key = begin
_pipe = <<"freebsd.pairs"/utf8>>,
gleam@string:split(_pipe, <<"."/utf8>>)
end,
new_toml_element_list(D, Key, fun(Toml) -> case Toml of
{table, T} ->
gleam@result:'try'(
get_string(T, <<"key"/utf8>>),
fun(Key@1) ->
gleam@result:'try'(
get_string(T, <<"value"/utf8>>),
fun(Value) ->
_pipe@1 = {config_pair, Key@1, Value},
{ok, _pipe@1}
end
)
end
);
T@1 ->
{error, {unexpected_toml_type, T@1}}
end end).
-file("src/glm_freebsd/packager.gleam", 521).
?DOC(" Extract list of strings from a toml dict.\n").
-spec get_strings(gleam@dict:dict(binary(), tom:toml()), binary()) -> {ok,
list(binary())} |
{error, app_error()}.
get_strings(Toml, Key) ->
Key@1 = gleam@string:split(Key, <<"."/utf8>>),
_pipe = tom:get_array(Toml, Key@1),
_pipe@1 = gleam@result:map_error(
_pipe,
fun(_) -> {key_not_found_in_toml, Key@1, maps:keys(Toml)} end
),
gleam@result:'try'(_pipe@1, fun(Data) -> _pipe@2 = Data,
_pipe@3 = gleam@list:map(_pipe@2, fun tom:as_string/1),
_pipe@4 = gleam@result:all(_pipe@3),
gleam@result:map_error(
_pipe@4,
fun(Field@0) -> {unable_to_convert_to_string, Field@0} end
) end).
-file("src/glm_freebsd/packager.gleam", 564).
?DOC(" Extract a list of strings from a toml dict, use the default value if the key is missing.\n").
-spec get_optional_strings(
gleam@dict:dict(binary(), tom:toml()),
binary(),
list(binary())
) -> {ok, list(binary())} | {error, app_error()}.
get_optional_strings(Toml, Key, Default) ->
case get_strings(Toml, Key) of
{error, _} ->
{ok, Default};
{ok, S} ->
{ok, S}
end.
-file("src/glm_freebsd/packager.gleam", 552).
?DOC(" Extract a string from a toml dict, use the default value if the key is missing.\n").
-spec get_optional_string(
gleam@dict:dict(binary(), tom:toml()),
binary(),
binary()
) -> {ok, binary()} | {error, app_error()}.
get_optional_string(Toml, Key, Default) ->
case get_string(Toml, Key) of
{error, _} ->
{ok, Default};
{ok, S} ->
{ok, S}
end.
-file("src/glm_freebsd/packager.gleam", 433).
?DOC(" Return a list of PlistLines from the toml dict.\n").
-spec new_pkg_plist_lines(gleam@dict:dict(binary(), tom:toml())) -> {ok,
list(plist_line())} |
{error, app_error()}.
new_pkg_plist_lines(D) ->
Key = begin
_pipe = <<"freebsd.pkg_plist_lines"/utf8>>,
gleam@string:split(_pipe, <<"."/utf8>>)
end,
new_toml_element_list(D, Key, fun(Toml) -> case Toml of
{inline_table, T} ->
gleam@result:'try'(
get_string(T, <<"type"/utf8>>),
fun(Plist_line_type) -> case Plist_line_type of
<<"file"/utf8>> ->
gleam@result:'try'(
get_string(T, <<"src"/utf8>>),
fun(Src) ->
gleam@result:'try'(
get_string(T, <<"dest"/utf8>>),
fun(Dest) ->
gleam@result:'try'(
get_string(
T,
<<"mode"/utf8>>
),
fun(Mode) ->
gleam@result:'try'(
get_string(
T,
<<"owner"/utf8>>
),
fun(Owner) ->
gleam@result:'try'(
get_string(
T,
<<"group"/utf8>>
),
fun(
Group
) ->
{ok,
{plist_file,
Src,
Dest,
Mode,
Owner,
Group}}
end
)
end
)
end
)
end
)
end
);
<<"directory"/utf8>> ->
gleam@result:'try'(
get_string(T, <<"src_dir"/utf8>>),
fun(Src_dir) ->
gleam@result:'try'(
get_string(
T,
<<"dest_dir"/utf8>>
),
fun(Dest_dir) ->
gleam@result:'try'(
get_string(
T,
<<"mode"/utf8>>
),
fun(Mode@1) ->
gleam@result:'try'(
get_string(
T,
<<"owner"/utf8>>
),
fun(Owner@1) ->
gleam@result:'try'(
get_string(
T,
<<"group"/utf8>>
),
fun(
Group@1
) ->
{ok,
{plist_directory,
Src_dir,
Dest_dir,
Mode@1,
Owner@1,
Group@1}}
end
)
end
)
end
)
end
)
end
);
<<"dir_directive"/utf8>> ->
gleam@result:'try'(
get_string(T, <<"path"/utf8>>),
fun(Path) ->
{ok, {plist_dir_directive, Path}}
end
);
<<"include_directive"/utf8>> ->
gleam@result:'try'(
get_string(T, <<"path"/utf8>>),
fun(Path@1) ->
{ok,
{plist_include_directive,
Path@1}}
end
);
_ ->
{error,
{unexpected_plist_line_type,
Plist_line_type}}
end end
);
T@1 ->
{error, {unexpected_toml_type, T@1}}
end end).
-file("src/glm_freebsd/packager.gleam", 477).
?DOC(" Return a list of package dependencies from the toml dict.\n").
-spec new_pkg_dependencies(gleam@dict:dict(binary(), tom:toml())) -> {ok,
list(dependency_config())} |
{error, app_error()}.
new_pkg_dependencies(D) ->
Key = begin
_pipe = <<"freebsd.dependencies"/utf8>>,
gleam@string:split(_pipe, <<"."/utf8>>)
end,
new_toml_element_list(D, Key, fun(Toml) -> case Toml of
{table, T} ->
gleam@result:'try'(
get_string(T, <<"name"/utf8>>),
fun(Name) ->
gleam@result:'try'(
get_string(T, <<"version"/utf8>>),
fun(Version) ->
gleam@result:'try'(
get_string(T, <<"origin"/utf8>>),
fun(Origin) ->
_pipe@1 = {dependency_config,
Name,
Version,
Origin},
{ok, _pipe@1}
end
)
end
)
end
);
T@1 ->
{error, {unexpected_toml_type, T@1}}
end end).
-file("src/glm_freebsd/packager.gleam", 325).
?DOC(
" Extract the values from the provided toml dict and generate a Config instance.\n"
" If any of the required Config fields are not present, this function will fail.\n"
).
-spec new_config(gleam@dict:dict(binary(), tom:toml())) -> {ok, config()} |
{error, app_error()}.
new_config(Toml) ->
gleam@result:'try'(
get_string(Toml, <<"name"/utf8>>),
fun(App_name) ->
gleam@result:'try'(
get_string(Toml, <<"version"/utf8>>),
fun(App_version) ->
gleam@result:'try'(
get_optional_string(
Toml,
<<"freebsd.pkg_user_name"/utf8>>,
App_name
),
fun(Pkg_user_name) ->
gleam@result:'try'(
get_string(
Toml,
<<"freebsd.pkg_user_uid"/utf8>>
),
fun(Pkg_user_uid) ->
gleam@result:'try'(
get_string(
Toml,
<<"freebsd.pkg_description"/utf8>>
),
fun(Pkg_description) ->
gleam@result:'try'(
get_string(
Toml,
<<"freebsd.pkg_maintainer"/utf8>>
),
fun(Pkg_maintainer) ->
gleam@result:'try'(
new_pkg_dependencies(
Toml
),
fun(Pkg_dependencies) ->
gleam@result:'try'(
get_optional_string(
Toml,
<<"freebsd.pkg_proc_name"/utf8>>,
<<"/usr/local/lib/erlang28/*/bin/beam.smp"/utf8>>
),
fun(
Pkg_proc_name
) ->
gleam@result:'try'(
get_string(
Toml,
<<"freebsd.pkg_config_dir"/utf8>>
),
fun(
Pkg_config_dir
) ->
gleam@result:'try'(
get_optional_string(
Toml,
<<"freebsd.pkg_path_extensions"/utf8>>,
<<"/usr/local/lib/erlang28/bin"/utf8>>
),
fun(
Pkg_path_extensions
) ->
gleam@result:'try'(
get_optional_string(
Toml,
<<"freebsd.pkg_var_dir"/utf8>>,
filepath:join(
<<"/var"/utf8>>,
App_name
)
),
fun(
Pkg_var_dir
) ->
gleam@result:'try'(
get_optional_string(
Toml,
<<"freebsd.pkg_env_file"/utf8>>,
<<App_name/binary,
".env"/utf8>>
),
fun(
Pkg_env_file
) ->
gleam@result:'try'(
get_optional_string(
Toml,
<<"freebsd.pkg_prefix"/utf8>>,
<<"/usr/local"/utf8>>
),
fun(
Pkg_prefix
) ->
gleam@result:'try'(
get_optional_string(
Toml,
<<"freebsd.pkg_command"/utf8>>,
<<"entrypoint.sh"/utf8>>
),
fun(
Pkg_command
) ->
gleam@result:'try'(
get_optional_string(
Toml,
<<"freebsd.pkg_command_args"/utf8>>,
<<"run"/utf8>>
),
fun(
Pkg_command_args
) ->
gleam@result:'try'(
get_optional_string(
Toml,
<<"freebsd.pkg_daemon_flags"/utf8>>,
<<""/utf8>>
),
fun(
Pkg_daemon_flags
) ->
gleam@result:'try'(
new_pkg_plist_lines(
Toml
),
fun(
Pkg_plist_lines
) ->
gleam@result:'try'(
get_optional_string(
Toml,
<<"freebsd.pkg_origin"/utf8>>,
<<"private/"/utf8,
App_name/binary>>
),
fun(
Pkg_origin
) ->
gleam@result:'try'(
get_string(
Toml,
<<"freebsd.pkg_comment"/utf8>>
),
fun(
Pkg_comment
) ->
gleam@result:'try'(
get_optional_string(
Toml,
<<"freebsd.pkg_arch"/utf8>>,
<<"freebsd:15:x86:64"/utf8>>
),
fun(
Pkg_arch
) ->
gleam@result:'try'(
get_string(
Toml,
<<"freebsd.pkg_www"/utf8>>
),
fun(
Pkg_www
) ->
gleam@result:'try'(
get_optional_string(
Toml,
<<"freebsd.pkg_license_logic"/utf8>>,
<<"single"/utf8>>
),
fun(
Pkg_license_logic
) ->
gleam@result:'try'(
get_optional_strings(
Toml,
<<"freebsd.pkg_licenses"/utf8>>,
[<<"PRIVATE"/utf8>>]
),
fun(
Pkg_licenses
) ->
gleam@result:'try'(
get_pairs(
Toml
),
fun(
Pkg_pairs
) ->
Config = {config,
App_name,
App_version,
Pkg_user_name,
Pkg_user_uid,
Pkg_description,
Pkg_maintainer,
Pkg_dependencies,
Pkg_proc_name,
Pkg_config_dir,
Pkg_path_extensions,
Pkg_var_dir,
Pkg_env_file,
Pkg_prefix,
Pkg_command,
Pkg_command_args,
Pkg_daemon_flags,
Pkg_plist_lines,
Pkg_origin,
Pkg_comment,
Pkg_arch,
Pkg_www,
Pkg_license_logic,
Pkg_licenses,
Pkg_pairs},
{ok,
Config}
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
).
-file("src/glm_freebsd/packager.gleam", 314).
?DOC(
" Read the `gleam.toml` file and parse it into toml.\n"
" Any valid (parsable) toml will succeed here.\n"
).
-spec load_toml(binary()) -> {ok, gleam@dict:dict(binary(), tom:toml())} |
{error, app_error()}.
load_toml(Toml_file) ->
_pipe = Toml_file,
_pipe@1 = simplifile:read(_pipe),
_pipe@2 = gleam@result:map_error(
_pipe@1,
fun(Field@0) -> {unable_to_read_toml_file, Field@0} end
),
gleam@result:'try'(_pipe@2, fun(Text) -> _pipe@3 = tom:parse(Text),
gleam@result:map_error(
_pipe@3,
fun(Field@0) -> {unable_to_parse_toml_text, Field@0} end
) end).
-file("src/glm_freebsd/packager.gleam", 230).
-spec run(binary(), binary(), binary(), binary()) -> {ok, binary()} |
{error, app_error()}.
run(App_dir, Templates_dir, Staging_dir, Output_dir) ->
Metadata_dir = filepath:join(Staging_dir, <<"metadata"/utf8>>),
Staging_dir@1 = filepath:join(Staging_dir, <<"staging"/utf8>>),
Erlang_shipment_dir = filepath:join(
App_dir,
<<"build/erlang-shipment"/utf8>>
),
gleam_stdlib:println(<<"packaging..."/utf8>>),
gleam_stdlib:println(<<"app_dir: "/utf8, App_dir/binary>>),
gleam_stdlib:println(<<"templates_dir: "/utf8, Templates_dir/binary>>),
gleam_stdlib:println(<<"metadata_dir: "/utf8, Metadata_dir/binary>>),
gleam_stdlib:println(<<"staging_dir: "/utf8, Staging_dir@1/binary>>),
gleam_stdlib:println(<<"output_dir: "/utf8, Output_dir/binary>>),
gleam_stdlib:println(
<<"erlang_shipment_dir: "/utf8, Erlang_shipment_dir/binary>>
),
Toml_file = filepath:join(App_dir, <<"gleam.toml"/utf8>>),
_ = case simplifile_erl:is_file(Toml_file) of
{error, E} ->
gleam_stdlib:println_error(
begin
_pipe = E,
gleam@string:inspect(_pipe)
end
),
erlang:error(#{gleam_error => panic,
message => <<"`panic` expression evaluated."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"glm_freebsd/packager"/utf8>>,
function => <<"run"/utf8>>,
line => 250});
{ok, false} ->
gleam_stdlib:println_error(
<<"missing gleam.toml file: "/utf8, Toml_file/binary>>
),
erlang:error(#{gleam_error => panic,
message => <<"`panic` expression evaluated."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"glm_freebsd/packager"/utf8>>,
function => <<"run"/utf8>>,
line => 254});
{ok, true} ->
nil
end,
_ = case simplifile_erl:is_directory(Templates_dir) of
{error, E@1} ->
gleam_stdlib:println_error(
begin
_pipe@1 = E@1,
gleam@string:inspect(_pipe@1)
end
),
erlang:error(#{gleam_error => panic,
message => <<"`panic` expression evaluated."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"glm_freebsd/packager"/utf8>>,
function => <<"run"/utf8>>,
line => 261});
{ok, false} ->
gleam_stdlib:println_error(
<<"missing templates directory: "/utf8, Templates_dir/binary>>
),
erlang:error(#{gleam_error => panic,
message => <<"`panic` expression evaluated."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"glm_freebsd/packager"/utf8>>,
function => <<"run"/utf8>>,
line => 265});
{ok, true} ->
nil
end,
_ = case simplifile_erl:is_directory(Erlang_shipment_dir) of
{error, E@2} ->
gleam_stdlib:println_error(
<<<<<<"missing erlang shipment dir: "/utf8,
Erlang_shipment_dir/binary>>/binary,
", error: "/utf8>>/binary,
(begin
_pipe@2 = E@2,
gleam@string:inspect(_pipe@2)
end)/binary>>
),
erlang:error(#{gleam_error => panic,
message => <<"`panic` expression evaluated."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"glm_freebsd/packager"/utf8>>,
function => <<"run"/utf8>>,
line => 277});
{ok, false} ->
gleam_stdlib:println_error(
<<"missing erlang shipment dir: "/utf8,
Erlang_shipment_dir/binary>>
),
erlang:error(#{gleam_error => panic,
message => <<"`panic` expression evaluated."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"glm_freebsd/packager"/utf8>>,
function => <<"run"/utf8>>,
line => 281});
_ ->
nil
end,
_ = begin
_pipe@3 = Staging_dir@1,
_pipe@4 = simplifile:create_directory_all(_pipe@3),
_pipe@5 = gleam@result:map_error(
_pipe@4,
fun(Field@0) -> {unable_to_create_staging_dir, Field@0} end
),
_pipe@8 = gleam@result:'try'(_pipe@5, fun(_) -> _pipe@6 = Output_dir,
_pipe@7 = simplifile:create_directory_all(_pipe@6),
gleam@result:map_error(
_pipe@7,
fun(Field@0) -> {unable_to_create_output_dir, Field@0} end
) end),
_pipe@11 = gleam@result:'try'(_pipe@8, fun(_) -> _pipe@9 = Metadata_dir,
_pipe@10 = simplifile:create_directory_all(_pipe@9),
gleam@result:map_error(
_pipe@10,
fun(Field@0) -> {unable_to_create_metadat_dir, Field@0} end
) end),
_pipe@13 = gleam@result:'try'(_pipe@11, fun(_) -> _pipe@12 = Toml_file,
load_toml(_pipe@12) end),
_pipe@14 = gleam@result:'try'(_pipe@13, fun new_config/1),
_pipe@15 = gleam@result:'try'(
_pipe@14,
fun(_capture) ->
gen_staging(
_capture,
App_dir,
Metadata_dir,
Staging_dir@1,
Templates_dir,
Erlang_shipment_dir
)
end
),
gleam@result:'try'(
_pipe@15,
fun(_) -> build_package(Metadata_dir, Staging_dir@1, Output_dir) end
)
end.
-file("src/glm_freebsd/packager.gleam", 782).
?DOC(" Copy the raw templates to a directory so that the user can modify them for their own purposes.\n").
-spec copy_raw_templates(binary()) -> {ok, list(binary())} |
{error, app_error()}.
copy_raw_templates(Target_dir) ->
_pipe = simplifile:copy_directory(
<<"./priv/templates/freebsd"/utf8>>,
Target_dir
),
_pipe@1 = gleam@result:map_error(
_pipe,
fun(E) -> {unable_to_copy_templates_to_directory, Target_dir, E} end
),
_pipe@3 = gleam@result:map(
_pipe@1,
fun(_) -> _pipe@2 = simplifile:get_files(Target_dir),
gleam@result:map_error(
_pipe@2,
fun(E@1) ->
{unable_to_get_templates_from_directory, Target_dir, E@1}
end
) end
),
gleam@result:flatten(_pipe@3).