中文 | English
读取 .ini 配置和 template 模板批量生成文件, .ini 中每个 session 对应一个文件(i2f_common 除外)
在 rebar.config 的 plugins 添加 ini2file
{plugins, [
{ini2file, {git, "https://github.com/Gelomen/ini2file.git", {branch, "master"}}}
]}.
rebar3 ini2file gen在 rebar.config 的 provider_hooks 添加配置
{provider_hooks, [
{pre, [
{compile, {ini2file, gen}}
]}
]}.
然后执行
rebar3 compile读取 .ini 文件的字段, 替换模板的内容, 如:
domain的值127.0.0.1替换模板文件中的{{domain}}
路径: ini/my_config.ini
[i2f_common]
domain = 127.0.0.1
[foo]
port = 8080
[bar]
port = 9090
session可以配置与[i2f_common]相同的参数以覆盖公共配置, 且不会影响其他session
如:
[i2f_common]
domain = 127.0.0.1
[foo]
domain = localhost
port = 8080
[bar]
port = 9090
路径: templates/erl.tmpl
%% Generated by plugin `ini2file`, don't edit.
-module({{i2f_file_name}}).
-export([
host/0
]).
host() ->
"{{domain}}:{{port}}".
{plugins, [
{ini2file, {git, "https://github.com/Gelomen/ini2file.git", {branch, "master"}}}
]}.
{ini2file, [
% ini 配置
{ini, [
% {ini_name, ini_path, [tmpl_name]}
{my, "ini/my_config.ini", [erl]} % {配置名字, 配置路径, [模板名字]}
]},
% templates 配置
{templates, [
{erl, [
% 文件名字, 如下配置将得到: `src/my_foo_xxx.erl` 和 `src/my_bar_xxx.erl`
{name, [i2f_ini_name, i2f_session_name, "xxx"]}, % 要拼接文件名字的单词, atom | int | string | bit_string
{suffix, ".erl"}, % 文件后缀
{save_path, "src"}, % 文件保存路径
{tmpl_path, "templates/erl.tmpl"} % 模板路径
]}
]}
]}.
| 参数 | 说明 | 例 |
|---|---|---|
{{i2f_ini_name}} |
ini 的配置名字 |
my |
{{i2f_session_name}} |
.ini 文件里的 session 名字 |
foo |
{{i2f_file_name}} |
要生成的文件名字(不带后缀) | my_foo_xxx |
{{i2f_file_full_name}} |
要生成的文件完整名字(带后缀) | my_foo_xxx.erl |
{{i2f_file_path}} |
要生成的文件路径 | src/my_foo_xxx.erl |