-
Notifications
You must be signed in to change notification settings - Fork 221
/
rebar.config.script
225 lines (194 loc) · 9.23 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
%%-*- mode: erlang -*-
%% =============================================================================
%% maybe upload coverdata
%% =============================================================================
CONFIG0 = case os:getenv("TRAVIS") of
"true" ->
JobId = os:getenv("TRAVIS_JOB_ID"),
[{coveralls_service_job_id, JobId},
{coveralls_coverdata, "_build/test/cover/*.coverdata"},
{coveralls_service_name , "travis-ci"} | CONFIG];
_ ->
CONFIG
end,
%% =============================================================================
%% Dependencies
%% =============================================================================
PluginsNeededByElixir = [rebar_mix,
{rebar3_elixir_compile,
{git, "https://github.com/barrel-db/rebar3_elixir_compile.git",
{branch, "master"}}}],
HooksNeededByElixir = {provider_hooks, [{pre, [{compile, {ex, compile}}]}]},
OptsNeededByElixir = {elixir_opts, [{env, dev}]},
Kf = fun(K, L) -> {K, V} = lists:keyfind(K, 1, L), V end,
{ElixirDeps, CONFIG1} = case Kf(elixir_deps, CONFIG0) of
false ->{[], CONFIG0};
[] -> {[], CONFIG0};
ElixirDeps0 ->
Plugins = Kf(plugins, CONFIG0),
NewConfig = lists:keydelete(plugins, 1, CONFIG0),
Plugins1 = {plugins, Plugins ++ PluginsNeededByElixir},
{ElixirDeps0,
[Plugins1, HooksNeededByElixir, OptsNeededByElixir | NewConfig]}
end,
PluginCompatWindowsPlatform = fun(Config) ->
PluginsTmp = Kf(plugins, Config),
ConfigTmp = lists:keydelete(plugins, 1, Config),
Plugins2 = {plugins, case os:type() of
{win32, nt} -> PluginsTmp -- [rebar3_run];
_ -> PluginsTmp
end},
CONFIG2 = [Plugins2 | ConfigTmp]
end,
CONFIG2 = PluginCompatWindowsPlatform(CONFIG1),
BaseDeps = Kf(deps, CONFIG2) ++ ElixirDeps,
CloudDeps = BaseDeps ++ Kf(cloud_deps, CONFIG2),
EdgeDeps = BaseDeps ++ Kf(edge_deps, CONFIG2),
%% Make a dep element for rebar.config GitRef should be either {tag, Tag} or {branch, Branch}
MakeDep =
fun({Name, {git, _, _}} = App, _DefaultDepRef) ->
%% alreay a complete ref
App;
(App, DefaultDepRef) ->
{AppName, GitRef} =
case App of
{Name, Pinned} when is_tuple(Pinned) -> {Name, Pinned};
{Name, Tag} when is_list(Tag) -> {Name, {tag, Tag}};
Name when is_atom(Name) -> {Name, DefaultDepRef}
end,
RepoName = string:join(string:tokens(atom_to_list(AppName), "_"), "-"),
URL = "https://github.com/emqx/" ++ RepoName,
{AppName, {git, URL, GitRef}}
end,
MakeDeps = fun(Deps, DefaultDepRef, TestDeps) -> [MakeDep(App, DefaultDepRef) || App <- Deps] ++ TestDeps end,
%% TODO: this is only a temporary workaround in order to be backward compatible
%% The right way is to always pin dependency version in rebar.config
%% Any dependency that can not be tested and released independently
%% (i.e. has to be a part of a emqx release in order to be tested)
%% should not be a dependency but a local application reside in the same repo.
%% (Meaning: emqx should be an umbrella project)
ComparingFun = fun
_Fun([C1|R1], [C2|R2]) when is_list(C1), is_list(C2);
is_integer(C1), is_integer(C2) -> C1 < C2 orelse _Fun(R1, R2);
_Fun([C1|R1], [C2|R2]) when is_integer(C1), is_list(C2) -> _Fun(R1, R2);
_Fun([C1|R1], [C2|R2]) when is_list(C1), is_integer(C2) -> true;
_Fun(_, _) -> false
end,
SortFun = fun(T1, T2) ->
C = fun(T) ->
[case catch list_to_integer(E) of
I when is_integer(I) -> I;
_ -> E
end || E <- re:split(string:sub_string(T, 2), "[.-]", [{return, list}])]
end,
ComparingFun(C(T1), C(T2))
end,
Tag = os:cmd("git describe --abbrev=0 --tags") -- "\n",
LatestTagCommitId = os:cmd(io_lib:format("git rev-parse ~s", [Tag])) -- "\n",
Tags = string:tokens(os:cmd(io_lib:format("git tag -l \"v*\" --points-at ~s", [LatestTagCommitId])), "\n"),
LatestTag = lists:last(lists:sort(SortFun, Tags)),
Branch = case os:getenv("GITHUB_RUN_ID") of
false -> os:cmd("git branch | grep -e '^*' | cut -d' ' -f 2") -- "\n";
_ -> re:replace(os:getenv("GITHUB_REF"), "^refs/heads/|^refs/tags/", "", [global, {return ,list}])
end,
GitRef = case re:run(Branch, "master|^dev/|^hotfix/", [{capture, none}]) of
match -> {branch, Branch};
_ -> {tag, LatestTag}
end,
DefaultDepRef =
case os:getenv("EMQX_DEPS_DEFAULT_VSN") of
false -> GitRef; %% not set
"" -> GitRef; %% set empty
[] -> GitRef; %% set empty
MaybeTag ->
case re:run(MaybeTag, "^[ev0-9\]+\.\[0-9\]+\.*") of
nomatch -> {branch, MaybeTag};
_ -> {tag, MaybeTag}
end
end,
%% =============================================================================
%% Relx configs
%% =============================================================================
GitDescribe = begin
T = case DefaultDepRef of
{tag, EnvTag} -> EnvTag;
_Else -> LatestTag
end,
re:replace(T, "v", "", [{return ,list}])
end,
Relx0 = Kf(relx, CONFIG2),
{release, {_, Vsn0}, RelxBaseApps0} = lists:keyfind(release, 1, Relx0),
Vsn1 = case Vsn0 of
git_describe -> GitDescribe;
Vsn -> Vsn
end,
RelxElixirApps = Kf(elixir_relx_apps, CONFIG2),
RelxBaseApps = RelxBaseApps0 ++ RelxElixirApps,
RelxOverlay = Kf(overlay, Relx0),
RelxCloudApps = RelxBaseApps ++ Kf(cloud_relx_apps, CONFIG2),
RelxEdgeApps = RelxBaseApps ++ Kf(edge_relx_apps, CONFIG2),
RelxCloudOverlay0 = Kf(cloud_relx_overlay, CONFIG2),
RelxEdgeOverlay0 = Kf(edge_relx_overlay, CONFIG2),
RelxCloudOverlay = RelxOverlay ++ RelxCloudOverlay0,
RelxEdgeOverlay = RelxOverlay ++ RelxEdgeOverlay0,
MakeRelx =
fun(Apps, Overlay, Vars) ->
VarFiles = ["vars-" ++ atom_to_list(Var) ++ ".config" || Var <- Vars],
Apps1 = case os:type() of {win32, nt} -> Apps -- [bcrypt]; _Other -> Apps end,
Relx1 = lists:keystore(release, 1, Relx0, {release, {emqx, Vsn1}, Apps1}),
Relx2 = lists:keystore(overlay, 1, Relx1, {overlay, Overlay}),
lists:keystore(overlay_vars, 1, Relx2, {overlay_vars, VarFiles})
end,
Relx = fun(Vars) -> MakeRelx(RelxBaseApps, RelxOverlay, Vars) end,
RelxCloud = fun(Vars) -> MakeRelx(RelxCloudApps, RelxCloudOverlay, Vars) end,
RelxEdge = fun(Vars) -> MakeRelx(RelxEdgeApps, RelxEdgeOverlay, Vars) end,
TestDeps = [ {meck, "0.8.13"} % hex
, {bbmustache, "1.7.0"} % hex
, {emqx_ct_helpers, {git, "https://github.com/emqx/emqx-ct-helpers", {tag, "v1.1.1"}}}
],
%% =============================================================================
%% Profiles
%% =============================================================================
Profiles =
[ {emqx,
[ {deps, MakeDeps(CloudDeps, DefaultDepRef, [])}
, {relx, RelxCloud([cloud, bin])}
, {post_hooks, [{"(linux|darwin|solaris|freebsd|netbsd|openbsd)", compile, "./inject-deps.escript emqx"}]}
]}
, {'emqx-pkg',
[ {deps, MakeDeps(CloudDeps, DefaultDepRef, [])}
, {relx, RelxCloud([cloud, pkg])}
, {post_hooks, [{"(linux|darwin|solaris|freebsd|netbsd|openbsd)", compile, "./inject-deps.escript emqx-pkg"}]}
]}
, {'emqx-edge',
[ {deps, MakeDeps(EdgeDeps, DefaultDepRef, [])}
, {relx, RelxEdge([edge, bin])}
, {post_hooks, [{"(linux|darwin|solaris|freebsd|netbsd|openbsd)", compile, "./inject-deps.escript emqx-edge"}]}
]}
, {'emqx-edge-pkg',
[ {deps, MakeDeps(EdgeDeps, DefaultDepRef, [])}
, {relx, RelxEdge([edge, pkg])}
, {post_hooks, [{"(linux|darwin|solaris|freebsd|netbsd|openbsd)", compile, "./inject-deps.escript emqx-edge-pkg"}]}
]}
],
Deletes = [ deps
, relx
, elixir_deps
, edge_deps
, cloud_deps
, elixir_relx_apps
, edge_relx_apps
, cloud_relx_apps
, cloud_relx_overlay
],
Additions = [{profiles, Profiles}],
CONFIG3 = lists:foldl(fun(K, Acc) -> lists:keydelete(K, 1, Acc) end, CONFIG2, Deletes),
CONFIG4 = lists:foldl(fun({K, V}, Acc) -> lists:keystore(K, 1, Acc, {K, V}) end, CONFIG3, Additions),
FilePath = case os:type() of
{win32, nt} ->
"emqx.rebar.config";
_ ->
"/tmp/emqx.rebar.config"
end,
file:write_file(FilePath, [io_lib:format("~p.\n", [I]) || I <- CONFIG4]),
CONFIG4.