From ceafc52ad6b3c8ef72abdbe92e960a4c8c1bbd6d Mon Sep 17 00:00:00 2001 From: "Zaiming (Stone) Shi" Date: Tue, 18 Apr 2023 19:59:23 +0200 Subject: [PATCH 1/2] refactor: use emqx_utils_ets for ets table creation --- apps/emqx_dashboard/src/emqx_dashboard_desc_cache.erl | 2 +- scripts/merge-config.escript | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/emqx_dashboard/src/emqx_dashboard_desc_cache.erl b/apps/emqx_dashboard/src/emqx_dashboard_desc_cache.erl index 9d8d1905d..b503fed88 100644 --- a/apps/emqx_dashboard/src/emqx_dashboard_desc_cache.erl +++ b/apps/emqx_dashboard/src/emqx_dashboard_desc_cache.erl @@ -36,7 +36,7 @@ init() -> OtherLangDesc0 = filelib:wildcard("desc.*.hocon", WwwStaticDir), OtherLangDesc = lists:map(fun(F) -> filename:join([WwwStaticDir, F]) end, OtherLangDesc0), Files = [EngDesc | OtherLangDesc], - ?MODULE = ets:new(?MODULE, [named_table, public, set, {read_concurrency, true}]), + ok = emqx_utils_ets:new(?MODULE, [public, ordered_set, {read_concurrency, true}]), ok = lists:foreach(fun(F) -> load_desc(?MODULE, F) end, Files). %% @doc Load the description of the configuration items from the file. diff --git a/scripts/merge-config.escript b/scripts/merge-config.escript index b3c214dd7..14ec979f2 100755 --- a/scripts/merge-config.escript +++ b/scripts/merge-config.escript @@ -110,14 +110,14 @@ merge_desc_files_per_lang(Lang) -> BaseConf = <<"">>, Cfgs0 = get_all_desc_files(Lang), Conf = do_merge_desc_files_per_lang(BaseConf, Cfgs0), - OutputFile = case Lang of + OutputFile = case Lang of "en" -> %% en desc will always be in the priv dir of emqx_dashboard "apps/emqx_dashboard/priv/desc.en.hocon"; "zh" -> %% so far we inject zh desc as if it's extracted from dashboard package %% TODO: remove this when we have zh translation moved to dashboard package - "apps/emqx_dashboard/priv/www/static/desc.zh.hocon" + "apps/emqx_dashboard/priv/www/static/desc.zh.hocon" end, ok = filelib:ensure_dir(OutputFile), ok = file:write_file(OutputFile, Conf). From a6d72b178bb32bd90fdf8931b168db74afbebba3 Mon Sep 17 00:00:00 2001 From: "Zaiming (Stone) Shi" Date: Tue, 18 Apr 2023 20:04:22 +0200 Subject: [PATCH 2/2] chore: delete old script split-i19n-files.escript is no longer needed --- scripts/split-i18n-files.escript | 84 -------------------------------- 1 file changed, 84 deletions(-) delete mode 100755 scripts/split-i18n-files.escript diff --git a/scripts/split-i18n-files.escript b/scripts/split-i18n-files.escript deleted file mode 100755 index b9f558925..000000000 --- a/scripts/split-i18n-files.escript +++ /dev/null @@ -1,84 +0,0 @@ -#!/usr/bin/env escript - -%% This script is for one-time use. -%% will be deleted after the migration is done. - --mode(compile). - -main([]) -> - %% we need to parse hocon - %% so we'll just add all compiled libs to path - code:add_pathsz(find_ebin_paths("_build/default/lib/*")), - Files = filelib:wildcard("rel/i18n/*.hocon"), - ok = lists:foreach(fun split_file/1, Files), - ok. - -find_ebin_paths(DirPattern) -> - LibDirs = filelib:wildcard(DirPattern), - lists:filtermap(fun add_ebin/1, LibDirs). - -add_ebin(Dir) -> - EbinDir = filename:join(Dir, "ebin"), - case filelib:is_dir(EbinDir) of - true -> {true, EbinDir}; - false -> false - end. - -split_file(Path) -> - {ok, DescMap} = hocon:load(Path), - [{Module, Descs}] = maps:to_list(DescMap), - try - ok = split(Path, Module, <<"en">>, Descs), - ok = split(Path, Module, <<"zh">>, Descs) - catch - throw : already_done -> - ok - end. - -split(Path, Module, Lang, Fields) when is_map(Fields) -> - split(Path, Module, Lang, maps:to_list(Fields)); -split(Path, Module, Lang, Fields) when is_list(Fields) -> - Split = lists:map(fun({Name, Desc})-> do_split(Path, Name, Lang, Desc) end, Fields), - IoData = [Module, " {\n\n", Split, "}\n"], - %% assert it's a valid HOCON object - {ok, _} = hocon:binary(IoData), - %io:format(user, "~s", [IoData]). - WritePath = case Lang of - <<"en">> -> - Path; - <<"zh">> -> - rename(Path, "zh") - end, - ok = filelib:ensure_dir(WritePath), - ok = file:write_file(WritePath, IoData), - ok. - -rename(FilePath, Lang) -> - Dir = filename:dirname(FilePath), - BaseName = filename:basename(FilePath), - filename:join([Dir, Lang, BaseName]). - -do_split(_Path, _Name, _Lang, #{<<"desc">> := Desc}) when is_binary(Desc) -> - throw(already_done); -do_split(Path, Name, Lang, #{<<"desc">> := Desc} = D) -> - try - Label = maps:get(<<"label">>, D, #{}), - DescL = maps:get(Lang, Desc), - LabelL = maps:get(Lang, Label, undefined), - [fmt([Name, ".desc:\n"], DescL), - fmt([Name, ".label:\n"], LabelL) - ] - catch - C : E : S-> - erlang:raise(C, {Path, Name, E}, S) - end. - - -tq() -> - "\"\"\"". - -fmt(_Key, undefined) -> - []; -fmt(Key, Content) -> - [Key, tq(), Content, tq(), "\n\n"]. -