From 24738f5278f82810144443b3539e1517bfa9037a Mon Sep 17 00:00:00 2001 From: Craig Everett Date: Tue, 12 Dec 2017 10:10:52 +0900 Subject: [PATCH] Less magic --- Emakefile | 1 + ebin/zx.app | 6 ++++ zx => src/zx.erl | 79 ++++++----------------------------------------- src/zx_daemon.erl | 72 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 70 deletions(-) create mode 100644 Emakefile create mode 100644 ebin/zx.app rename zx => src/zx.erl (97%) mode change 100755 => 100644 create mode 100644 src/zx_daemon.erl diff --git a/Emakefile b/Emakefile new file mode 100644 index 0000000..68c7b67 --- /dev/null +++ b/Emakefile @@ -0,0 +1 @@ +{"src/*", [debug_info, {i, "include/"}, {outdir, "ebin/"}]}. diff --git a/ebin/zx.app b/ebin/zx.app new file mode 100644 index 0000000..4e623c4 --- /dev/null +++ b/ebin/zx.app @@ -0,0 +1,6 @@ +{application, zx, + [{description, "Zomp client program"}, + {vsn, "0.1.0"}, + {applications, [stdlib, kernel]}, + {modules, [zx, zx_daemon]}, + {mod, {zx, []}}]}. diff --git a/zx b/src/zx.erl old mode 100755 new mode 100644 similarity index 97% rename from zx rename to src/zx.erl index ddd3501..43ad019 --- a/zx +++ b/src/zx.erl @@ -1,6 +1,5 @@ -#! /usr/bin/env escript - -%%% zx +%%% @doc +%%% ZX %%% %%% A general dependency and packaging tool that works together with the zomp %%% 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. %%% - Locally install packages from files and locally stored public keys. %%% - Build and run a local project from source using zomp dependencies. +%%% @end -module(zx). --mode(compile). --export([main/1]). +-export([start/2, 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, @@ -1195,71 +1199,6 @@ remove_binaries(TargetDir) -> ok = log(info, "Removing: ~tp", [ToDelete]), lists:foreach(fun file:delete/1, ToDelete) 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}. diff --git a/src/zx_daemon.erl b/src/zx_daemon.erl new file mode 100644 index 0000000..e8073a0 --- /dev/null +++ b/src/zx_daemon.erl @@ -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}.