summaryrefslogtreecommitdiff
path: root/src/rebar_fetch.erl
blob: 088a49afaeda5ea6ec3c79a01706537ca219ba05 (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
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
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 et
%% -------------------------------------------------------------------
%%
%% rebar: Erlang Build Tools
%%
%% -------------------------------------------------------------------
-module(rebar_fetch).

-export([lock_source/2,
         download_source/2,
         needs_update/2]).

-export([format_error/1]).

-include("rebar.hrl").

%% map short versions of resources to module names
-define(RESOURCES, [{git, rebar_git_resource}, {pkg, rebar_pkg_resource}]).

-spec lock_source(file:filename_all(), rebar_resource:resource()) ->
                         rebar_resource:resource() | {error, string()}.
lock_source(AppDir, Source) ->
    Module = get_resource_type(Source),
    Module:lock(AppDir, Source).

-spec download_source(file:filename_all(), rebar_resource:resource()) -> true | {error, any()}.
download_source(AppDir, Source) ->
    try
        Module = get_resource_type(Source),
        TmpDir = ec_file:insecure_mkdtemp(),
        AppDir1 = ec_cnv:to_list(AppDir),
        case Module:download(TmpDir, Source) of
            {ok, _} ->
                ec_file:mkdir_p(AppDir1),
                code:del_path(filename:absname(filename:join(AppDir1, "ebin"))),
                ec_file:remove(filename:absname(AppDir1), [recursive]),
                ok = ec_file:copy(TmpDir, filename:absname(AppDir1), [recursive]),
                true;
            {tarball, File} ->
                ec_file:mkdir_p(AppDir1),
                ok = erl_tar:extract(File, [{cwd, TmpDir}
                                           ,compressed]),
                BaseName = filename:basename(AppDir1),
                [FromDir] = filelib:wildcard(filename:join(TmpDir, BaseName++"-*")),
                code:del_path(filename:absname(filename:join(AppDir1, "ebin"))),
                ec_file:remove(filename:absname(AppDir1), [recursive]),
                ok = ec_file:copy(FromDir, filename:absname(AppDir1), [recursive]),
                true = code:add_patha(filename:join(AppDir1, "ebin")),
                true
        end
    catch
        _:_ ->
            {error, {rebar_fetch, {fetch_fail, Source}}}
    end.

-spec needs_update(file:filename_all(), rebar_resource:resource()) -> boolean() | {error, string()}.
needs_update(AppDir, Source) ->
    Module = get_resource_type(Source),
    try
        Module:needs_update(AppDir, Source)
    catch
        _:_ ->
            true
    end.

format_error({fetch_fail, Source}) ->
    io_lib:format("Failed to fetch and copy dep: ~p", [Source]).

get_resource_type({Type, Location}) ->
    find_resource_module(Type, Location);
get_resource_type({Type, Location, _}) ->
    find_resource_module(Type, Location);
get_resource_type({Type, _, _, Location}) ->
    find_resource_module(Type, Location);
get_resource_type(_) ->
    rebar_pkg_resource.

find_resource_module(Type, Location) ->
    case lists:keyfind(Type, 1, ?RESOURCES) of
        false ->
            case code:which(Type) of
                non_existing ->
                    {error, io_lib:format("Cannot handle dependency ~s.~n"
                                         "     No module for resource type ~p", [Location, Type])};
                _ ->
                    Type
            end;
        {Type, Module} ->
            Module
    end.