Skip to content

Commit

Permalink
huge refact
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmytro Lytovchenko committed Feb 8, 2014
1 parent 06cd768 commit 682dcdc
Show file tree
Hide file tree
Showing 9 changed files with 345 additions and 324 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -5,4 +5,5 @@ ebin/*
epm
*.iml
.idea
*.plt
*.plt
*.log
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -9,7 +9,7 @@ PLT_LIBS0 = ebin $(wildcard deps/*/ebin)
PLT_LIBS = $(subst deps/riak_pb/ebin,,$(PLT_LIBS0))

DIALYZER_APPS = epm
DIALYZER_APPS_PATHS = ebin
DIALYZER_APPS_PATHS = $(wildcard ebin/*.beam)
#$(addsuffix /ebin, $(addprefix apps/, $(DIALYZER_APPS)))

.PHONY: check_plt
Expand Down
68 changes: 46 additions & 22 deletions include/epm.hrl
Expand Up @@ -3,30 +3,54 @@
-define(DEFAULT_API_MODULES, [github_api]).
-define(epm_index_filename, "epm_index").

-define(any_author, any_author).
-define(any_vsn, any_vsn).

-define(EXIT(Format, Args), exit(lists:flatten(io_lib:format(Format, Args)))).
-type platform() :: x86 | x64.
-type erlangvsn() :: binary().

-record(repoid, { name :: string()
}).
-type repoid() :: #repoid{}.

%% Unified global identifier for package
-record(pkgid, { author :: string()
, pkg_name :: string()
, platform :: platform()
, vsn :: string()
, erlang_vsn :: erlangvsn()
}).
-type pkgid() :: #pkgid{}.

-record(repo, { id :: repoid()
, description :: string()
, url :: string()
%, followers
%, pushed
, api_module
}).
-type repo() :: #repo{}.

-record(pkg, { id :: pkgid()
%, vsn=undefined :: string()
, install_dir :: string()
, deps=[] :: [pkgid()]
, args=[] :: list()
, repo :: repoid()
}).
-type pkg() :: #pkg{}.

-record(installed_pkg, { id :: pkgid()
, repository :: repoid()
, platform :: platform()
}).
-type installed_pkg() :: #installed_pkg{}.

%% State of the application, contains loaded local index, installed apps and
%% remote index
-record(epm_state, { local_available = []
, installed = []
, remote_available = []
-record(epm_state, { local_available=[] :: [pkg()]
, installed=[] :: [installed_pkg()]
, remote_available=[] :: [pkg()]
, repositories=[] :: [repo()]
}).

-record(epm_repo, { name
, owner
, description
, homepage
, followers
, pushed
, api_module
}).

-record(epm_package, {user
, name
, vsn=undefined
, app_vsn
, install_dir
, deps=[]
, args=[]
, repo=#epm_repo{}
}).
45 changes: 34 additions & 11 deletions src/epm.erl
@@ -1,21 +1,20 @@
-module(epm).
-export([main/1]).
-export([main/1
, author/1, name/1, vsn/1, erlang_vsn/1, as_string/1, equals/2]).

-include("epm.hrl").

main(Args) ->
application:load(sasl),
application:set_env(sasl, sasl_error_logger, false),
lists:map(fun application:start/1, [sasl, crypto, public_key, ssl, ibrowse, epm]),
lists:map(fun application:start/1
, [sasl, crypto, public_key, ssl, ibrowse, epm]),

case (catch main_internal(Args)) of
{'EXIT', ErrorMsg} when is_list(ErrorMsg) ->
io:format("- ~s~n", [ErrorMsg]);
{'EXIT', Other} ->
io:format("~p~n", [Other]);
_ ->
ok
{'EXIT', Msg} when is_list(Msg) -> io:format("- ~s~n", [Msg]);
{'EXIT', Other} -> io:format("~p~n", [Other]);
_ -> ok
end,

epm_index:close(),
io:format("~n").

Expand All @@ -24,10 +23,34 @@ main_internal(Args) ->
Home = epm_util:home_dir(),
EpmHome = epm_util:epm_home_dir(Home),
State = epm_index:open(Home, EpmHome),
setup_connectivity(),
setup_proxy(),
epm_core:execute(State, Args).

setup_connectivity() ->
setup_proxy() ->
epm_util:set_http_proxy( epm_cfg:get(proxy_host, none)
, epm_cfg:get(proxy_port, none)),
epm_util:set_net_timeout(epm_cfg:get(net_timeout, 6000)).

%%------------------------------------------------------------------------------
author(#pkg{id=#pkgid{author=X}}) -> X.

name(#pkg{id=#pkgid{pkg_name=X}}) -> X;
name(#repo{id=#repoid{name=X}}) -> X.

vsn(#pkg{id=#pkgid{vsn=X}}) -> X.

platform(#pkg{id=#pkgid{platform=X}}) -> X.

erlang_vsn(#pkg{id=#pkgid{erlang_vsn=X}}) -> X.

as_string(#pkgid{author=A, pkg_name=N, vsn=V, platform=P, erlang_vsn=E}) ->
lists:flatten(io_lib:format("~s/~s/~s ~s/~s", [A,N,V, P,E]));
as_string(#repo{id=I, description=_D, url=U}) ->
lists:flatten(io_lib:format("Repo ~s url=~s", [I, U])).

equals(#pkgid{}=P1, #pkgid{}=P2) ->
(author(P1) == author(P2) orelse author(P2) =:= ?any_author)
andalso (name(P1) == name(P2))
andalso (vsn(P1) == vsn(P2))
andalso (erlang_vsn(P1) == vsn(P2))
andalso (platform(P1) == platform(P2)).
66 changes: 30 additions & 36 deletions src/epm_core.erl
Expand Up @@ -18,14 +18,12 @@ execute(State=#epm_state{}, ["install" | Args]) ->
io:format("===============================~n"),
io:format("Packages already installed:~n"),
io:format("===============================~n"),
[io:format(" + ~s-~s-~s (~s)~n", [U, N, V, AppVsn])
|| #epm_package{user = U, name = N, vsn = V, app_vsn = AppVsn} <- Installed]
[io:format(" + ~s~n", [epm:as_string(P)]) || P <- Installed]
end,
io:format("===============================~n"),
io:format("Install the following packages?~n"),
io:format("===============================~n"),
[io:format(" + ~s-~s-~s~n", [U, N, V])
|| #epm_package{user = U, name = N, vsn = V} <- NotInstalled],
[io:format(" + ~s~n", [epm:as_string(P)]) || P <- NotInstalled],
io:format("~n([y]/n) "),
case io:get_chars("", 1) of
C when C == "y"; C == "\n" ->
Expand All @@ -47,8 +45,7 @@ execute(State=#epm_state{}, ["remove" | Args]) ->
io:format("===============================~n"),
io:format("Remove the following packages?~n"),
io:format("===============================~n"),
[io:format(" + ~s-~s-~s~n", [U, N, V])
|| #epm_package{user = U, name = N, vsn = V} <- Installed],
[io:format(" + ~s~n", [epm:as_string(P)]) || P <- Installed],
io:format("~n([y]/n) "),
case io:get_chars("", 1) of
C when C == "y"; C == "\n" ->
Expand All @@ -70,8 +67,7 @@ execute(State=#epm_state{}, ["update" | Args]) ->
io:format("===============================~n"),
io:format("Update the following packages?~n"),
io:format("===============================~n"),
[io:format(" + ~s-~s-~s~n", [U, N, V])
|| #epm_package{user = U, name = N, vsn = V} <- Installed],
[io:format(" + ~s~n", [epm:as_string(P)]) || P <- Installed],
io:format("~n([y]/n) "),
case io:get_chars("", 1) of
C when C == "y"; C == "\n" ->
Expand Down Expand Up @@ -209,15 +205,16 @@ execute(_State=#epm_state{}, _) ->
%% parse input args
%% -----------------------------------------------------------------------------

%% collect_args(Target, Args, GlobalConfig) -> Results
%% Target = atom()
%% Args = [string()]
%%` GlobalConfig = list()
%% Results = {[package(), Flags]}
%% Flags = [atom()]
-spec collect_args(Target :: atom(), Args :: [string()])
-> {[#pkgid{}], [atom()]}.
collect_args(Target, Args) ->
collect_args_internal(Target, Args, [], []).

-spec collect_args_internal(Target :: atom()
, Args :: [string()]
, Packages :: [#pkgid{}]
, Flags :: [atom()])
-> {[#pkgid{}], [atom()]}.
collect_args_internal(_, [], Packages, Flags) ->
{lists:reverse(Packages), lists:reverse(Flags)};
collect_args_internal(Target, [Arg | Rest], Packages, Flags) ->
Expand All @@ -226,43 +223,40 @@ collect_args_internal(Target, [Arg | Rest], Packages, Flags) ->
%% split into user and project
{ProjectName, User} = epm_ops:split_package(Arg),
collect_args_internal(Target, Rest
, [#epm_package{user = User, name = ProjectName}|Packages]
, [#pkgid{author=User, pkg_name=ProjectName}|Packages]
, Flags);
{Type, Tag, 0} -> %% tag with no trailing value
case Type of
project ->
[#epm_package{args = Args} = Package|OtherPackages] = Packages,
[#pkg{args = Args} = Package|OtherPackages] = Packages,
collect_args_internal(Target, Rest
, [Package#epm_package{args = Args ++ [Tag]}|OtherPackages]
, [Package#pkg{args = Args ++ [Tag]}|OtherPackages]
, Flags);
global ->
collect_args_internal(Target, Rest, Packages, [Tag|Flags])
end;
{Type, Tag, NumVals} when is_integer(NumVals) -> % tag with trailing value(s)
if
length(Rest) < NumVals ->
if length(Rest) < NumVals ->
exit("poorly formatted command");
true -> ok
end,
{Vals, Rest1} = lists:split(NumVals, Rest),
Vals1 =
case Vals of
[V] -> V;
_ -> Vals
end,
Vals1 = case Vals of
[V] -> V;
_ -> Vals
end,
case Type of
project ->
%% this tag applies to the last project on the stack
[#epm_package{args = Args} = Package|OtherPackages] = Packages,
Vsn = if
Tag == tag; Tag == branch; Tag == sha -> Vals1;
true -> Package#epm_package.vsn
end,
collect_args_internal( Target, Rest1
, [Package#epm_package{ vsn = Vsn
, args = Args ++ [{Tag, Vals1}]
} | OtherPackages]
, Flags);
%% project ->
%% %% this tag applies to the last project on the stack
%% [#pkg{args=Args}=Package | OtherPackages] = Packages,
%% Vsn = if Tag == tag; Tag == branch; Tag == sha -> Vals1;
%% true -> Package#pkg.vsn
%% end,
%% collect_args_internal( Target, Rest1
%% , [Package#pkg{ vsn = Vsn
%% , args = Args ++ [{Tag, Vals1}]
%% } | OtherPackages]
%% , Flags);
global ->
if Tag == config_set ->
[K, V1] = Vals1,
Expand Down

0 comments on commit 682dcdc

Please sign in to comment.