Less magic

This commit is contained in:
Craig Everett 2017-12-12 10:10:52 +09:00
parent 11d5f3f767
commit 24738f5278
4 changed files with 88 additions and 70 deletions

1
Emakefile Normal file
View File

@ -0,0 +1 @@
{"src/*", [debug_info, {i, "include/"}, {outdir, "ebin/"}]}.

6
ebin/zx.app Normal file
View File

@ -0,0 +1,6 @@
{application, zx,
[{description, "Zomp client program"},
{vsn, "0.1.0"},
{applications, [stdlib, kernel]},
{modules, [zx, zx_daemon]},
{mod, {zx, []}}]}.

79
zx → src/zx.erl Executable file → Normal file
View File

@ -1,6 +1,5 @@
#! /usr/bin/env escript %%% @doc
%%% ZX
%%% zx
%%% %%%
%%% A general dependency and packaging tool that works together with the zomp %%% A general dependency and packaging tool that works together with the zomp
%%% package manager. Given a project directory with a standard layout, zx can: %%% package manager. Given a project directory with a standard layout, zx can:
@ -11,10 +10,15 @@
%%% - Update, upgrade or run any application from source that zomp tracks. %%% - Update, upgrade or run any application from source that zomp tracks.
%%% - Locally install packages from files and locally stored public keys. %%% - Locally install packages from files and locally stored public keys.
%%% - Build and run a local project from source using zomp dependencies. %%% - Build and run a local project from source using zomp dependencies.
%%% @end
-module(zx). -module(zx).
-mode(compile). -export([start/2, main/1]).
-export([main/1]).
-export_type([serial/0, package_id/0, package/0, realm/0, name/0, version/0, option/0,
host/0, key_id/0, key_name/0, user/0, username/0, lower0_9/0, label/0,
package_meta/0]).
-record(s, -record(s,
@ -1195,71 +1199,6 @@ remove_binaries(TargetDir) ->
ok = log(info, "Removing: ~tp", [ToDelete]), ok = log(info, "Removing: ~tp", [ToDelete]),
lists:foreach(fun file:delete/1, ToDelete) lists:foreach(fun file:delete/1, ToDelete)
end. end.
%%% App execution loop
-spec exec_wait(State) -> no_return()
when State :: state().
%% @private
%% Execution maintenance loop.
%% Once an application is started by zompc this process will wait for a message from
%% the application if that application was written in a way to take advantage of zompc
%% facilities such as post-start upgrade checking.
%%
%% NOTE:
%% Adding clauses to this `receive' is where new functionality belongs.
%% It may make sense to add a `zompc_lib' as an available dependency authors could
%% use to interact with zompc without burying themselves under the complexity that
%% can come with naked send operations. (Would it make sense, for example, to have
%% the registered zompc process convert itself to a gen_server via zompc_lib to
%% provide more advanced functionality?)
exec_wait(State = #s{pid = none, mon = none}) ->
receive
{monitor, Pid} ->
Mon = monitor(process, Pid),
exec_wait(State#s{pid = Pid, mon = Mon});
Unexpected ->
ok = log(warning, "Unexpected message: ~tp", [Unexpected]),
exec_wait(State)
end;
exec_wait(State = #s{pid = Pid, mon = Mon}) ->
receive
{check_update, Requester, Ref} ->
{Response, NewState} = check_update(State),
Requester ! {Ref, Response},
exec_wait(NewState);
{exit, Reason} ->
ok = log(info, "Exiting with: ~tp", [Reason]),
halt(0);
{'DOWN', Mon, process, Pid, normal} ->
ok = log(info, "Application exited normally."),
halt(0);
{'DOWN', Mon, process, Pid, Reason} ->
ok = log(warning, "Application exited with: ~tp", [Reason]),
halt(1);
Unexpected ->
ok = log(warning, "Unexpected message: ~tp", [Unexpected]),
exec_wait(State)
end.
-spec check_update(State) -> {Response, NewState}
when State :: state(),
Response :: term(),
NewState :: state().
%% @private
%% Check for updated version availability of the current application.
%% The return value should probably provide up to three results, a Major, Minor and
%% Patch update, and allow the Requestor to determine what to do with it via some
%% interaction.
check_update(State) ->
ok = log(info, "Would be checking for an update of the current application now..."),
Response = "Nothing was checked, but you can imagine it to have been.",
{Response, State}.

72
src/zx_daemon.erl Normal file
View File

@ -0,0 +1,72 @@
%%% @doc
%%% ZX Daemon
%%%
%%% Resident execution daemon and runtime interface to Zomp.
%%% @end
-module(zx_daemon).
-export([]).
%%% App execution loop
-spec exec_wait(State) -> no_return()
when State :: state().
%% @private
%% Execution maintenance loop.
%% Once an application is started by zompc this process will wait for a message from
%% the application if that application was written in a way to take advantage of zompc
%% facilities such as post-start upgrade checking.
%%
%% NOTE:
%% Adding clauses to this `receive' is where new functionality belongs.
%% It may make sense to add a `zompc_lib' as an available dependency authors could
%% use to interact with zompc without burying themselves under the complexity that
%% can come with naked send operations. (Would it make sense, for example, to have
%% the registered zompc process convert itself to a gen_server via zompc_lib to
%% provide more advanced functionality?)
exec_wait(State = #s{pid = none, mon = none}) ->
receive
{monitor, Pid} ->
Mon = monitor(process, Pid),
exec_wait(State#s{pid = Pid, mon = Mon});
Unexpected ->
ok = log(warning, "Unexpected message: ~tp", [Unexpected]),
exec_wait(State)
end;
exec_wait(State = #s{pid = Pid, mon = Mon}) ->
receive
{check_update, Requester, Ref} ->
{Response, NewState} = check_update(State),
Requester ! {Ref, Response},
exec_wait(NewState);
{exit, Reason} ->
ok = log(info, "Exiting with: ~tp", [Reason]),
halt(0);
{'DOWN', Mon, process, Pid, normal} ->
ok = log(info, "Application exited normally."),
halt(0);
{'DOWN', Mon, process, Pid, Reason} ->
ok = log(warning, "Application exited with: ~tp", [Reason]),
halt(1);
Unexpected ->
ok = log(warning, "Unexpected message: ~tp", [Unexpected]),
exec_wait(State)
end.
-spec check_update(State) -> {Response, NewState}
when State :: state(),
Response :: term(),
NewState :: state().
%% @private
%% Check for updated version availability of the current application.
%% The return value should probably provide up to three results, a Major, Minor and
%% Patch update, and allow the Requestor to determine what to do with it via some
%% interaction.
check_update(State) ->
ok = log(info, "Would be checking for an update of the current application now..."),
Response = "Nothing was checked, but you can imagine it to have been.",
{Response, State}.