-
Notifications
You must be signed in to change notification settings - Fork 1
/
rebar.config.script
153 lines (134 loc) · 5.08 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
%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 ft=erlang et
%% 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) ->
Dbg andalso F()
end.
%%
%% 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} ->
DbgDo(fun() -> io:format("File vsn: ~p~n", [BVsn]) end),
S = StrClean(binary_to_list(BVsn)),
DbgDo(fun() -> io:format("Cleaned vsn: ~p~n", [S]) end),
S;
_ ->
undefined
end
end.
GetAppVersion = fun() ->
case os:getenv("APP_VERSION") of
X when X =:= false;
X =:= [] -> GetFileAppVersion();
Val -> StrClean(Val)
end
end.
CheckDeps =
fun(PL, Filename) ->
DbgDo(fun() -> io:format("CheckDeps~n", []) end),
case lists:keyfind(deps, 1, PL) of
{deps, Deps} when is_list(Deps)->
DbgDo(fun() -> io:format("Got deps: ~p~n", [Deps]) end),
Deps;
false ->
io:format("problem with deps in ~s~n", [Filename]),
[]
end
end.
GetDeps =
fun(Filename) ->
DbgDo(fun() -> io:format("Consult ~s~n", [Filename]) end),
case file:consult(Filename) of
{ok, PL} ->
DbgDo(fun() -> io:format("PL: ~p~n", [PL]) end),
CheckDeps(PL, Filename);
{error, Err} ->
io:format("~s: ~s~n", [Filename, file:format(Err)]),
[]
end
end.
ValTarget =
fun(Target) ->
TargAtom = list_to_atom(Target),
lists:member(TargAtom, ['github', 'gitlab', 'stash']) 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.
%%
CfgEdownTarget = fun(Cfg) ->
RunIfEnvVar("EDOWN_TARGET", ChangeEdownTarget, Cfg)
end.
CfgEdownUrl = fun(Cfg) ->
RunIfEnvVar("EDOWN_TOP_LEVEL_README_URL",
ChangeEdownUrl, 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) ->
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]),
DbgDo(fun() -> file:write_file("CONFIG", io_lib:format("~p~n", [NC])) end),
NC.