blah
This commit is contained in:
parent
3e3f72359a
commit
a1af4182ee
196
zx
196
zx
@ -144,44 +144,13 @@ run(Identifier, Args) ->
|
|||||||
State = #s{realm = Realm,
|
State = #s{realm = Realm,
|
||||||
name = Name,
|
name = Name,
|
||||||
version = Version},
|
version = Version},
|
||||||
NextState = #s{version = Installed} = ensure_installed(State),
|
NewState = #s{version = Installed} = ensure_installed(State),
|
||||||
PackageID = {Realm, Name, Installed},
|
PackageID = {Realm, Name, Installed},
|
||||||
Dir = filename:join("lib", package_string(PackageID)),
|
Dir = filename:join("lib", package_string(PackageID)),
|
||||||
Meta = read_meta(Dir),
|
Meta = read_meta(Dir),
|
||||||
Deps = maps:get(deps, Meta),
|
Deps = maps:get(deps, Meta),
|
||||||
NewState = ensure_deps(NextState#s{dir = Dir, deps = Deps}),
|
ok = ensure_deps(Deps),
|
||||||
execute(NewState).
|
execute(NewState#s{dir = Dir, deps = Deps}).
|
||||||
|
|
||||||
|
|
||||||
% Required = [PackageID | Deps],
|
|
||||||
% Needed = scrub(Required),
|
|
||||||
% Host = {"localhost", 11411},
|
|
||||||
% Socket = connect(Host, user),
|
|
||||||
% ok = fetch(Socket, Needed),
|
|
||||||
% ok = lists:foreach(fun install/1, Needed),
|
|
||||||
% ok = lists:foreach(fun build/1, Required),
|
|
||||||
% ok = file:set_cwd(PackageRoot),
|
|
||||||
% case maps:get(type, Meta) of
|
|
||||||
% app ->
|
|
||||||
% true = register(zx, self()),
|
|
||||||
% ok = inets:start(),
|
|
||||||
% ok = log(info, "Starting ~ts", [package_string(PackageID)]),
|
|
||||||
% PackageMod = list_to_atom(Name),
|
|
||||||
% {ok, Pid} = PackageMod:start(normal, Args),
|
|
||||||
% Mon = monitor(process, Pid),
|
|
||||||
% Shell = spawn(shell, start, []),
|
|
||||||
% ok = log(info, "Your shell is ~p, application is: ~p", [Shell, Pid]),
|
|
||||||
% State = #s{realm = Realm,
|
|
||||||
% name = Name,
|
|
||||||
% version = Version,
|
|
||||||
% pid = Pid,
|
|
||||||
% mon = Mon},
|
|
||||||
% exec_wait(State);
|
|
||||||
% lib ->
|
|
||||||
% Message = "Lib ~ts is available on the system, but is not a standalone app.",
|
|
||||||
% ok = log(info, Message, [package_string(PackageID)]),
|
|
||||||
% halt(0)
|
|
||||||
% end.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -348,10 +317,10 @@ ensure_installed(State = #s{realm = Realm, name = Name, version = Version}) ->
|
|||||||
| exact
|
| exact
|
||||||
| {ok, Installed :: version()}.
|
| {ok, Installed :: version()}.
|
||||||
%% @private
|
%% @private
|
||||||
%% Resolve the provided PackageID to the latest matching installed package directory version
|
%% Resolve the provided PackageID to the latest matching installed package directory
|
||||||
%% if one exists, returning a value that indicates whether an exact match was found (in the
|
%% version if one exists, returning a value that indicates whether an exact match was
|
||||||
%% case of a full version input), a version matching a partial version input was found, or no
|
%% found (in the case of a full version input), a version matching a partial version
|
||||||
%% match was found at all.
|
%% input was found, or no match was found at all.
|
||||||
|
|
||||||
resolve_installed_version(PackageID) ->
|
resolve_installed_version(PackageID) ->
|
||||||
PackageString = package_string(PackageID),
|
PackageString = package_string(PackageID),
|
||||||
@ -371,35 +340,52 @@ resolve_installed_version(PackageID) ->
|
|||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
ensure_deps(State = #s{realm = Realm, deps = Deps, socket = MaybeSocket}) ->
|
ensure_deps(Deps) ->
|
||||||
case scrub(Deps) of
|
case scrub(Deps) of
|
||||||
[] ->
|
[] ->
|
||||||
State;
|
ok;
|
||||||
Needed ->
|
Needed ->
|
||||||
Sorted = lists:sort(Needed),
|
Partitioned = partition_by_realm(Needed),
|
||||||
Partition =
|
EnsureDeps =
|
||||||
fun(D = {R, _, _}, M) ->
|
fun({Realm, Packages}) ->
|
||||||
maps:update_with(R, fun(Ds) -> [D | Ds] end, [D], M)
|
Socket = connect_user(Realm),
|
||||||
|
ok = ensure_deps(Socket, Realm, Packages),
|
||||||
|
ok = disconnect(Socket),
|
||||||
|
log(info, "Disconnecting from realm: ~ts", [Realm]),
|
||||||
end,
|
end,
|
||||||
Partitioned = lists:foldl(Partition, #{}, Needed),
|
lists:foreach(EnsureDeps, Partitioned)
|
||||||
|
end.
|
||||||
|
|
||||||
|
|
||||||
-spec ensure_dep(package_id()) -> ok | no_return().
|
partition_by_realm(PackageIDs) ->
|
||||||
|
Sorted = lists:sort(Needed),
|
||||||
|
PartitionMap = lists:foldl(fun partition_by_realm/2, #{}, Needed),
|
||||||
|
maps:to_list(PartitionMap).
|
||||||
|
|
||||||
|
|
||||||
|
partition_by_realm({R, P, V}, M) ->
|
||||||
|
maps:update_with(R, fun(Ps) -> [{P, V} | Ps] end, [{P, V}], M).
|
||||||
|
|
||||||
|
|
||||||
|
ensure_deps(_, _, []) ->
|
||||||
|
ok;
|
||||||
|
ensure_deps(Socket, Realm, [{Name, Version} | Rest]) ->
|
||||||
|
ok = ensure_dep(Socket, {Realm, Name, Version}),
|
||||||
|
ensure_deps(Socket, Realm, Rest).
|
||||||
|
|
||||||
|
|
||||||
|
-spec ensure_dep(gen_tcp:socket(), package_id()) -> ok | no_return().
|
||||||
%% @private
|
%% @private
|
||||||
%% Given an PackageID as an argument, check whether its package file exists in the
|
%% Given an PackageID as an argument, check whether its package file exists in the
|
||||||
%% system cache, and if not download it. Should return `ok' whenever the file is
|
%% system cache, and if not download it. Should return `ok' whenever the file is
|
||||||
%% sourced, but exit with an error if it cannot locate or acquire the package.
|
%% sourced, but exit with an error if it cannot locate or acquire the package.
|
||||||
|
|
||||||
ensure_dep(PackageID) ->
|
ensure_dep(Socket, PackageID) ->
|
||||||
ZrpFile = filename:join("zrp", namify_zrp(PackageID)),
|
ZrpFile = filename:join("zrp", namify_zrp(PackageID)),
|
||||||
ok =
|
ok =
|
||||||
case filelib:is_regular(ZrpFile) of
|
case filelib:is_regular(ZrpFile) of
|
||||||
true ->
|
true -> ok;
|
||||||
ok;
|
false -> fetch(Socket, PackageID)
|
||||||
false ->
|
|
||||||
PackageString = package_string(PackageID),
|
|
||||||
log(error, "Would fetch ~ts now, but not implemented", [PackageString]),
|
|
||||||
halt(0)
|
|
||||||
end,
|
end,
|
||||||
install(PackageID).
|
install(PackageID).
|
||||||
|
|
||||||
@ -576,10 +562,9 @@ verup(_) -> usage_exit(22).
|
|||||||
|
|
||||||
run_local(Args) ->
|
run_local(Args) ->
|
||||||
Meta = read_meta(),
|
Meta = read_meta(),
|
||||||
{package_id, PackageID} = lists:keyfind(package_id, 1, Meta),
|
{Realm, Name, Version} = maps:get(package_id, Meta),
|
||||||
{Realm, Name, Version} = PackageID,
|
Type = maps:get(type, Meta),
|
||||||
{type, Type} = lists:keyfind(type, 1, Meta),
|
Deps = maps:get(deps, Meta),
|
||||||
{deps, Deps} = lists:keyfind(deps, 1, Meta),
|
|
||||||
ok = build(),
|
ok = build(),
|
||||||
{ok, Dir} = file:get_cwd(),
|
{ok, Dir} = file:get_cwd(),
|
||||||
ok = file:set_cwd(zomp_dir()),
|
ok = file:set_cwd(zomp_dir()),
|
||||||
@ -589,12 +574,8 @@ run_local(Args) ->
|
|||||||
type = Type,
|
type = Type,
|
||||||
deps = Deps,
|
deps = Deps,
|
||||||
dir = Dir},
|
dir = Dir},
|
||||||
NewState = ensure_deps(State),
|
ok = ensure_deps(Deps),
|
||||||
execute(State).
|
ok = file:set_cwd(Dir),
|
||||||
|
|
||||||
|
|
||||||
ensure_deps(Deps, State) ->
|
|
||||||
|
|
||||||
execute(State).
|
execute(State).
|
||||||
|
|
||||||
|
|
||||||
@ -613,23 +594,14 @@ execute(State = #s{type = app}) ->
|
|||||||
pid = Pid,
|
pid = Pid,
|
||||||
mon = Mon},
|
mon = Mon},
|
||||||
exec_wait(State);
|
exec_wait(State);
|
||||||
execute(State = #s{type = lib}) ->
|
execute(State = #s{type = lib, realm = Realm, name = Name, version = Version}) ->
|
||||||
Message = "Lib ~ts is available on the system, but is not a standalone app",
|
Message = "Lib ~ts is available on the system, but is not a standalone app",
|
||||||
ok = log(info, Message, [package_string(PackageID)]),
|
PackageString = package_string({Realm, Name, Version}),
|
||||||
|
ok = log(info, Message, [PackageString]),
|
||||||
halt(0).
|
halt(0).
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
% Needed = scrub(Deps),
|
|
||||||
% Host = {"localhost", 11411},
|
|
||||||
% Socket = connect(Host, user),
|
|
||||||
% ok = fetch(Socket, Needed),
|
|
||||||
% ok = lists:foreach(fun install/1, Needed),
|
|
||||||
% ok = lists:foreach(fun build/1, Deps),
|
|
||||||
% ok = file:set_cwd(ProjectRoot),
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
%%% Package generation
|
%%% Package generation
|
||||||
|
|
||||||
-spec package(TargetDir) -> no_return()
|
-spec package(TargetDir) -> no_return()
|
||||||
@ -1443,53 +1415,45 @@ verify(Data, Signature, PubKey) ->
|
|||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
-spec fetch(gen_tcp:socket(), [package_id()]) -> ok.
|
-spec fetch(gen_tcp:socket(), package_id()) -> ok.
|
||||||
%% @private
|
%% @private
|
||||||
%% Download a list of deps to the local package cache.
|
%% Download a package to the local cache.
|
||||||
|
|
||||||
fetch(Socket, Needed) ->
|
fetch(Socket, PackageID) ->
|
||||||
Namified = lists:map(fun namify_zrp/1, Needed),
|
ok = send(Socket, {fetch, PackageID}),
|
||||||
{Have, Lack} = lists:partition(fun filelib:is_regular/1, Namified),
|
ok = await_zrp(Socket, PackageID),
|
||||||
ok = lists:foreach(fun(A) -> log(info, "Have ~ts", [A]) end, Have),
|
ok = receive_zrp(Socket, PackageID),
|
||||||
ok = lists:foreach(fun(A) -> log(info, "Lack ~ts", [A]) end, Lack),
|
log(info, "Fetched ~ts", [package_string(PackageID)]).
|
||||||
ok = send(Socket, "Would be sending fetch requests now."),
|
|
||||||
log(info, "Done fake fetching").
|
|
||||||
|
|
||||||
|
|
||||||
% Grouped = group_by_realm(Needed),
|
await_zrp(Socket, PackageID) ->
|
||||||
% Realms = [R || {R, _} <- Grouped],
|
receive
|
||||||
% Sockets = connect(Realms),
|
{tcp, Socket, Bin} ->
|
||||||
% fetch(Sockets, Groups).
|
case binary_to_term(Bin, [safe]) of
|
||||||
%
|
sending ->
|
||||||
%
|
ok;
|
||||||
%-spec group_by_realm(AppIDs) -> GroupedAppIDs
|
Error = {error, Reason} ->
|
||||||
% when AppIDs :: [app_id()],
|
PackageString = package_string(PackageID),
|
||||||
% GroupedAppIDs :: [{realm(), [app_id()]}].
|
Message = "Error receiving package ~ts: ~tp",
|
||||||
%%% @private
|
ok = log(info, Message, [PackageString, Reason]),
|
||||||
%%% Group apps by realm.
|
Error
|
||||||
%
|
after 60000 ->
|
||||||
%group_by_realm(AppIDs) ->
|
{error, timeout}
|
||||||
% Group =
|
end.
|
||||||
% fun(AppID = {Realm, _, _}, Groups) ->
|
|
||||||
% case lists:keyfind(Realm, Groups) of
|
|
||||||
% {Realm, Members} ->
|
|
||||||
% lists:keystore(Realm, 1, Groups, {Realm, [AppID | Members]});
|
|
||||||
% false ->
|
|
||||||
% lists:keystore(Realm, 1, Groups, {Realm, [AppID]})
|
|
||||||
% end
|
|
||||||
% end,
|
|
||||||
% lists:foldl(Group, AppIDs).
|
|
||||||
%
|
|
||||||
%
|
|
||||||
% ZrpFile = namify_zrp(AppID),
|
|
||||||
% case filelib:is_regular(filename:join("zrp", ZrpFile)) of
|
|
||||||
% true ->
|
|
||||||
% log(info, "Found in cache: ~ts", [ZrpFile]);
|
|
||||||
% false ->
|
|
||||||
% log(info, "Would download: ~ts", [ZrpFile])
|
|
||||||
% end.
|
|
||||||
|
|
||||||
|
|
||||||
|
receive_zrp(Socket, PackageID) ->
|
||||||
|
receive
|
||||||
|
{tcp, Socket, Bin} ->
|
||||||
|
ZrpPath = filename:join("zrp", namify_zrp(PackageID)),
|
||||||
|
ok = file:write_file(ZrpPath, Bin),
|
||||||
|
ok = send(Socket, ok),
|
||||||
|
log(info, "Wrote ~ts", [ZrpPath])
|
||||||
|
after 60000 ->
|
||||||
|
ok = log(error, "Timeout in socket receive for ~tp", [PackageID]),
|
||||||
|
{error, timeout}
|
||||||
|
end.
|
||||||
|
|
||||||
|
|
||||||
%%% Utility functions
|
%%% Utility functions
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user