-
Notifications
You must be signed in to change notification settings - Fork 2
/
rebar.config.script
299 lines (258 loc) · 11.1 KB
/
rebar.config.script
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
%% Strip any combination of newline and whitespace off both ends of S
StrStrip = fun(S, F, S) ->
S;
(S, F, _) ->
F(string:strip(string:strip(S, both, $\n), both), F, S)
end.
StrClean = fun(S) ->
StrStrip(S, StrStrip, "")
end.
IsFalsy = fun(false) -> true;
(S) when is_list(S) -> StrClean(S) =:= [];
(_) -> false
end.
IsTruthy = fun(true) -> true;
(S) when is_list(S) -> StrClean(S) =/= [];
(_) -> false
end.
GetEnv =
fun F(EnvVar, {IfFalsyFun, IfTruthyFun}) when is_function(IfFalsyFun, 1) andalso
is_function(IfTruthyFun, 1) ->
case IsTruthy(Val = os:getenv(EnvVar)) of
true -> IfTruthyFun(Val);
false -> IfFalsyFun(Val)
end;
F(EnvVar, {IfFalsyFun}) ->
F(EnvVar, IfFalsyFun);
F(EnvVar, bool) ->
F(EnvVar, {fun(_) -> false end, fun(_) -> true end});
F(EnvVar, IfFalsyFun) when is_function(IfFalsyFun, 1) ->
F(EnvVar, {IfFalsyFun, StrClean})
end.
%% If EnvVar exists and has some content, call UserFun(Content, UserArg)
%% and return its result.
%% Otherwise return UserArg unchanged.
RunIfEnvVar =
fun(EnvVar, UserFun, UserArg) ->
IfFalsyFun = fun(_) -> UserArg end,
IfTruthyFun = fun(Val) -> UserFun(Val, UserArg) end,
GetEnv(EnvVar, {IfFalsyFun, IfTruthyFun})
end.
IsDbg = GetEnv("REBAR_EXTRA_DBG", bool).
DbgDo = fun(F) when is_function(F, 0) -> IsDbg andalso F() end.
DbgMsg = fun(Fmt, Args) -> DbgDo(fun() -> io:format(Fmt, Args) end) end.
Msg = fun(Fmt, Args) ->
io:format(standard_error, Fmt, Args),
true
end.
DbgMsg("CONFIG: ~p~n", [CONFIG]).
GetFileAppVersion = fun() ->
case file:read_file("APP_VERSION") of
{ok, BVsn} ->
DbgMsg("File vsn: ~p~n", [BVsn]),
S = StrClean(binary_to_list(BVsn)),
DbgMsg("Cleaned vsn: ~p~n", [S]),
S;
_ ->
undefined
end
end.
GetAppVersion =
fun() -> GetEnv("APP_VERSION", fun(_) -> GetFileAppVersion() end) end.
%% Ensure edown target is valid
ValTarget = fun(Target) ->
ATarget = list_to_atom(Target),
lists:member(ATarget, ['github', 'gitlab', 'stash'])
orelse throw({bad_edown_target, Target}),
ATarget
end.
GetCfg = fun(Key, Config, Default) ->
case lists:keyfind(Key, 1, Config) of
false ->
Default;
{Key, PL} ->
PL
end
end.
KeyStore = fun(Key, Config, NewVal) ->
lists:keystore(Key, 1, Config, {Key, NewVal})
end.
GetEDocOpts = fun(Config) -> GetCfg(edoc_opts, Config, []) end.
GetPreHooks = fun(Config) -> GetCfg(pre_hooks, Config, []) end.
SetPreHooks = fun(PreHooks, Config) ->
KeyStore(pre_hooks, Config, PreHooks)
end.
GetCTHook = fun(PreHooks) -> GetCfg(ct, PreHooks, undefined) end.
SetCTHook = fun(CTHook, PreHooks) ->
KeyStore(ct, PreHooks, CTHook)
end.
GetCTOpts = fun(Config) -> GetCfg(ct_opts, Config, []) end.
SetCTOpts = fun(NewVal, Config) -> KeyStore(ct_opts, Config, NewVal) end.
GetCTSpec = fun(CTOpts) -> GetCfg(spec, CTOpts, undefined) end.
SetCTSpec = fun(NewVal, CTOpts) -> KeyStore(spec, CTOpts, NewVal) end.
GetFullHostname = fun() -> net_adm:localhost() end.
MakeLongNodeB = fun(NodeName, HostName) ->
list_to_binary([NodeName, $@, HostName])
end.
MakeLongNodeS = fun(NodeName, HostName) ->
NodeName ++ "@" ++ HostName
end.
LONG_NODE_NAME_BIN = fun() ->
HostN = GetFullHostname(),
MakeLongNodeB("ct1_apns_erlv3", HostN)
end.
LONG_NODE_NAME_ATOM =
fun() ->
list_to_atom(binary_to_list(LONG_NODE_NAME_BIN()))
end.
ConfigureCTOpts =
fun(Config) ->
DbgMsg("In ConfigureCTOpts~n", []),
DbgMsg("Config IN: ~p~n", [Config]),
CTOpts0 = GetCTOpts(Config),
CTSpec0 = case GetCTSpec(CTOpts0) of
undefined ->
% This MUST be a list of specs!
["apns_erlv3.test.spec"];
S -> S
end,
CTOpts = SetCTSpec(CTSpec0, CTOpts0),
ConfigOut = SetCTOpts(CTOpts, Config),
DbgMsg("Config OUT: ~p~n", [ConfigOut]),
ConfigOut
end.
%% This function generates the test spec file needed by common test.
%% It expects a file to exist, $SPEC_NAME.src, where $SPEC_NAME
%% is defined as returned by ConfigureCTOpts.
%%
%% $SPEC_NAME.src must contain a stanza as follows:
%%
%% {node, ct1, '{{NEEDFULLNODENAME}}'}.
%%
%% This function replaces {{NEEDFULLNODENAME}} with the long node
%% name of the node running rebar3 and saves the file as $SPECNAME.
%%
GenerateTestSpec =
fun(Config0) ->
Template = <<"{{NEEDFULLNODENAME}}">>,
Config = ConfigureCTOpts(Config0),
CTSpec = GetCTSpec(GetCTOpts(Config)),
true = (CTSpec /= undefined),
CTSpecSrc = CTSpec ++ ".src",
Res = case file:read_file(CTSpecSrc) of
{ok, Spec0} ->
true = (binary:match(Spec0, Template) /= nomatch),
Spec = binary:replace(Spec0, Template, LONG_NODE_NAME_BIN()),
ok = file:write_file(CTSpec, Spec),
Config;
Error ->
Msg("Error opening ~s: ~p\n", [CTSpecSrc, Error]),
undefined
end,
DbgMsg("GenerateTestSpec returned ~p\n", [Res]),
Res
end.
UpsertDistNode =
fun(Config, LongNodeName, Cookie) when is_atom(LongNodeName),
is_atom(Cookie) ->
DistNode = {dist_node, [{name, LongNodeName}, {setcookie, Cookie}]},
lists:keystore(dist_node, 1, Config, DistNode)
end.
ConfigurePreHooks = fun(Config0) ->
Config = ConfigureCTOpts(Config0),
CTSpec = GetCTSpec(GetCTOpts(Config)),
true = (CTSpec /= undefined),
Script = "./pre_common_test_hook.sh",
PreHooks = GetPreHooks(Config),
HostN = GetFullHostname(),
NodeName = MakeLongNodeS("ct1_apns_erlv3", HostN),
CTHook = case GetCTHook(PreHooks) of
S when S == undefined orelse
S == Script ->
lists:flatten(
string:join([Script,
NodeName,
CTSpec], " ")
);
S ->
S
end,
PreHooks1 = SetCTHook(CTHook, PreHooks),
SetPreHooks(PreHooks1, Config)
end.
ChangeEdownUrl = fun(Url, Cfg) ->
EDocOpts = KeyStore(top_level_readme,
GetEDocOpts(Cfg),
{"./README.md", Url}),
KeyStore(edoc_opts, Cfg, EDocOpts)
end.
%% If env var is defined, replace edown_target with its contents
ChangeEdownTarget = fun(Target, Cfg) ->
EDocOpts = KeyStore(edown_target,
GetEDocOpts(Cfg),
ValTarget(Target)),
KeyStore(edoc_opts, Cfg, EDocOpts)
end.
%%
%% Funs that run the change function if the environment variable is present.
%%
CfgEdownUrl = fun(Cfg) ->
RunIfEnvVar("EDOWN_TOP_LEVEL_README_URL",
ChangeEdownUrl, Cfg)
end.
CfgEdownTarget = fun(Cfg) ->
RunIfEnvVar("EDOWN_TARGET", ChangeEdownTarget, Cfg)
end.
%% Override edoc '@version' macro value to be current APP_VERSION.
CfgVersion = fun(Cfg) ->
case GetAppVersion() of
Vsn when is_list(Vsn) ->
DbgMsg("APP_VERSION: ~s~n", [Vsn]),
VsnDef = {version, Vsn},
Opts0 = proplists:get_value(edoc_opts, Cfg, []),
Defs = case proplists:get_value(def, Opts0, []) of
Macros when is_list(Macros) ->
[VsnDef | Macros -- [VsnDef]];
{_Name, _Str} = Macro ->
[VsnDef | [Macro] -- [VsnDef]]
end,
Opts = lists:keystore(def, 1, Opts0, {def, Defs}),
lists:keystore(edoc_opts, 1, Cfg,
{edoc_opts, Opts});
undefined ->
Cfg
end
end.
CfgCtOverride =
fun(Cfg) ->
GetEnv("CT_OVERRIDE",
{fun(_) ->
DbgMsg("Configuring pre_hooks", []),
ConfigurePreHooks(Cfg)
end,
fun(_) ->
DbgMsg("CT_OVERRIDE, no ct pre_hooks", []),
Cfg
end})
end.
%%{pre_hooks, [{ct, "./pre_common_test_hook.sh"}]}.
%% Common Test configuration: ensure that the CT long node name
%% uses the current dns host name.
%%{pre_hooks, [{ct, "./pre_common_test_hook.sh"}]}.
%%{ct_opts, [{spec, "apns_erlv3.test.spec"}]}.
CfgDistNode =
fun(Cfg) ->
Cookie = 'apns_erlv3',
UpsertDistNode(Cfg, LONG_NODE_NAME_ATOM(), Cookie)
end.
%% Chain the config change functions
RunChangeFuns =
fun(Config) ->
NC = lists:foldl(fun(F, Cfg) -> F(Cfg) end,
Config,
[GenerateTestSpec, CfgEdownUrl, CfgEdownTarget,
CfgVersion, CfgCtOverride, CfgDistNode]),
DbgMsg("Final config:~n~p~n", [NC]),
NC
end.
RunChangeFuns(CONFIG).