merge-i18n.escript 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env escript
  2. -mode(compile).
  3. main(_) ->
  4. BaseConf = <<"">>,
  5. Cfgs0 = get_all_cfgs("apps/"),
  6. Cfgs1 = get_all_cfgs("lib-ee/"),
  7. Conf0 = merge(BaseConf, Cfgs0),
  8. Conf = [merge(Conf0, Cfgs1),
  9. io_lib:nl()
  10. ],
  11. OutputFile = "apps/emqx_dashboard/priv/i18n.conf",
  12. ok = filelib:ensure_dir(OutputFile),
  13. ok = file:write_file(OutputFile, Conf).
  14. merge(BaseConf, Cfgs) ->
  15. lists:foldl(
  16. fun(CfgFile, Acc) ->
  17. case filelib:is_regular(CfgFile) of
  18. true ->
  19. {ok, Bin1} = file:read_file(CfgFile),
  20. [Acc, io_lib:nl(), Bin1];
  21. false -> Acc
  22. end
  23. end, BaseConf, Cfgs).
  24. get_all_cfgs(Root) ->
  25. Apps = filelib:wildcard("*", Root) -- ["emqx_machine"],
  26. Dirs = [filename:join([Root, App]) || App <- Apps],
  27. lists:foldl(fun get_cfgs/2, [], Dirs).
  28. get_all_cfgs(Dir, Cfgs) ->
  29. Fun = fun(E, Acc) ->
  30. Path = filename:join([Dir, E]),
  31. get_cfgs(Path, Acc)
  32. end,
  33. lists:foldl(Fun, Cfgs, filelib:wildcard("*", Dir)).
  34. get_cfgs(Dir, Cfgs) ->
  35. case filelib:is_dir(Dir) of
  36. false ->
  37. Cfgs;
  38. _ ->
  39. Files = filelib:wildcard("*", Dir),
  40. case lists:member("i18n", Files) of
  41. false ->
  42. try_enter_child(Dir, Files, Cfgs);
  43. true ->
  44. EtcDir = filename:join([Dir, "i18n"]),
  45. Confs = filelib:wildcard("*.conf", EtcDir),
  46. NewCfgs = [filename:join([EtcDir, Name]) || Name <- Confs],
  47. try_enter_child(Dir, Files, NewCfgs ++ Cfgs)
  48. end
  49. end.
  50. try_enter_child(Dir, Files, Cfgs) ->
  51. case lists:member("src", Files) of
  52. false ->
  53. Cfgs;
  54. true ->
  55. get_all_cfgs(filename:join([Dir, "src"]), Cfgs)
  56. end.