Permalink
Browse files

work

  • Loading branch information...
1 parent 2456f51 commit 88249fc177126f2816be67b06a987063ac3b6487 Dmytro Lytovchenko committed Feb 9, 2014
Showing with 91 additions and 59 deletions.
  1. +1 −0 .gitignore
  2. +25 −9 src/epm.erl
  3. +2 −8 src/epm_core.erl
  4. +3 −2 src/epm_deps.erl
  5. +38 −23 src/epm_index.erl
  6. +22 −17 src/epm_ops.erl
View
@@ -7,3 +7,4 @@ epm
.idea
*.plt
*.log
+epm_local_index
View
@@ -8,9 +8,10 @@
, matches/2
, platform/1
, p/3, p/2, p/1
- , s/2]).
+ , s/2, pkgid_match_spec/1]).
-include("epm.hrl").
+-include_lib("stdlib/include/ms_transform.hrl").
main(Args) ->
application:load(sasl),
@@ -59,15 +60,18 @@ erlang_vsn(#pkgid{erlang_vsn=X}) -> X.
%%------------------------------------------------------------------------------
as_string(#pkg{id=Id}) -> as_string(Id);
-as_string(#pkgid{author=?any_author, pkg_name=N, vsn=V, platform=P, erlang_vsn=E}) ->
- lists:flatten(io_lib:format("Package ~s/~s ~s/~s", [N,V, P,E]));
as_string(#pkgid{author=A, pkg_name=N, vsn=V, platform=P, erlang_vsn=E}) ->
- lists:flatten(io_lib:format("Package by ~s ~s/~s ~s/~s", [A,N,V, P,E]));
+ "["
+ ++ case A of ?any_author -> ""; _ -> s("~s/", [A]) end
+ ++ N
+ ++ case V of ?any_vsn -> ""; _ -> s(", ~s", [V]) end
+ ++ case P of ?any_platform -> ""; _ -> s(", ~s", [P]) end
+ ++ case E of ?any_erlang_vsn -> ""; _ -> s(", ~s", [E]) end
+ ++ "]";
as_string(#repo{id=I, description=_D, url=U}) ->
- lists:flatten(io_lib:format("Repo ~s url=~s", [I, U]));
+ s("[Repo ~s url=~s]", [I, U]);
as_string(#repoid{name=N}) ->
- lists:flatten(io_lib:format("Repo id ~s", [N]));
-as_string(undefined) -> "undefined".
+ s("[Repo id ~s]", [N]).
%% @doc Checks that P1 with some wildcard fields matches P2
matches(#pkgid{}=P1, #pkgid{}=P2) ->
@@ -82,17 +86,29 @@ matches(#pkgid{}=P1, #pkgid{}=P2) ->
andalso (P1Platf =:= platform(P2) orelse P1Platf =:= ?any_platform)
).
+pkgid_match_spec(#pkgid{ author=A1, pkg_name=N1, platform=P1
+ , vsn=V1, erlang_vsn=E1}) ->
+ ets:fun2ms(fun(#pkg{id=#pkgid{ author=A2, pkg_name=N2, platform=P2
+ , vsn=V2, erlang_vsn=E2}}=Pkg)
+ when (N1 =:= N2
+ andalso (A1 =:= A2 orelse A1 =:= ?any_author)
+ andalso (V1 =:= V2 orelse V1 =:= ?any_vsn)
+ andalso (E1 =:= E2 orelse E1 =:= ?any_erlang_vsn)
+ andalso (P1 =:= P2 orelse P1 =:= ?any_platform)
+ ) -> Pkg end).
+
+%%------------------------------------------------------------------------------
%% @doc Uncolored print
p(Text) -> io:format(Text).
%% @doc Colored print and formatted uncolored print
p(Color, Text) when is_atom(Color) ->
- io:format(ansi_color(Color) ++ Text ++ ansi_endfont());
+ p(ansi_color(Color) ++ Text ++ ansi_endfont());
p(Text, Args) -> io:format(Text, Args).
%% @doc Formatted colored print
p(Color, Format, Args) when is_atom(Color) ->
- io:format(ansi_color(Color) ++ Format ++ ansi_endfont() ++ "~n", Args).
+ p(ansi_color(Color) ++ Format ++ ansi_endfont() ++ "~n", Args).
%% @doc sprintf
s(Format, Args) ->
View
@@ -18,19 +18,13 @@ execute(State=#epm_state{}, ["install" | Args]) ->
case Installed of
[] -> ok;
_ ->
- epm:p("===============================~n"
- "Packages already installed:~n"
- "===============================~n"),
+ epm:p(white, "Packages already installed:~n"),
[epm:p(" + ~s~n", [epm:as_string(P)]) || P <- Installed]
end,
- epm:p("===============================~n"
- "Install the following packages?~n"
- "===============================~n"),
+ epm:p(white, "Install the following packages? ([y]/n)~n"),
[epm:p(" + ~s~n", [epm:as_string(P)]) || P <- NotInstalled],
- epm:p(yellow, "~n([y]/n) "),
case io:get_chars("", 1) of
C when C == "y"; C == "\n" ->
- epm:p("~n"),
lists:foldl(fun(X, St) -> epm_ops:install_package(St, X) end
, State, NotInstalled);
_ -> ok
View
@@ -16,6 +16,7 @@
%% -----------------------------------------------------------------------------
-spec resolve_dependencies(Pkgids :: [pkgid()]) -> [pkgid()].
resolve_dependencies(Pkgids) ->
+ %% TODO: Return tree of subdependencies for visual printout
resolve_dependencies_internal(ordsets:from_list(Pkgids), ordsets:new()).
-spec resolve_dependencies_internal(ordsets:ordset(pkgid())
@@ -24,9 +25,9 @@ resolve_dependencies(Pkgids) ->
resolve_dependencies_internal(Pkgids, Pkgids) -> Pkgids;
resolve_dependencies_internal(Pkgids, _Previous) ->
F = fun(Pkgid=#pkgid{}, A) ->
- [Pkg] = epm_index:find_package(Pkgid),
+ PkgList = epm_index:list_global_matching(Pkgid),
lists:foldl(fun(Dep, A1) -> ordsets:add_element(Dep, A1) end
- , A, Pkg#pkg.deps)
+ , A, lists:flatten([Pkg#pkg.deps || Pkg <- PkgList]))
end,
PkgidsPlusDeps = lists:foldl(F, ordsets:from_list(Pkgids), Pkgids),
resolve_dependencies_internal(PkgidsPlusDeps, Pkgids).
View
@@ -9,18 +9,20 @@
%% API
-export([open/2
- , list_local_packages/0
- , list_local_by/3
, delete_local/1
, insert_local/2
- , close/0, find_package/1]).
+ , close/0
+ , is_installed/1]).
+-export([list_local_packages/0
+ , list_global_packages/0
+ , list_global_matching/1
+ , list_local_matching/1
+ ]).
-include("epm.hrl").
-%-include_lib("stdlib/include/qlc.hrl").
--include_lib("stdlib/include/ms_transform.hrl").
-%% -define(local_index, epm_local_index).
-%% -define(local_index_file, "epm_local_index").
+-define(local_index, epm_local_index).
+-define(local_index_file, "epm_local_index").
-define(global_index, epm_index).
-define(global_index_file, "epm_packages").
@@ -29,43 +31,56 @@
%% -define(global_repo_index_file, "epm_repos").
%%------------------------------------------------------------------------------
-find_package(#pkgid{ author=A1, pkg_name=N1, platform=P1
- , vsn=V1, erlang_vsn=E1}) ->
- Q = ets:fun2ms(fun(#pkg{id=#pkgid{ author=A2, pkg_name=N2, platform=P2
- , vsn=V2, erlang_vsn=E2}}=Pkg)
- when (N1 =:= N2
- andalso (A1 =:= A2 orelse A1 =:= ?any_author)) -> Pkg
- end),
- ets:select(?global_index, Q).
+is_installed(Id=#pkgid{}) ->
+ [] =/= list_local_matching(Id).
+
close() -> ok.
+
open(_Home, EpmHome) ->
G1 = open_dets_file(filename:join([EpmHome, ?global_index_file])),
ets:new(?global_index, [named_table, {keypos, #pkg.id}]),
lists:foreach(fun(X1) -> ets:insert(?global_index, X1) end, G1),
%% Fixtures
- CowboyId = #pkgid{pkg_name="cowboy"},
- RanchId = #pkgid{pkg_name="ranch"},
- GunId = #pkgid{pkg_name="gun"},
- FwId = #pkgid{pkg_name="farwest"},
- OtherId = #pkgid{pkg_name="other"},
+ CowboyId = #pkgid{author="extend", pkg_name="cowboy", vsn="2.7", platform=x64},
+ Cowboy2Id = #pkgid{author="derp", pkg_name="cowboy", vsn="2.6"},
+ RanchId = #pkgid{pkg_name="ranch", vsn="1.1"},
+ GunId = #pkgid{pkg_name="gun", vsn="0.1-dev", platform=x86},
+ FwId = #pkgid{pkg_name="farwest", vsn="1a"},
+ OtherId = #pkgid{pkg_name="other", erlang_vsn="r13b"},
ets:insert(?global_index, #pkg{id=CowboyId, deps=[RanchId]}),
+ ets:insert(?global_index, #pkg{id=Cowboy2Id, deps=[RanchId]}),
ets:insert(?global_index, #pkg{id=RanchId, deps=[GunId]}),
ets:insert(?global_index, #pkg{id=GunId}),
ets:insert(?global_index, #pkg{id=FwId}),
ets:insert(?global_index, #pkg{id=OtherId}),
+ G2 = open_dets_file(?local_index_file),
+ ets:new(?local_index, [named_table, {keypos, #pkg.id}]),
+ lists:foreach(fun(X2) -> ets:insert(?local_index, X2) end, G2),
+ ets:insert(?local_index, #pkg{id=GunId}),
+
State = #epm_state{},
State.
+%% @doc List all installed packages in local index
list_local_packages() ->
+ ets:match(?local_index, '$1').
+
+%% @doc List all available packages in global index
+list_global_packages() ->
ets:match(?global_index, '$1').
-%% @doc Provide '_' if field is not relevant
-list_local_by(User, ProjectName, Version) ->
- ets:match(?global_index, {{User, ProjectName, Version}, '$1'}).
+%% @doc Search local index (installed)
+list_local_matching(Id=#pkgid{}) ->
+ Q = epm:pkgid_match_spec(Id),
+ ets:select(?local_index, Q).
+
+list_global_matching(Id=#pkgid{}) ->
+ Q = epm:pkgid_match_spec(Id),
+ ets:select(?global_index, Q).
delete_local(Key={_User, _Name, _Vsn}) ->
ets:delete(?global_index, Key).
View
@@ -12,7 +12,7 @@
%% , retrieve_remote_repos/4
, installed_packages/1
, get_installed_packages/1
- , split_package/1
+ , split_package/1 % remove this?
, read_vsn_from_args/2
, print_installed_package_info/1
, print_not_installed_package_info/2
@@ -29,17 +29,19 @@
%% @doc Returns pair of installed and not installed packages
-spec filter_installed_packages([#pkg{}]) ->
{[#pkg{}], [#pkg{}]}.
-filter_installed_packages(Packages) ->
- filter_installed_packages(Packages, [], []).
-
-%% @private
-filter_installed_packages([], Installed, NotInstalled) ->
- {lists:reverse(Installed), NotInstalled};
-filter_installed_packages([Package|Tail], Installed, NotInstalled) ->
- case local_package_info(Package) of
- [] -> filter_installed_packages(Tail, Installed, [Package|NotInstalled]);
- [P|_] -> filter_installed_packages(Tail, [P|Installed], NotInstalled)
- end.
+filter_installed_packages(Pkgids) ->
+ lists:partition(fun(Id=#pkgid{}) -> epm_index:is_installed(Id) end, Pkgids).
+%% filter_installed_packages(Packages) ->
+%% filter_installed_packages(Packages, [], []).
+%%
+%% %% @private
+%% filter_installed_packages([], Installed, NotInstalled) ->
+%% {lists:reverse(Installed), NotInstalled};
+%% filter_installed_packages([Package|Tail], Installed, NotInstalled) ->
+%% case local_package_info(Package) of
+%% [] -> filter_installed_packages(Tail, Installed, [Package|NotInstalled]);
+%% [P|_] -> filter_installed_packages(Tail, [P|Installed], NotInstalled)
+%% end.
split_package(Raw) -> split_package(Raw, []).
@@ -162,28 +164,28 @@ dependant_installed_packages(#pkg{}=Package
local_package_info(#pkg{id=#pkgid{ author = ?any_author
, pkg_name = ProjectName
, vsn = ?any_vsn}}) ->
- case epm_index:list_local_by('_', ProjectName, '_') of
+ case epm_index:list_local_matching('_', ProjectName, '_') of
[] -> [];
List -> [Package || [Package] <- List]
end;
local_package_info(#pkg{id=#pkgid{ author = ?any_author
, pkg_name = ProjectName
, vsn = Vsn}}) ->
- case epm_index:list_local_by('_', ProjectName, Vsn) of
+ case epm_index:list_local_matching('_', ProjectName, Vsn) of
[] -> [];
List -> [Package || [Package] <- List]
end;
local_package_info(#pkg{id=#pkgid{ author = User
, pkg_name = ProjectName
, vsn = ?any_vsn }}) ->
- case epm_index:list_local_by(User, ProjectName, '_') of
+ case epm_index:list_local_matching(User, ProjectName, '_') of
[] -> [];
List -> [Package || [Package] <- List]
end;
local_package_info(#pkg{id=#pkgid{ author = User
, pkg_name = ProjectName
, vsn = Vsn }}) ->
- case epm_index:list_local_by(User, ProjectName, Vsn) of
+ case epm_index:list_local_matching(User, ProjectName, Vsn) of
[] -> [];
List -> [Package || [Package] <- List]
end.
@@ -308,7 +310,10 @@ update_package(State=#epm_state{}, #pkgid{}=Pkgid) ->
%% -----------------------------------------------------------------------------
-spec install_package(#epm_state{}, #pkgid{}) -> #epm_state{}.
install_package(State=#epm_state{}, #pkgid{}=Pkgid) ->
- io:format("+ searching to install package ~s~n" , [epm:as_string(Pkgid)]),
+ epm:p("+ searching to install ~s~n" , [epm:as_string(Pkgid)]),
+ PkgList = epm_index:list_global_matching(Pkgid),
+ epm:p(" available: ~s~n",
+ [string:join(lists:map(fun epm:as_string/1, PkgList), "; ")]),
%Repo = Package#pkg.repo,
%User = Repo#repo.owner,
%Name = Repo#repo.name,

0 comments on commit 88249fc

Please sign in to comment.