summaryrefslogtreecommitdiff
path: root/src/rebar_packages.erl
blob: 5c676008549c556da8c359052086e17a154a6867 (plain)
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
-module(rebar_packages).

-export([get_packages/1]).

-export_type([package/0]).

-include("rebar.hrl").

-type pkg_name() :: binary() | atom().
-type vsn() :: binary().
-type package() :: pkg_name() | {pkg_name(), vsn()}.

-spec get_packages(rebar_state:t()) -> {rebar_dict(), rebar_digraph()}.
get_packages(State) ->
    RebarDir = rebar_dir:global_cache_dir(State),
    RegistryDir = filename:join(RebarDir, "packages"),
    DictFile = filename:join(RegistryDir, "dict"),
    Edges = filename:join(RegistryDir, "edges"),
    Vertices = filename:join(RegistryDir, "vertices"),
    Neighbors = filename:join(RegistryDir, "neighbors"),

    case lists:all(fun(X) -> filelib:is_file(X) end, [DictFile, Edges, Vertices, Neighbors]) of
        true ->
            try
                {ok, DictBinary} = file:read_file(DictFile),
                Dict = binary_to_term(DictBinary),
                {ok, EdgesTab} = ets:file2tab(Edges),
                {ok, VerticesTab} = ets:file2tab(Vertices),
                {ok, NeighborsTab} = ets:file2tab(Neighbors),
                {Dict, {digraph, EdgesTab, VerticesTab, NeighborsTab, true}}
            catch
                _:_ ->
                    ?ERROR("Bad packages index, try to fix with `rebar3 update`", []),
                    {dict:new(), digraph:new()}
            end;
        false ->
            ?ERROR("Bad packages index, try to fix with `rebar3 update`", []),
            {dict:new(), digraph:new()}
    end.