chore(build): inject emqx_vsn to all modules as attribute

This commit is contained in:
Zaiming Shi 2021-02-26 21:43:58 +01:00
parent cac2fe9009
commit c54636b6c2
3 changed files with 46 additions and 4 deletions

View File

@ -11,7 +11,8 @@
{erl_opts, [warn_unused_vars,warn_shadow_vars,warn_unused_import,
warn_obsolete_guard,compressed]}.
{overrides,[{add,[{extra_src_dirs, [{"etc", [{recursive,true}]}]}]}
{overrides,[{add,[ {extra_src_dirs, [{"etc", [{recursive,true}]}]}
, {erl_opts, [{parse_transform, emqx_mod_vsn}]}]}
]}.
{extra_src_dirs, [{"etc", [{recursive,true}]}]}.

View File

@ -52,7 +52,7 @@ test_deps() ->
].
default_compile_opts() ->
[compressed, deterministic, no_debug_info, warnings_as_errors, {parse_transform, mod_vsn}].
[compressed, deterministic, no_debug_info, warnings_as_errors, {parse_transform, emqx_mod_vsn}].
profiles() ->
[ {'emqx', [ {erl_opts, default_compile_opts()}
@ -67,11 +67,11 @@ profiles() ->
, {'emqx-edge-pkg', [ {erl_opts, default_compile_opts()}
, {relx, relx('emqx-edge-pkg')}
]}
, {check, [ {erl_opts, [debug_info, warnings_as_errors, {parse_transform, mod_vsn}]}
, {check, [ {erl_opts, [debug_info, warnings_as_errors, {parse_transform, emqx_mod_vsn}]}
]}
, {test, [ {deps, test_deps()}
, {plugins, test_plugins()}
, {erl_opts, [debug_info, {parse_transform, mod_vsn}] ++ erl_opts_i()}
, {erl_opts, [debug_info, {parse_transform, emqx_mod_vsn}] ++ erl_opts_i()}
, {extra_src_dirs, [{"test", [{recursive,true}]}]}
]}
].
@ -281,6 +281,7 @@ provide_bcrypt_release(ReleaseType) ->
compile_and_load_pase_transforms(Dir) ->
PtFiles =
[ "apps/emqx_rule_engine/src/emqx_rule_actions_trans.erl"
, "src/emqx_mod_vsn.erl"
],
CompileOpts = [verbose,report_errors,report_warnings,return_errors,debug_info],
lists:foreach(fun(PtFile) -> {ok, _Mod} = compile:file(path(Dir, PtFile), CompileOpts) end, PtFiles).

40
src/emqx_mod_vsn.erl Normal file
View File

@ -0,0 +1,40 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2021 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% 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.
%%--------------------------------------------------------------------
%% This module provides a parse_transform to inject emqx version number
%% to all modules as a module attribute.
%% The module attribute is so far only used for beam reload inspection.
-module(emqx_mod_vsn).
-export([parse_transform/2]).
parse_transform(Form, _Opts) ->
case os:getenv("PKG_VSN") of
false -> Form;
Vsn -> trans(Form, {attribute, 1, emqx_vsn, Vsn})
end.
trans(Form, Injection) ->
trans(Form, Injection, []).
trans([], _Injection, Acc) ->
lists:reverse(Acc);
trans([{eof, _} | _] = EOF, _Injection, Acc) ->
lists:reverse(Acc) ++ EOF;
trans([{attribute, _, module, _} = Module | Form], Injection, Acc) ->
lists:reverse(Acc) ++ [Module, Injection | Form];
trans([H | T], Injection, Acc) ->
trans(T, Injection, [H | Acc]).