forked from SilentCircle/scpf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebar.config.script
399 lines (353 loc) · 14.5 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
%% If the environment variable SCPF_OVERRIDE_REL exists, it is expected to be set to
%% a valid profile name in rebar.config. If that profile name doesn't exist,
%% CONFIG will remain unchanged.
%%
%% If the profile does exist, replace the version in the relx release tuple
%% with the version in APP_VERSION, but only if semantic versioning (semver) is
%% being used. This requires a bit of legerdemain because the default release
%% tuple must be used, and overridden by the profile-specific release tuple if one
%% exists.
%% REMARKS
%%
%% Sorry for all the weird functions named like AndThen and OrElse. I've been
%% playing with Rust and Haskell, and, well...
%% If EnvVar exists and has some content, call UserFun(Content, UserArg)
%% and return its result.
%% Otherwise return UserArg unchanged.
RunIfEnvVar =
fun(EnvVar, UserFun, UserArg) ->
case os:getenv(EnvVar) of
false -> UserArg;
[] -> UserArg;
EnvVal -> UserFun(EnvVal, UserArg)
end
end.
Dbg = RunIfEnvVar("REBAR_EXTRA_DBG",
fun(_EnvVal, _Arg) -> true end,
false).
DbgDo = fun(F) when is_function(F, 0) ->
Dbg andalso F()
end.
Msg = fun(Fmt, Args) ->
io:format(standard_error, Fmt, Args),
true
end.
MsgFun = fun(Fmt, Args) ->
fun() -> Msg(Fmt, Args) end
end.
Dmsg = fun(Fmt, Args) -> DbgDo(MsgFun(Fmt, Args)) end.
%% Expect {'Some', X} and return X.
Some = fun({'Some', X}) ->
X;
('None') ->
erlang:error("Expected Some, got None")
end.
%% Lift value into Some monad. If already monad, leave unchanged.
Return = fun({'Some', _} = X) -> X;
('None') -> 'None';
(undefined) -> 'None';
(Val) -> {'Some', Val}
end.
%% Return Fun(Val) if passed {'Some', Val}, else return 'None'.
AndThen = fun({'Some', Val}, Fun) when is_function(Fun, 1) -> Fun(Val);
('None', _) -> 'None'
end.
%% Return Fun() if passed 'None', else return {'Some', Val}.
OrElse = fun({'Some', _Val}=S, _) -> S;
('None', Fun) when is_function(Fun, 0) -> Fun()
end.
%% Return X unchanged.
Identity = fun(X) -> X end.
GetVal = fun(K, PL) ->
Return(proplists:get_value(K, PL))
end.
GetRel = fun(PL) ->
Res = case lists:keysearch(release, 1, PL) of
{value, V} -> V;
_ -> undefined
end,
Return(Res)
end.
SetVal = fun(K, Tuple, PL) -> Return(lists:keystore(K, 1, PL, Tuple)) end.
SetProp = fun(K, V, PL) -> SetVal(K, {K, V}, PL) end.
%% Get app version from file or return default.
APP_VERSION =
fun(DefVsn) ->
case file:read_file("APP_VERSION") of
{ok, Vsn} ->
string:strip(binary_to_list(Vsn), right, $\n);
_ ->
DefVsn
end
end.
MaybeAppVersion = fun() -> Return(APP_VERSION(undefined)) end.
%% Replace release if semver is the version. Leave as semver if APP_VERSION
%% cannot be read.
ReplaceRel =
fun(R) ->
R1 = fun({release, {RelName, semver}, Apps}) ->
{release, {RelName, APP_VERSION(semver)}, Apps};
(_) ->
undefined
end,
Return(R1(R))
end.
%% Replace the relx config if it's there, it contains a release tuple, and
%% the tuple was semantically versioned.
ReplaceRelx =
fun(PL) ->
AndThen(GetVal(relx, PL),
fun(Relx0) ->
AndThen(GetRel(Relx0),
fun(Rel0) ->
AndThen(ReplaceRel(Rel0),
fun({release, {_, semver}, _}=Rel) ->
Relx = SetVal(release, Rel, Relx0),
SetProp(relx, Some(Relx), PL);
(Rel) ->
Msg("Cannot replace relx config ~p", [Rel]),
PL
end)
end)
end)
end.
ReplOverrideProfile =
fun(Profiles, Vsn) ->
Dmsg("ReplOverrideProfile, Profiles: ~p, Vsn: ~p\n", [Profiles, Vsn]),
AndThen(GetVal(Vsn, Profiles),
fun(Prof) ->
Dmsg("ReplOverrideProfile, Prof: ~p\n", [Prof]),
AndThen(ReplaceRelx(Prof),
fun(NewProf) ->
Dmsg("ReplOverrideProfile, "
"NewProf: ~p\n", [NewProf]),
SetProp(Vsn, Profiles, NewProf)
end)
end)
end.
ReplOverrideVsn =
fun(C0, Vsn) ->
AndThen(GetVal(profiles, C0),
fun(Profiles) ->
AndThen(ReplOverrideProfile(Profiles, Vsn),
fun(C) ->
AndThen(ReplaceRelx(C), Return)
end)
end)
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.
GetMacros = fun(EDocOpts) -> proplists:get_value(def, EDocOpts, []) end.
SetMacros = fun(NewVal, EDocOpts) -> KeyStore(def, EDocOpts, NewVal) end.
AddMacro = fun(NewDef, EDocOpts) ->
NewMacros = case GetMacros(EDocOpts) of
Macros when is_list(Macros) ->
[NewDef | Macros -- [NewDef]];
{_Name, _Str} = Def ->
[NewDef | [Def] -- [NewDef]]
end,
SetMacros(NewMacros, EDocOpts)
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.
LONG_NODE_NAME_BIN = fun() ->
HostN = GetFullHostname(),
MakeLongNodeB("ct1_scpf", HostN)
end.
LONG_NODE_NAME_ATOM =
fun() ->
list_to_atom(binary_to_list(LONG_NODE_NAME_BIN()))
end.
ConfigureCTOpts =
fun(Config) ->
CTOpts0 = GetCTOpts(Config),
CTSpec0 = case GetCTSpec(CTOpts0) of
undefined ->
"scpf.test.spec";
S ->
S
end,
CTOpts = SetCTSpec(CTSpec0, CTOpts0),
SetCTOpts(CTOpts, Config)
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),
Return(Config);
Error ->
Msg("Error opening ~s: ~p\n", [CTSpecSrc, Error]),
Return(undefined)
end,
Dmsg("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}]},
NewConfig = lists:keystore(dist_node, 1, Config, DistNode),
Return(NewConfig)
end.
%% Return {'Some', 'release'} if env var SCPF_OVERRIDE_REL exists, using
%% the value of the env var converted to atom as the release.
%% Otherwise return 'None'.
SCPF_OVERRIDE_REL =
fun() ->
Res = Return(case os:getenv("SCPF_OVERRIDE_REL") of
false -> undefined;
Str -> list_to_atom(Str)
end),
Dmsg("SCPF_OVERRIDE_REL returned ~p\n", [Res]),
Res
end.
%% Return {'Some', NewConfig} if SCPF_OVERRIDE_REL env var exists, 'None'
%% otherwise.
MaybeOverride =
fun(Cfg) ->
Res = AndThen(SCPF_OVERRIDE_REL(),
fun(Vsn) when is_atom(Vsn) ->
Dmsg("MaybeOverride, Vsn: ~p\n", [Vsn]),
OrElse(ReplOverrideVsn(Cfg, Vsn),
fun() ->
Dmsg("MaybeOverride, OrElse1\n", []),
OrElse(ReplaceRelx(Cfg),
fun() ->
Res = Return(Identity(Cfg)),
Dmsg("MaybeOverride OrElse2: ~p\n", [Res]),
Res
end)
end)
end),
Dmsg("MaybeOverride returned ~p\n", [Res]),
Res
end.
EDOWN_TARGET =
fun() ->
Return(case os:getenv("EDOWN_TARGET") of
false -> undefined;
Str -> Str
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.
%% If env var is defined, replace edown_target with its contents.
%% Return {'Some', NewCfg} if changed, 'None' otherwise.
ConfigEdownTarget =
fun(Cfg) ->
AndThen(EDOWN_TARGET(),
fun(Target) ->
EDocOpts = KeyStore(edown_target,
GetEDocOpts(Cfg),
ValTarget(Target)),
Return(KeyStore(edoc_opts, Cfg, EDocOpts))
end)
end.
EDOWN_TOP_LEVEL_README_URL =
fun() ->
Return(case os:getenv("EDOWN_TOP_LEVEL_README_URL") of
E when E == false orelse E == [] ->
undefined;
URL ->
URL
end)
end.
%% If env var is defined, replace top_level_readme with its contents
%% and return {'Some', NewCfg}, else return 'None'.
ConfigEdownUrl =
fun(Cfg) ->
AndThen(EDOWN_TOP_LEVEL_README_URL(),
fun(URL) ->
EDocOpts = KeyStore(top_level_readme,
GetEDocOpts(Cfg),
{"./README.md", URL}),
Return(KeyStore(edoc_opts, Cfg, EDocOpts))
end)
end.
%% Override edoc '@version' macro value to be current APP_VERSION.
ConfigEdocVsn =
fun(Cfg) ->
AndThen(MaybeAppVersion(),
fun(Vsn) when is_list(Vsn) ->
Return(KeyStore(edoc_opts, Cfg,
AddMacro({version, Vsn},
GetEDocOpts(Cfg))))
end)
end.
ConfigDistNode =
fun(Cfg) ->
UpsertDistNode(Cfg, LONG_NODE_NAME_ATOM(), 'scpf')
end.
%% Apply chain of modifications to CONFIG
{CONFIG1, _} = lists:foldl(
fun(ModCfg, {Cfg, N}) when is_function(ModCfg, 1) ->
Dmsg("Old Cfg ~B: ~p~n", [N, Cfg]),
ModdedCfg = ModCfg(Cfg),
Dmsg("Modded Cfg ~B: ~p~n", [N, ModdedCfg]),
Acc = case ModdedCfg of
{'Some', NewCfg} ->
Dmsg("New Cfg ~B: ~p~n", [N, NewCfg]),
NewCfg;
'None' ->
Cfg
end,
{Acc, N + 1}
end, {CONFIG, 1}, [
GenerateTestSpec,
MaybeOverride,
ConfigEdownTarget,
ConfigEdownUrl,
ConfigEdocVsn,
ConfigDistNode
]
).
CONFIG1.