-
Notifications
You must be signed in to change notification settings - Fork 1
/
rebar.config.script
143 lines (125 loc) · 5.77 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
%%% ==========================================================================
%%% Copyright 2015 Silent Circle
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%% http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%% ==========================================================================
%% If EnvVar exists and has some content, call UserFun(Content, UserArg)
%% and return its result.
%% Otherwise return UserArg unchanged.
RunIfEnvVar = fun(EnvVar, UserFun, Default) ->
case os:getenv(EnvVar) of
X when X =:= false; X == [] ->
Default;
EnvVal ->
UserFun(EnvVal, Default)
end
end.
IsDbg = RunIfEnvVar("REBAR_EXTRA_DBG", fun(_EnvVal, _Def) -> true end, false).
DbgDo = fun(F) when is_function(F, 0) -> IsDbg andalso F() end.
DbgMsg = fun(Fmt, Args) -> DbgDo(fun() -> io:format(Fmt, Args) end) end.
DbgMsg("CONFIG: ~p~n", [CONFIG]).
%%
%% Helper functions
%%
%% 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.
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() ->
case os:getenv("APP_VERSION") of
X when X =:= false;
X =:= [] -> GetFileAppVersion();
Val -> StrClean(Val)
end
end.
ValTarget = fun(Target) when is_list(Target) ->
Valid = ['github', 'gitlab', 'stash'],
TargAtom = list_to_atom(Target),
lists:member(TargAtom, Valid) orelse
throw({bad_edown_target, Target}),
TargAtom
end.
%%
%% Funs that modify the config. These are conditionally run using RunIfEnvVar.
%% Funs take two parameters: the first is the value of the environment
%% variable, the second the rebar config.
%% Funs return the second parameter, possibly modified.
%%
ChangeEdownTarget = fun(Target, Cfg) ->
NewTarget = {edown_target, ValTarget(Target)},
Opts = proplists:get_value(edoc_opts, Cfg, []),
NewOpts = lists:keystore(edown_target, 1, Opts,
NewTarget),
lists:keystore(edoc_opts, 1, Cfg,
{edoc_opts, NewOpts})
end.
ChangeEdownUrl = fun(Url, Cfg) ->
Opts0 = proplists:get_value(edoc_opts, Cfg, []),
NewReadme = {"./README.md", Url},
Opts = lists:keystore(top_level_readme, 1, Opts0,
{top_level_readme, NewReadme}),
lists:keystore(edoc_opts, 1, Cfg, {edoc_opts, Opts})
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
undefined ->
Cfg;
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})
end
end.
%% Finally, chain the config change functions
NC = lists:foldl(fun(F, Cfg) -> F(Cfg) end, CONFIG,
[CfgEdownUrl, CfgEdownTarget, CfgVersion]),
DbgMsg("~p~n", [NC]),
NC.
%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 ft=erlang et