Преглед изворни кода

feat: move *i18n.conf from etc to i18n dir

Zhongwen Deng пре 3 година
родитељ
комит
700c2cfb39

+ 1 - 0
.gitignore

@@ -1,4 +1,5 @@
 .eunit
+*.conf.all
 test-data/
 deps
 !deps/.placeholder

+ 15 - 19
apps/emqx_conf/src/emqx_conf.erl

@@ -25,7 +25,7 @@
 -export([update/3, update/4]).
 -export([remove/2, remove/3]).
 -export([reset/2, reset/3]).
--export([dump_schema/1, dump_schema/2]).
+-export([dump_schema/1, dump_schema/3]).
 -export([schema_module/0]).
 
 %% for rpc
@@ -133,41 +133,41 @@ reset(Node, KeyPath, Opts) ->
 %% @doc Called from build script.
 -spec dump_schema(file:name_all()) -> ok.
 dump_schema(Dir) ->
-    dump_schema(Dir, emqx_conf_schema).
+    I18nFile = emqx:etc_file("i18n.conf"),
+    dump_schema(Dir, emqx_conf_schema, I18nFile).
 
-dump_schema(Dir, SchemaModule) ->
-    PrivDir = filename:dirname(filename:dirname(Dir)),
+dump_schema(Dir, SchemaModule, I18nFile) ->
     lists:foreach(
         fun(Lang) ->
-            gen_config_md(Dir, PrivDir, SchemaModule, Lang),
-            gen_hot_conf_schema_json(Dir, PrivDir, Lang)
+            gen_config_md(Dir, I18nFile, SchemaModule, Lang),
+            gen_hot_conf_schema_json(Dir, I18nFile, Lang)
         end,
         [en, zh]
     ),
-    gen_schema_json(Dir, PrivDir, SchemaModule).
+    gen_schema_json(Dir, I18nFile, SchemaModule).
 
 %% for scripts/spellcheck.
-gen_schema_json(Dir, PrivDir, SchemaModule) ->
+gen_schema_json(Dir, I18nFile, SchemaModule) ->
     SchemaJsonFile = filename:join([Dir, "schema.json"]),
     io:format(user, "===< Generating: ~s~n", [SchemaJsonFile]),
-    Opts = #{desc_file => i18n_file(PrivDir), lang => "en"},
+    Opts = #{desc_file => I18nFile, lang => "en"},
     JsonMap = hocon_schema_json:gen(SchemaModule, Opts),
     IoData = jsx:encode(JsonMap, [space, {indent, 4}]),
     ok = file:write_file(SchemaJsonFile, IoData).
 
-gen_hot_conf_schema_json(Dir, PrivDir, Lang) ->
-    emqx_dashboard:init_i18n(i18n_file(PrivDir), Lang),
+gen_hot_conf_schema_json(Dir, I18nFile, Lang) ->
+    emqx_dashboard:init_i18n(I18nFile, Lang),
     JsonFile = "hot-config-schema-" ++ atom_to_list(Lang) ++ ".json",
     HotConfigSchemaFile = filename:join([Dir, JsonFile]),
     io:format(user, "===< Generating: ~s~n", [HotConfigSchemaFile]),
     ok = gen_hot_conf_schema(HotConfigSchemaFile),
     emqx_dashboard:clear_i18n().
 
-gen_config_md(Dir, PrivDir, SchemaModule, Lang0) ->
+gen_config_md(Dir, I18nFile, SchemaModule, Lang0) ->
     Lang = atom_to_list(Lang0),
     SchemaMdFile = filename:join([Dir, "config-" ++ Lang ++ ".md"]),
     io:format(user, "===< Generating: ~s~n", [SchemaMdFile]),
-    ok = gen_doc(SchemaMdFile, SchemaModule, PrivDir, Lang).
+    ok = gen_doc(SchemaMdFile, SchemaModule, I18nFile, Lang).
 
 %% @doc return the root schema module.
 -spec schema_module() -> module().
@@ -182,13 +182,12 @@ schema_module() ->
 %%--------------------------------------------------------------------
 
 -spec gen_doc(file:name_all(), module(), file:name_all(), string()) -> ok.
-gen_doc(File, SchemaModule, EtcDir, Lang) ->
+gen_doc(File, SchemaModule, I18nFile, Lang) ->
     Version = emqx_release:version(),
     Title = "# " ++ emqx_release:description() ++ " " ++ Version ++ " Configuration",
     BodyFile = filename:join([code:lib_dir(emqx_conf), "etc", "emqx_conf.md"]),
     {ok, Body} = file:read_file(BodyFile),
-    DescFile = i18n_file(EtcDir),
-    Opts = #{title => Title, body => Body, desc_file => DescFile, lang => Lang},
+    Opts = #{title => Title, body => Body, desc_file => I18nFile, lang => Lang},
     Doc = hocon_schema_md:gen(SchemaModule, Opts),
     file:write_file(File, Doc).
 
@@ -446,6 +445,3 @@ to_bin(Boolean) when is_boolean(Boolean) -> Boolean;
 to_bin(Atom) when is_atom(Atom) -> atom_to_binary(Atom, utf8);
 to_bin(X) ->
     X.
-
-i18n_file(EtcDir) ->
-    filename:join([EtcDir, "i18n.conf"]).

apps/emqx_dashboard/etc/emqx_dashboard_i18n.conf → apps/emqx_dashboard/i18n/emqx_dashboard_i18n.conf


+ 2 - 1
build

@@ -82,7 +82,8 @@ make_doc() {
     # shellcheck disable=SC2086
     erl -noshell -pa $libs_dir1 $libs_dir2 $libs_dir3 -eval \
         "Dir = filename:join(['_build', '${PROFILE}', lib, emqx_dashboard, priv, www, static]), \
-         ok = emqx_conf:dump_schema(Dir, $SCHEMA_MODULE), \
+         I18nFile = filename:join(['_build', '${PROFILE}', lib, emqx_dashboard, etc, 'i18n.conf.all']), \
+         ok = emqx_conf:dump_schema(Dir, $SCHEMA_MODULE, I18nFile), \
          halt(0)."
 }
 

+ 4 - 3
mix.exs

@@ -341,12 +341,13 @@ defmodule EMQXUmbrella.MixProject do
       "apps/emqx/etc/certs",
       Path.join(etc, "certs")
     )
+
     # required by emqx_dashboard
-     Mix.Generator.copy_file(
-      "apps/emqx_dashboard/etc/i18n.conf",
+    Mix.Generator.copy_file(
+      "apps/emqx_dashboard/etc/i18n.conf.all",
       Path.join(etc, "i18n.conf"),
       force: overwrite?
-     )
+    )
 
     # this is required by the produced escript / nodetool
     Mix.Generator.copy_file(

+ 1 - 2
scripts/merge-config.escript

@@ -62,8 +62,7 @@ get_cfgs(Dir, Cfgs) ->
                     %% the conf name must start with emqx
                     %% because there are some other conf, and these conf don't start with emqx
                     Confs = filelib:wildcard("emqx*.conf", EtcDir),
-                    Confs1 = lists:filter(fun(N) -> string:find(N, "i18n") =:= nomatch end, Confs),
-                    NewCfgs = [filename:join([EtcDir, Name]) || Name <- Confs1],
+                    NewCfgs = [filename:join([EtcDir, Name]) || Name <- Confs],
                     try_enter_child(Dir, Files, NewCfgs ++ Cfgs)
             end
     end.

+ 4 - 6
scripts/merge-i18n.escript

@@ -3,7 +3,7 @@
 -mode(compile).
 
 main(_) ->
-    {ok, BaseConf} = file:read_file("apps/emqx_dashboard/etc/emqx_dashboard_i18n.conf"),
+    {ok, BaseConf} = file:read_file("apps/emqx_dashboard/i18n/emqx_dashboard_i18n.conf"),
 
     Cfgs = get_all_cfgs("apps/"),
     Conf = [merge(BaseConf, Cfgs),
@@ -40,14 +40,12 @@ get_cfgs(Dir, Cfgs) ->
             Cfgs;
         _ ->
             Files = filelib:wildcard("*", Dir),
-            case lists:member("etc", Files) of
+            case lists:member("i18n", Files) of
                 false ->
                     try_enter_child(Dir, Files, Cfgs);
                 true ->
-                    EtcDir = filename:join([Dir, "etc"]),
-                    %% the conf name must start with emqx
-                    %% because there are some other conf, and these conf don't start with emqx
-                    Confs = filelib:wildcard("emqx*_i18n.conf", EtcDir),
+                    EtcDir = filename:join([Dir, "i18n"]),
+                    Confs = filelib:wildcard("*.conf", EtcDir),
                     NewCfgs = [filename:join([EtcDir, Name]) || Name <- Confs],
                     try_enter_child(Dir, Files, NewCfgs ++ Cfgs)
             end