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
|
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 et
%% -------------------------------------------------------------------
%%
%% 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(rebar3).
-export([main/1,
run/2,
global_option_spec_list/0,
init_config/0,
init_config1/1,
set_options/2,
parse_args/1,
version/0,
log_level/0]).
-include("rebar.hrl").
%% ====================================================================
%% Public API
%% ====================================================================
%% escript Entry point
main(Args) ->
case catch(run(Args)) of
{ok, _State} ->
ok;
rebar_abort ->
rebar_utils:delayed_halt(1);
{error, rebar_abort} ->
rebar_utils:delayed_halt(1);
{error, {Module, Reason}} ->
case code:which(Module) of
non_existing ->
?ERROR("Uncaught error in rebar_core. Run with DEBUG=1 to see stacktrace~n", []),
?DEBUG("Uncaught error: ~p ~p~n", [Module, Reason]);
_ ->
?ERROR(Module:format_error(Reason, []), [])
end,
rebar_utils:delayed_halt(1);
{error, Error} when is_list(Error) ->
?ERROR(Error++"~n", []),
rebar_utils:delayed_halt(1);
Error ->
%% Nothing should percolate up from rebar_core;
%% Dump this error to console
?ERROR("Uncaught error in rebar_core. Run with DEBUG=1 to see stacktrace~n", []),
?DEBUG("Uncaught error: ~p~n", [Error]),
rebar_utils:delayed_halt(1)
end.
%% Erlang-API entry point
run(BaseState, Command) ->
_ = application:load(rebar),
BaseState1 = rebar_state:set(BaseState, task, Command),
run_aux(BaseState1, [Command]).
%% ====================================================================
%% Internal functions
%% ====================================================================
run(RawArgs) ->
ok = load_rebar_app(),
BaseConfig = init_config(),
{BaseConfig1, _Args1} = set_options(BaseConfig, {[], []}),
run_aux(BaseConfig1, RawArgs).
load_rebar_app() ->
%% Pre-load the rebar app so that we get default configuration
ok = application:load(rebar).
init_config() ->
%% Initialize logging system
Verbosity = log_level(),
ok = rebar_log:init(command_line, Verbosity),
Config = case os:getenv("REBAR_CONFIG") of
false ->
rebar_config:consult_file(?DEFAULT_CONFIG_FILE);
ConfigFile ->
rebar_config:consult_file(ConfigFile)
end,
Config1 = case rebar_config:consult_file(?LOCK_FILE) of
[D] ->
[{locks, D} | Config];
_ ->
Config
end,
%% If $HOME/.rebar/config exists load and use as global config
GlobalConfigFile = filename:join([os:getenv("HOME"), ".rebar", "config"]),
State = case filelib:is_regular(GlobalConfigFile) of
true ->
?DEBUG("Load global config file ~p~n",
[GlobalConfigFile]),
GlobalConfig = rebar_state:new(rebar_config:consult_file(GlobalConfigFile)),
rebar_state:new(GlobalConfig, Config1);
false ->
rebar_state:new(Config1)
end,
%% Initialize vsn cache
rebar_state:set(State, vsn_cache, dict:new()).
init_config1(BaseConfig) ->
%% Determine the location of the rebar executable; important for pulling
%% resources out of the escript
try
ScriptName = filename:absname(escript:script_name()),
rebar_state:set(BaseConfig, escript, ScriptName)
catch
_:_ ->
BaseConfig
end.
run_aux(State, RawArgs) ->
%% Make sure crypto is running
case crypto:start() of
ok -> ok;
{error,{already_started,crypto}} -> ok
end,
application:start(asn1),
application:start(public_key),
application:start(ssl),
inets:start(),
State1 = init_config1(State),
code:add_pathsa([filename:join(rebar_utils:get_cwd(), "plugins")]),
%% Process each command, resetting any state between each one
State2 = rebar_state:set(State1, base_dir, filename:absname(rebar_state:dir(State1))),
{ok, Providers} = application:get_env(rebar, providers),
{ok, PluginProviders, State3} = rebar_plugins:install(State2),
rebar_core:update_code_path(State3),
State4 = rebar_state:create_logic_providers(Providers++PluginProviders, State3),
{Task, Args} = parse_args(RawArgs),
rebar_core:process_command(rebar_state:command_args(State4, Args), list_to_atom(Task)).
%%
%% Parse command line arguments using getopt and also filtering out any
%% key=value pairs. What's left is the list of commands to run
%%
parse_args([]) ->
parse_args(["help"]);
parse_args([H | Rest]) when H =:= "-h"
; H =:= "--help" ->
parse_args(["help" | Rest]);
parse_args([H | Rest]) when H =:= "-v"
; H =:= "--version" ->
parse_args(["version" | Rest]);
parse_args([RawTask | RawRest]) ->
{RawTask, RawRest}.
set_options(State, {Options, NonOptArgs}) ->
GlobalDefines = proplists:get_all_values(defines, Options),
State1 = rebar_state:set(State, defines, GlobalDefines),
%% Set global variables based on getopt options
State2 = set_global_flag(State1, Options, force),
Task = proplists:get_value(task, Options, "help"),
{rebar_state:set(State2, task, Task), NonOptArgs}.
%%
%% get log level based on getopt option
%%
log_level() ->
case os:getenv("QUIET") of
false ->
DefaultLevel = rebar_log:default_level(),
case os:getenv("DEBUG") of
false ->
DefaultLevel;
_ ->
DefaultLevel + 3
end;
_ ->
rebar_log:error_level()
end.
%%
%% show version information and halt
%%
version() ->
{ok, Vsn} = application:get_key(rebar, vsn),
?CONSOLE("rebar ~s on Erlang/OTP ~s Erts ~s~n",
[Vsn, erlang:system_info(otp_release), erlang:system_info(version)]).
%%
%% set global flag based on getopt option boolean value
%%
set_global_flag(State, Options, Flag) ->
Value = case proplists:get_bool(Flag, Options) of
true ->
"1";
false ->
"0"
end,
rebar_state:set(State, Flag, Value).
%%
%% options accepted via getopt
%%
global_option_spec_list() ->
[
%% {Name, ShortOpt, LongOpt, ArgSpec, HelpMsg}
{help, $h, "help", undefined, "Print this help."},
%{verbose, $v, "verbose", integer, "Verbosity level (-v, -vv)."},
{version, $V, "version", undefined, "Show version information."},
%{config, $C, "config", string, "Rebar config file to use."},
{task, undefined, undefined, string, "Task to run."}
].
|