summaryrefslogtreecommitdiff
path: root/src/rebar_reltool.erl
blob: d228202589b385454a2e78aca1e711e2d4acdae1 (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
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
%% -------------------------------------------------------------------
%%
%% rebar: Erlang Build Tools
%%
%% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
%%
%% Permission is hereby granted, free of charge, to any person obtaining a copy
%% of this software and associated documentation files (the "Software"), to deal
%% in the Software without restriction, including without limitation the rights
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%% copies of the Software, and to permit persons to whom the Software is
%% furnished to do so, subject to the following conditions:
%%
%% The above copyright notice and this permission notice shall be included in
%% all copies or substantial portions of the Software.
%%
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%% THE SOFTWARE.
%% -------------------------------------------------------------------
-module(rebar_reltool).

-export([generate/2,
         clean/2]).

-include("rebar.hrl").
-include_lib("reltool/src/reltool.hrl").

%% ===================================================================
%% Public API
%% ===================================================================

generate(Config, ReltoolFile) ->
    %% Load the reltool configuration from the file
    ReltoolConfig = load_config(ReltoolFile),

    %% Spin up reltool server and load our config into it
    {ok, Server} = reltool:start_server([{sys, ReltoolConfig}]),

    %% Do some validation of the reltool configuration; error messages out of
    %% reltool are still pretty cryptic
    validate_rel_apps(Server, ReltoolConfig),

    %% Finally, run reltool
    case catch(run_reltool(Config, ReltoolConfig)) of
        ok ->
            ok;
        {error, failed} ->
            ?FAIL;
        Other2 ->
            ?ERROR("Unexpected error: ~p\n", [Other2]),
            ?FAIL
    end.


clean(Config, ReltoolFile) ->
    ReltoolConfig = load_config(ReltoolFile),
    TargetDir = target_dir(Config, ReltoolConfig),
    rebar_file_utils:rm_rf(TargetDir),
    rebar_file_utils:delete_each(["reltool.spec"]).



%% ===================================================================
%% Internal functions
%% ===================================================================

load_config(ReltoolFile) ->
    %% Load the reltool configuration from the file
    case file:consult(ReltoolFile) of
        {ok, [{sys, ReltoolConfig}]} ->
            ReltoolConfig;
        Other ->
            ?ERROR("Failed to load expected config from ~s: ~p\n", [ReltoolFile, Other]),
            ?FAIL
    end.
    

%%
%% Determine the name of the target directory; try the user provided name
%% first, or fall back to the release name if that's available. If neither
%% is available, just use "target"
%%
target_dir(Config, ReltoolConfig) ->
  case rebar_config:get(Config, target_name, undefined) of
      undefined ->
          case lists:keysearch(rel, 1, ReltoolConfig) of
              {value, {rel, Name, _Vsn, _Apps}} ->
                  Name;
              false ->
                  "target"
          end;
      Name ->
          Name
  end.

validate_rel_apps(ReltoolServer, ReltoolConfig) ->
    case lists:keysearch(rel, 1, ReltoolConfig) of
        {value, {rel, _Name, _Vsn, Apps}} ->
            %% Identify all the apps that do NOT exist, based on what's available
            %% from the reltool server
            Missing = lists:sort([App || App <- Apps, 
                                         app_exists(App, ReltoolServer) == false]),
            case Missing of
                [] ->
                    ok;
                _ ->
                    ?ERROR("Apps in {rel, ...} section not found by reltool: ~p\n", [Missing]),
                    ?FAIL
            end;
        {value, Rel} ->
            %% Invalid release format!
            ?ERROR("Invalid {rel, ...} section in reltools.config: ~p\n", [Rel]),
            ?FAIL;
        false ->
            ok
    end.

app_exists(App, Server) when is_atom(App) ->
    case reltool_server:get_app(Server, App) of
        {ok, _} ->
            true;
        _ ->
            false
    end;
app_exists(AppTuple, Server) when is_tuple(AppTuple) ->
    app_exists(element(1, AppTuple), Server).
                       

run_reltool(Config, ReltoolConfig) ->
    {ok, Server} = reltool:start_server([{sys, ReltoolConfig}]),
    case reltool:get_target_spec(Server) of
        {ok, Spec} ->
            dump_spec(Spec),
            TargetDir = target_dir(Config, ReltoolConfig),
            ok = file:make_dir(TargetDir),
            case reltool:eval_target_spec(Spec, code:root_dir(), TargetDir) of
                ok ->
                    ok;
                {error, Reason} ->
                    ?ERROR("Failed to generate target from spec: ~p\n", [Reason]),
                    ?FAIL
            end;
        {error, Reason} ->
            ?ERROR("Unable to generate spec: ~s\n", [Reason]),
            ?FAIL
    end.
    

dump_spec(Spec) ->
    case rebar_config:get_global(dump_spec, "0") of
        "1" ->
            SpecBin = list_to_binary(io_lib:print(Spec, 1, 120, -1)),
            ok = file:write_file("reltool.spec", SpecBin);
        _ ->
            ok
    end.