Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 341 lines (295 sloc) 12.391 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
%% -*- Mode: Erlang; tab-width: 4 -*-
-module(agner_server).
-include_lib("agner.hrl").
-include_lib("typespecs/include/typespecs.hrl").
-behaviour(gen_server).

%% API
-export([start_link/0]).
-export([spec/2, spec_url/2, index/0, fetch/3, versions/1]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).

-define(SERVER, ?MODULE).

-define(TIMEOUT, 60000).

-record(state, {
         }).

-type gen_server_state() :: #state{}.

%%%===================================================================
%%% API
%%%===================================================================

%%--------------------------------------------------------------------
%% @doc
%% Starts the server
%%
%% @spec start_link() -> {ok, Pid} | ignore | {error, Error}
%% @end
%%--------------------------------------------------------------------
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).

%% @doc Ask the server for a spec on Name and Version
%% @end
-spec spec(agner_package_name(), agner_package_version()) -> agner_spec().
                  
spec(Name, Version) ->
    gen_server:call(?SERVER, {spec, Name, Version}, ?TIMEOUT).

%% @doc Ask the server for a spec URL
%% @end
-spec spec_url(agner_package_name(), agner_package_version()) -> url().

spec_url(Name, Version) ->
    gen_server:call(?SERVER, {spec_url, Name, Version}, ?TIMEOUT).

%% @doc Ask the server for an index
%% @end
-spec index() -> list(agner_package_name()).
                   
index() ->
    gen_server:call(?SERVER, index, infinity).

%% @doc Fetch a package/project to a directory
%% @end
-spec fetch(agner_package_name(), agner_package_version(), directory()) -> ok | not_found_error();
           (agner_spec(), any(), directory()) -> ok | not_found_error().
                   
fetch(NameOrSpec, Version, Directory) ->
        gen_server:call(?SERVER, {fetch, NameOrSpec, Version, Directory}, infinity).

%% @doc Ask for the versions of a given package, Name
%% @end
-spec versions(agner_package_name()) -> list(agner_package_version()).
                      
versions(Name) ->
    gen_server:call(?SERVER, {versions, Name},?TIMEOUT).

%%%===================================================================
%%% gen_server callbacks
%%%===================================================================

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Initializes the server
%%
%% @spec init(Args) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% @end
%%--------------------------------------------------------------------
-spec init([]) -> gen_server_init_result().

init([]) ->
{ok, #state{
      }}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling call messages
%%
%% @spec handle_call(Request, From, State) ->
%% {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
-type agner_call_spec() :: {spec, agner_package_name(), agner_package_version()}.
-type agner_call_spec_url() :: {spec_url, agner_package_name(), agner_package_version()}.
-type agner_call_index() :: index.
-type agner_call_fetch() :: {fetch, agner_package_name() | agner_spec(), agner_package_version(), directory()}.
-type agner_call_versions() :: {versions, agner_package_name()}.

-spec handle_call(agner_call_spec(), gen_server_from(), gen_server_state()) -> gen_server_async_reply(agner_spec()|{error, bad_version}) ;
                 (agner_call_spec_url(), gen_server_from(), gen_server_state()) -> gen_server_async_reply(url()|{error, bad_version}) ;
                 (agner_call_index(), gen_server_from(), gen_server_state()) -> gen_server_async_reply(list(agner_package_name())) ;
                 (agner_call_fetch(), gen_server_from(), gen_server_state()) -> gen_server_async_reply(ok | {error, any()}) ;
                 (agner_call_versions(), gen_server_from(), gen_server_state()) -> gen_server_async_reply(list(agner_package_version()) | not_found_error()).

handle_call({spec, Name, Version}, From, #state{}=State) ->
spawn_link(fun () ->
handle_spec(Name, Version, From, indices())
end),
{noreply, State};

handle_call({spec_url, Name, Version}, From, #state{}=State) ->
spawn_link(fun () ->
handle_spec_url(Name, Version, From, indices())
end),
{noreply, State};

handle_call(index, From, #state{}=State) ->
spawn_link(fun () ->
handle_index(From, [], indices())
end),
{noreply, State};

handle_call({fetch, NameOrSpec, Version, Directory}, From, #state{}=State) ->
spawn_link(fun () ->
handle_fetch(NameOrSpec, Version, Directory, From)
end),
{noreply, State};

handle_call({versions, Name}, From, #state{}=State) ->
spawn_link(fun () ->
handle_versions(Name, From, indices())
end),
{noreply, State}.


%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling cast messages
%%
%% @spec handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_cast(_Msg, State) ->
{noreply, State}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling all non call/cast messages
%%
%% @spec handle_info(Info, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------

-spec handle_info(any(), gen_server_state()) ->
gen_server_handle_info_result().

handle_info(_, State) ->
{noreply, State}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any
%% necessary cleaning up. When it returns, the gen_server terminates
%% with Reason. The return value is ignored.
%%
%% @spec terminate(Reason, State) -> void()
%% @end
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
ok.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Convert process state when code is changed
%%
%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
%% @end
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.

%%%===================================================================
%%% Internal functions
%%%===================================================================
-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, Params}|Rest]) ->
Mod = index_module(Mod0),
    case Mod:exists(Params, Name) of
        true ->
            case Mod:spec(Params, Name, Version) of
                {error, not_found} ->
                    handle_spec(Name, Version, From, Rest);
                Data ->
                    gen_server:reply(From, Data)
            end;
        false ->
            handle_spec(Name, Version, From, Rest)
    end.

-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, Params}|Rest]) ->
Mod = index_module(Mod0),
    case sha1(Mod, Params, Name, Version) of
        SHA1 when is_list(SHA1) ->
            case Mod:exists(Params, Name) of
                true ->
                    case Mod:spec_url(Params, Name, SHA1) of
                        {error, not_found} ->
                            handle_spec_url(Name, Version, From, Rest);
                        URL ->
                            gen_server:reply(From, URL)
                    end;
                false ->
                    handle_spec_url(Name, Version, From, Rest)
            end;
        _ ->
            gen_server:reply(From, {error, bad_version})
    end.

-spec handle_index(gen_server_from(), list(agner_package_name()), list(tuple())) -> any().
handle_index(From, Acc, []) ->
    Repos = lists:reverse(Acc),
    RepoNames = lists:map(fun ({Name, _}) -> Name end, Repos),
    lists:foreach(fun (Name) ->
                          {ok, Pid} = agner_repo_server:create(Name, {flavour, "master"}),
                          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, Params}|Rest]) ->
Mod = index_module(Mod0),
case Mod:repositories(Params) of
{error, not_found} ->
handle_index(From, Acc, Rest);
Repos ->
            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().
handle_fetch(NameOrSpec, Version, Directory, From) ->
    case io_lib:printable_list(NameOrSpec) of
        true ->
            case agner:spec(NameOrSpec, Version) of
                {error, _} = Error ->
                    gen_server:reply(From, Error);
                Spec ->
                    agner_download:fetch(Spec, Directory),
                    gen_server:reply(From, ok)
            end;
        false -> %% it is a spec
            agner_download:fetch(NameOrSpec, Directory),
            gen_server:reply(From, ok)
    end.

-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, Params}|Rest]) ->
Mod = index_module(Mod0),
    case Mod:exists(Params, Name) of
        true ->
            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(Params, Name)),
                    Tags = lists:map(fun ({[$%|_],_}) -> undefined;
                                         ({Tag, _}) -> {release, Tag} end,
                                     Mod:tags(Params, Name)),
                    gen_server:reply(From, lists:filter(fun (undefined) -> false; (_) -> true end, Branches ++ Tags))
            end;
        false ->
            handle_versions(Name, From, Rest)
    end.

-spec sha1(agner_index(), agner_account(), agner_package_name(), agner_package_version()) -> sha1().
                  
sha1(Mod, Params, Name, Version) ->
    case Version of
        {flavour, Branch} ->
            Branch;
        {release, Tag} ->
            Tags = Mod:tags(Params, Name),
            proplists:get_value(Tag, Tags);
        no_such_version ->
            no_such_version
    end.


index_module(T) ->
    case application:get_env(index_modules) of
        {ok, Modules} ->
            proplists:get_value(T, Modules);
        _ ->
            T
    end.

indexize(github, "agner", Name) ->
    Name;
indexize(github, Account, Name) ->
    Account ++ "/" ++ Name.

indices() ->
    case application:get_env(indices) of
        {ok, Val} ->
            Val;
        undefined ->
            []
    end.
Something went wrong with that request. Please try again.