summaryrefslogtreecommitdiff
path: root/src/rebar_dir.erl
blob: e692c658402375a72bbbb7689d94330a2d219352 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
-module(rebar_dir).

-export([base_dir/1,
         deps_dir/1,
         deps_dir/2,
         plugins_dir/1,
         lib_dirs/1,
         profile_dir/1,
         default_deps_dir/1,
         default_profile_dir/1,
         default_profile_deps/1,
         home_dir/0,
         global_config_dir/1,
         get_cwd/0,
         ensure_dir/1,
         src_dirs/1,
         ebin_dir/0,
         processing_base_dir/1,
         processing_base_dir/2]).

-include("rebar.hrl").

-spec base_dir(rebar_state:t()) -> file:filename_all().
base_dir(State) ->
    rebar_state:get(State, base_dir, ?DEFAULT_BASE_DIR).

-spec deps_dir(rebar_state:t()) -> file:filename_all().
deps_dir(State) ->
    DepsDir = rebar_state:get(State, deps_dir, ?DEFAULT_DEPS_DIR),
    filename:join(profile_dir(State), DepsDir).

-spec deps_dir(file:filename_all(), file:filename_all()) -> file:filename_all().
deps_dir(DepsDir, App) ->
    filename:join(DepsDir, App).

-spec default_deps_dir(rebar_state:t()) -> file:filename_all().
default_deps_dir(State) ->
    DepsDir = rebar_state:get(State, deps_dir, ?DEFAULT_DEPS_DIR),
    filename:join([base_dir(State), "default", DepsDir]).

-spec plugins_dir(rebar_state:t()) -> file:filename_all().
plugins_dir(State) ->
    filename:join(base_dir(State), rebar_state:get(State, plugins_dir, ?DEFAULT_PLUGINS_DIR)).

-spec lib_dirs(rebar_state:t()) -> file:filename_all().
lib_dirs(State) ->
    rebar_state:get(State, project_app_dirs, ?DEFAULT_PROJECT_APP_DIRS).

-spec default_profile_dir(rebar_state:t()) -> file:filename_all().
default_profile_dir(State) ->
    filename:join(base_dir(State), "default").

profile_dir(State) ->
    case rebar_state:current_profile(State) of
        global ->
            global_config_dir(State);
        Profile ->
            filename:join(base_dir(State), atom_to_list(Profile))
    end.

-spec default_profile_deps(rebar_state:t()) -> file:filename_all().
default_profile_deps(State) ->
    filename:join(default_profile_dir(State), ?DEFAULT_DEPS_DIR).

home_dir() ->
    {ok, [[Home]]} = init:get_argument(home),
    Home.

global_config_dir(State) ->
    Home = home_dir(),
    rebar_state:get(State, global_rebar_dir, filename:join(Home, ?CONFIG_DIR)).

get_cwd() ->
    {ok, Dir} = file:get_cwd(),
    Dir.

%% TODO: filelib:ensure_dir/1 corrected in R13B04. Remove when we drop
%% support for OTP releases older than R13B04.
ensure_dir(Path) ->
    case filelib:ensure_dir(Path) of
        ok ->
            ok;
        {error,eexist} ->
            ok;
        Error ->
            Error
    end.

-spec src_dirs([string()]) -> [file:filename(), ...].
src_dirs([]) ->
    ["src"];
src_dirs(SrcDirs) ->
    SrcDirs.

ebin_dir() ->
    filename:join(get_cwd(), "ebin").

processing_base_dir(State) ->
    Cwd = get_cwd(),
    processing_base_dir(State, Cwd).

processing_base_dir(State, Dir) ->
    AbsDir = filename:absname(Dir),
    AbsDir =:= rebar_state:get(State, base_dir).