Permalink
Browse files

unparametrized agner_index, agner_github and agner_server

  • Loading branch information...
1 parent a8147b4 commit 523988793c79888c9f36465f829e9758e3a15948 @ferd committed Feb 21, 2011
Showing with 54 additions and 53 deletions.
  1. +3 −2 include/agner.hrl
  2. +6 −5 include/agner_index.hrl
  3. +25 −25 src/agner_github.erl
  4. +2 −3 src/agner_index.erl
  5. +18 −18 src/agner_server.erl
View
@@ -9,8 +9,9 @@
-type sha1() :: string().
--type agner_index() :: {github, string()}.
--type agner_indices() :: list(agner_index()).
+-type agner_index() :: github.
+-type agner_account() :: string().
+-type agner_indices() :: list({agner_index(), agner_account()}).
-type not_found_error() :: {error, not_found}.
View
@@ -1,5 +1,6 @@
--spec repositories() -> list(agner_repo()) | not_found_error().
--spec tags(agner_repo()) -> list({agner_repo_tag(),sha1()}) | not_found_error().
--spec branches(agner_repo()) -> list({agner_repo_branch(), sha1()}) | not_found_error().
--spec spec(agner_repo(), agner_package_version()) -> agner_spec() | not_found_error().
--spec spec_url(agner_repo(), sha1()) -> url() | not_found_error().
+%% depends on include/agner.hrl
+-spec repositories(agner_account()) -> list(agner_repo()) | not_found_error().
+-spec tags(agner_account(), agner_repo()) -> list({agner_repo_tag(),sha1()}) | not_found_error().
+-spec branches(agner_account(), agner_repo()) -> list({agner_repo_branch(), sha1()}) | not_found_error().
+-spec spec(agner_account(), agner_repo(), agner_package_version()) -> agner_spec() | not_found_error().
+-spec spec_url(agner_account(), agner_repo(), sha1()) -> url() | not_found_error().
View
@@ -1,22 +1,22 @@
%% -*- Mode: Erlang; tab-width: 4 -*-
--module(agner_github, [Account]).
+-module(agner_github).
-behaviour(agner_index).
-include_lib("agner.hrl").
-include_lib("agner_index.hrl").
-include_lib("kernel/include/file.hrl").
--export([repositories/0,
- repository/1,
- tags/1,
- branches/1,
- spec/2,
- spec_url/2
+-export([repositories/1,
+ repository/2,
+ tags/2,
+ branches/2,
+ spec/3,
+ spec_url/3
]).
-repositories() ->
- repositories(1).
+repositories(Account) ->
+ repositories(Account, 1).
-repositories(Page) ->
+repositories(Account, Page) ->
case request("https://github.com/api/v2/json/repos/show/" ++ Account ++ "?page=" ++ integer_to_list(Page)) of
{error, _Reason} = Error ->
Error;
@@ -37,22 +37,22 @@ repositories(Page) ->
{repo_name(proplists:get_value(<<"name">>, RepObject)),
proplists:get_value(<<"pushed_at">>, RepObject)}
end, Repositories)),
- Repos ++ repositories(Page + 1)
+ Repos ++ repositories(Account, Page + 1)
end
end.
-repository(Name) ->
- case request("https://github.com/api/v2/json/repos/show/" ++ proper_repo_name(Name)) of
+repository(Account, Name) ->
+ case request("https://github.com/api/v2/json/repos/show/" ++ proper_repo_name(Account, Name)) of
{error, _Reason} = Error ->
Error;
Object ->
proplists:get_value(<<"repository">>, Object)
end.
-tags(Name) ->
+tags(Account, Name) ->
{ok, RepoServer} = agner_repo_server:create(Name, {flavour, "master"}),
- ok = agner_repo_server:clone(RepoServer, fun (N) -> "git://github.com/" ++ proper_repo_name(N) ++ ".git" end),
+ ok = agner_repo_server:clone(RepoServer, fun (N) -> "git://github.com/" ++ proper_repo_name(Account, N) ++ ".git" end),
Path = agner_repo_server:file(RepoServer, []),
Port = agner_download:git(["tag", "-l"], [{cd, Path}, use_stdio, stderr_to_stdout, {line, 255}]),
PortHandler = fun (F,Acc) ->
@@ -98,9 +98,9 @@ tags(Name) ->
end.
-branches(Name) ->
+branches(Account, Name) ->
{ok, RepoServer} = agner_repo_server:create(Name, {flavour, "master"}),
- ok = agner_repo_server:clone(RepoServer, fun (N) -> "git://github.com/" ++ proper_repo_name(N) ++ ".git" end),
+ ok = agner_repo_server:clone(RepoServer, fun (N) -> "git://github.com/" ++ proper_repo_name(Account, N) ++ ".git" end),
Path = agner_repo_server:file(RepoServer, []),
Port = agner_download:git(["branch", "-r"], [{cd, Path}, use_stdio, stderr_to_stdout, {line, 255}]),
PortHandler = fun (F,Acc) ->
@@ -148,7 +148,7 @@ branches(Name) ->
end, Branches)
end.
-spec(Name, Version) ->
+spec(Account, Name, Version) ->
{ok, RepoServer} = agner_repo_server:create(Name, Version),
case agner_repo_server:pushed_at(RepoServer) of
At when is_list(At) ->
@@ -164,21 +164,21 @@ spec(Name, Version) ->
(C) ->
C
end, At)]),
- spec_1(RepoServer, AtFilename);
+ spec_1(Account, RepoServer, AtFilename);
undefined ->
{A,B,C} = now(),
N = node(),
TmpFile = lists:flatten(io_lib:format("/tmp/agner-~p-~p.~p.~p",[N,A,B,C])),
- Result = spec_1(RepoServer, TmpFile),
+ Result = spec_1(Account, RepoServer, TmpFile),
file:delete(TmpFile),
Result
end.
-spec_1(RepoServer, AtFilename) ->
+spec_1(Account, RepoServer, AtFilename) ->
Spec =
case file:read_file_info(AtFilename) of
{error, enoent} ->
- case agner_repo_server:clone(RepoServer, fun (Name) -> "git://github.com/" ++ proper_repo_name(Name) ++ ".git" end) of
+ case agner_repo_server:clone(RepoServer, fun (Name) -> "git://github.com/" ++ proper_repo_name(Account, Name) ++ ".git" end) of
ok ->
Config = agner_repo_server:file(RepoServer, "agner.config"),
{ok, S} = file:consult(Config),
@@ -195,12 +195,12 @@ spec_1(RepoServer, AtFilename) ->
end,
agner_spec:normalize(Spec).
-spec_url(Name, SHA1) ->
- "https://github.com/" ++ proper_repo_name(Name) ++ "/blob/" ++ SHA1 ++ "/agner.config".
+spec_url(Account, Name, SHA1) ->
+ "https://github.com/" ++ proper_repo_name(Account, Name) ++ "/blob/" ++ SHA1 ++ "/agner.config".
%%%
-proper_repo_name(Name) ->
+proper_repo_name(Account, Name) ->
case string:tokens(Name,"/") of
[_, _]=L ->
string:join(L,"/") ++ ".agner";
View
@@ -5,8 +5,7 @@
-spec behaviour_info(atom()) -> undefined | list({atom(), arity()}).
behaviour_info(callbacks) ->
- [{repositories,0}, {repository, 1}, {tags,1}, {branches, 1}, {spec, 2},
- {spec_url, 2}];
+ [{repositories,1}, {repository, 2}, {tags,2}, {branches, 2}, {spec, 3},
+ {spec_url, 3}];
behaviour_info(_Other) ->
undefined.
-
View
@@ -210,9 +210,9 @@ code_change(_OldVsn, State, _Extra) ->
-spec handle_spec(agner_package_name(), agner_package_version(), gen_server_from(), agner_indices()) -> any().
handle_spec(_,_,From,[]) ->
gen_server:reply(From, {error, not_found});
-handle_spec(Name, Version, From, [Mod0|Rest]) ->
+handle_spec(Name, Version, From, [{Mod0, Params}|Rest]) ->
Mod = index_module(Mod0),
- case Mod:spec(Name, Version) of
+ case Mod:spec(Params, Name, Version) of
{error, not_found} ->
handle_spec(Name, Version, From, Rest);
Data ->
@@ -222,11 +222,11 @@ handle_spec(Name, Version, From, [Mod0|Rest]) ->
-spec handle_spec_url(agner_package_name(), agner_package_version(), gen_server_from(), agner_indices()) -> any().
handle_spec_url(_,_,From,[]) ->
gen_server:reply(From, {error, not_found});
-handle_spec_url(Name, Version, From, [Mod0|Rest]) ->
+handle_spec_url(Name, Version, From, [{Mod0, Params}|Rest]) ->
Mod = index_module(Mod0),
- case sha1(Mod, Name, Version) of
+ case sha1(Mod, Params, Name, Version) of
SHA1 when is_list(SHA1) ->
- case Mod:spec_url(Name, SHA1) of
+ case Mod:spec_url(Params, Name, SHA1) of
{error, not_found} ->
handle_spec_url(Name, Version, From, Rest);
URL ->
@@ -245,13 +245,13 @@ handle_index(From, Acc, []) ->
agner_repo_server:set_pushed_at(Pid, binary_to_list(proplists:get_value(Name, Repos)))
end, RepoNames),
gen_server:reply(From, lists:usort(RepoNames));
-handle_index(From, Acc, [Mod0|Rest]) ->
+handle_index(From, Acc, [{Mod0, Params}|Rest]) ->
Mod = index_module(Mod0),
- case Mod:repositories() of
+ case Mod:repositories(Params) of
{error, not_found} ->
handle_index(From, Acc, Rest);
Repos ->
- handle_index(From, lists:map(fun (Repo) -> indexize(Mod0, Repo) end, Repos) ++ Acc, Rest)
+ handle_index(From, lists:map(fun (Repo) -> indexize(Mod0, Params, Repo) end, Repos) ++ Acc, Rest)
end.
-spec handle_fetch(agner_package_name() | agner_spec(), agner_package_version(), directory(), gen_server_from()) -> any().
@@ -273,31 +273,31 @@ handle_fetch(NameOrSpec, Version, Directory, From) ->
-spec handle_versions(agner_package_name(), gen_server_from(), agner_indices()) -> any().
handle_versions(_,From,[]) ->
gen_server:reply(From, {error, not_found});
-handle_versions(Name, From, [Mod0|Rest]) ->
+handle_versions(Name, From, [{Mod0, Params}|Rest]) ->
Mod = index_module(Mod0),
- case Mod:repository(Name) of
+ case Mod:repository(Params, Name) of
{error, not_found} ->
handle_versions(Name, From, Rest);
_ ->
Branches = lists:map(fun
({[$%|_],_}) -> undefined;
({"gh-pages",_}) -> undefined;
({Branch, _}) -> {flavour, Branch} end,
- Mod:branches(Name)),
+ Mod:branches(Params, Name)),
Tags = lists:map(fun ({[$%|_],_}) -> undefined;
({Tag, _}) -> {release, Tag} end,
- Mod:tags(Name)),
+ Mod:tags(Params, Name)),
gen_server:reply(From, lists:filter(fun (undefined) -> false; (_) -> true end, Branches ++ Tags))
end.
--spec sha1(agner_index(), agner_package_name(), agner_package_version()) -> sha1().
+-spec sha1(agner_index(), agner_account(), agner_package_name(), agner_package_version()) -> sha1().
-sha1(Mod, Name, Version) ->
+sha1(Mod, Params, Name, Version) ->
case Version of
{flavour, Branch} ->
Branch;
{release, Tag} ->
- Tags = Mod:tags(Name),
+ Tags = Mod:tags(Params, Name),
proplists:get_value(Tag, Tags);
no_such_version ->
no_such_version
@@ -307,14 +307,14 @@ sha1(Mod, Name, Version) ->
index_module(T) ->
case application:get_env(index_modules) of
{ok, Modules} ->
- setelement(1,T,proplists:get_value(element(1,T), Modules));
+ proplists:get_value(T, Modules);
_ ->
T
end.
-indexize({github, "agner"}, Name) ->
+indexize(github, "agner", Name) ->
Name;
-indexize({github, Account}, Name) ->
+indexize(github, Account, Name) ->
Account ++ "/" ++ Name.
indices() ->

0 comments on commit 5239887

Please sign in to comment.