merge-config.escript 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env escript
  2. %% This script reads up emqx.conf and split the sections
  3. %% and dump sections to separate files.
  4. %% Sections are grouped between CONFIG_SECTION_BGN and
  5. %% CONFIG_SECTION_END pairs
  6. %%
  7. %% NOTE: this feature is so far not used in opensource
  8. %% edition due to backward-compatibility reasons.
  9. -mode(compile).
  10. -define(APPS, ["emqx", "emqx_dashboard", "emqx_authz"]).
  11. main(_) ->
  12. {ok, BaseConf} = file:read_file("apps/emqx_conf/etc/emqx_conf.conf"),
  13. Cfgs = get_all_cfgs("apps/"),
  14. Conf = [
  15. merge(BaseConf, Cfgs),
  16. io_lib:nl()
  17. ],
  18. ok = file:write_file("apps/emqx_conf/etc/emqx.conf.all", Conf),
  19. merge_desc_files_per_lang("en"),
  20. %% TODO: remove this when we have zh translation moved to dashboard package
  21. merge_desc_files_per_lang("zh").
  22. merge(BaseConf, Cfgs) ->
  23. Confs = [BaseConf | lists:map(fun read_conf/1, Cfgs)],
  24. infix(lists:filter(fun(I) -> iolist_size(I) > 0 end, Confs), [io_lib:nl(), io_lib:nl()]).
  25. read_conf(CfgFile) ->
  26. case filelib:is_regular(CfgFile) of
  27. true ->
  28. {ok, Bin1} = file:read_file(CfgFile),
  29. string:trim(Bin1, both);
  30. false ->
  31. <<>>
  32. end.
  33. infix([], _With) -> [];
  34. infix([One], _With) -> [One];
  35. infix([H | T], With) -> [H, With, infix(T, With)].
  36. get_all_cfgs(Root) ->
  37. Apps0 = filelib:wildcard("*", Root) -- ["emqx_machine", "emqx_conf"],
  38. Apps1 = (Apps0 -- ?APPS) ++ lists:reverse(?APPS),
  39. Dirs = [filename:join([Root, App]) || App <- Apps1],
  40. lists:foldl(fun get_cfgs/2, [], Dirs).
  41. get_all_cfgs(Dir, Cfgs) ->
  42. Fun = fun(E, Acc) ->
  43. Path = filename:join([Dir, E]),
  44. get_cfgs(Path, Acc)
  45. end,
  46. lists:foldl(Fun, Cfgs, filelib:wildcard("*", Dir)).
  47. get_cfgs(Dir, Cfgs) ->
  48. case filelib:is_dir(Dir) of
  49. false ->
  50. Cfgs;
  51. _ ->
  52. Files = filelib:wildcard("*", Dir),
  53. case lists:member("etc", Files) of
  54. false ->
  55. try_enter_child(Dir, Files, Cfgs);
  56. true ->
  57. EtcDir = filename:join([Dir, "etc"]),
  58. %% the conf name must start with emqx
  59. %% because there are some other conf, and these conf don't start with emqx
  60. Confs = filelib:wildcard("emqx*.conf", EtcDir),
  61. NewCfgs = [filename:join([EtcDir, Name]) || Name <- Confs],
  62. try_enter_child(Dir, Files, NewCfgs ++ Cfgs)
  63. end
  64. end.
  65. try_enter_child(Dir, Files, Cfgs) ->
  66. case lists:member("src", Files) of
  67. false ->
  68. Cfgs;
  69. true ->
  70. get_all_cfgs(filename:join([Dir, "src"]), Cfgs)
  71. end.
  72. %% Desc files merge is for now done locally in emqx.git repo for all languages.
  73. %% When zh and other languages are moved to a separate repo,
  74. %% we will only merge the en files.
  75. %% The file for other languages will be merged in the other repo,
  76. %% the built as a part of the dashboard package,
  77. %% finally got pulled at build time as a part of the dashboard package.
  78. merge_desc_files_per_lang(Lang) ->
  79. BaseConf = <<"">>,
  80. Cfgs0 = get_all_desc_files(Lang),
  81. Conf = do_merge_desc_files_per_lang(BaseConf, Cfgs0),
  82. OutputFile = case Lang of
  83. "en" ->
  84. %% en desc will always be in the priv dir of emqx_dashboard
  85. "apps/emqx_dashboard/priv/desc.en.hocon";
  86. "zh" ->
  87. %% so far we inject zh desc as if it's extracted from dashboard package
  88. %% TODO: remove this when we have zh translation moved to dashboard package
  89. "apps/emqx_dashboard/priv/www/static/desc.zh.hocon"
  90. end,
  91. ok = filelib:ensure_dir(OutputFile),
  92. ok = file:write_file(OutputFile, Conf).
  93. do_merge_desc_files_per_lang(BaseConf, Cfgs) ->
  94. lists:foldl(
  95. fun(CfgFile, Acc) ->
  96. case filelib:is_regular(CfgFile) of
  97. true ->
  98. {ok, Bin1} = file:read_file(CfgFile),
  99. [Acc, io_lib:nl(), Bin1];
  100. false -> Acc
  101. end
  102. end, BaseConf, Cfgs).
  103. get_all_desc_files(Lang) ->
  104. Dir =
  105. case Lang of
  106. "en" ->
  107. filename:join(["rel", "i18n"]);
  108. "zh" ->
  109. %% TODO: remove this when we have zh translation moved to dashboard package
  110. filename:join(["rel", "i18n", "zh"])
  111. end,
  112. Files = filelib:wildcard("*.hocon", Dir),
  113. lists:map(fun(Name) -> filename:join([Dir, Name]) end, Files).