範本
預設變數
date
:預設為今天的日期,使用世界標準時間,根據 ISO 8601 標準列印(例如,"2014-03-11"
)datetime
:預設為今天的日期和時間,使用世界標準時間,根據 ISO 8601 標準列印(例如,"2014-03-11T16:06:02+00:00"
)。author_name
:預設為"Anonymous"
author_email
:預設為"anonymous@example.org"
apps_dir
:在發布專案中應建立 OTP 應用程式的目錄。預設為"apps/"
。copyright_year
:預設為目前的年份,使用世界標準時間。
全域變數
可以透過編輯 $HOME/.config/rebar3/templates/globals
檔案來設定全域變數
{variables, [
{author_name, "My Name Is A String"},
{copyright_year, "2014-2022", "The year or range of years for copyright"},
{my_custom_var, "hello there"}
]}.
這將允許您為所有範本定義變數。未定義的變數將被忽略並恢復為預設值。
這些變數的覆蓋順序為:預設值 < $HOME/.config/rebar3/templates/globals < 命令列呼叫
。
內建範本
Rebar3 附帶了一些已安裝的範本,可以透過呼叫 rebar3 new
來列出
$ ./rebar3 new
app (built-in): Complete OTP Application structure.
cmake (built-in): Standalone Makefile for building C/C++ in c_src
escript (built-in): Complete escriptized application structure
lib (built-in): Complete OTP Library application (no processes) structure
plugin (built-in): Rebar3 plugin project structure
release (built-in): OTP Release structure for executable programs
任何自定義插件都會顯示為 <插件名稱> (自定義): <描述>
。
可以透過呼叫 rebar3 new help <插件>
來取得每個插件的詳細資訊
$ ./rebar3 new help plugin
plugin:
built-in template
Description: Rebar3 plugin
Variables:
name="myplugin" (Name of the plugin)
desc="A rebar plugin" (Short description of the plugin's purpose)
date="2014-11-10"
datetime="2014-11-10T18:29:41+00:00"
author_name="Anonymous"
author_email="anonymous@example.org"
copyright_year="2014"
apps_dir="apps/" (Directory where applications will be created if needed)
那裡的所有變數都顯示其預設值,並在括號中提供可選的說明。變數也可以在全域範圍內被覆蓋。
可以透過呼叫以下指令來執行範本
$ ./rebar3 new plugin name=demo author_name="Fred H."
...
$ ./rebar3 new plugin demo author_name="Fred H."
...
然後前往 rebar3 為專案建立的目錄。
自定義範本
可以在 $HOME/.config/rebar3/templates/
中新增自定義範本。每個範本至少包含兩個檔案
my_template.erl
:可以有很多這樣的檔案。它們是使用 mustache 範本語法進行變數替換的常規 Erlang 檔案(由 soranoba 的實現 提供)。my_template.template
:稱為*範本索引*,每個可從rebar3
呼叫的範本都有一個。呼叫rebar3 new my_template
時,將會顯示此檔案。此檔案將不同的 mustache 範本檔案重新組合成更具凝聚力的範本。
檔案語法
範本索引
可以使用以下選項
{description, "This template does a thing"}.
{variables, [
{var1, "default value"},
{var2, "default", "explain what this does in help files"},
{app_dir, ".", "The directory where the application goes"}
]}.
{dir, "{{appdir}}/src"}.
{file, "mytemplate_README", "README"}.
{chmod, "README", 8#644}.
{template, "myapp/myapp.app.src", "{{appdir}}/src/{{name}}.app.src"}.
具體來說
description
:接受一個字串,說明範本的用途。variables
:接受兩種形式的變數列表{名稱, 預設字串, 說明字串}
;{名稱, 預設字串}
.
{dir, 可範本化路徑字串}
:建立指定的目錄。可以在路徑名稱中使用變數名稱。{file, 檔案路徑, 目標檔案路徑}
:將檔案原封不動地複製到其目的地。{template, 檔案路徑, 可範本化路徑字串}
:評估指定的範本。檔案路徑
是相對於範本索引的。{chmod, 檔案路徑, 整數}
:使用指定的整數值更改檔案的權限。可以透過8#640
輸入八進位值。
範例
作為範例,我們將建立一個通用測試測試套件的範本。建立目錄結構 ~/.config/rebar3/templates/
,然後進入該目錄。
我們將從範本的索引開始,稱為 ct_suite.template
{description, "A basic Common Test suite for an OTP application"}.
{variables, [
{name, "suite", "Name of the suite, prepended to the standard _SUITE suffix"}
]}.
{dir, "test"}.
{template, "ct_suite.erl", "test/{{name}}_SUITE.erl"}.
這會告知 Rebar3 建立測試目錄並評估 mustache 範本。所有路徑都是相對於目前工作目錄的。
讓我們建立範本檔案
-module({{name}}_SUITE).
-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl"). % Eunit macros for convenience
-export([all/0
,groups/0
%,init_per_suite/1, end_per_suite/1
%,init_per_group/2, end_per_group/2
,init_per_testcase/2, end_per_testcase/2
]).
-export([fail/1]).
all() -> [fail].
groups() -> [].
init_per_testcase(_Name, Config) -> Config.
end_per_testcase(_Name, _Config) -> ok.
fail(_Config) ->
?assert(false).
這個檔案只對名稱進行非常簡單的變數替換(使用 {{name}}
),這就是它所需要的全部。
讓我們進入您現有的任何專案並嘗試一下
$ ./rebar3 new
app (built-in): OTP Application
ct_suite (custom): A basic Common Test suite for an OTP application
lib (built-in): OTP Library application (no processes)
release (built-in): OTP Release structure for executable programs
plugin (built-in): Rebar3 plugin
第一行顯示我們的 ct_suite
範本已被偵測到並且可以使用。
讓我們看看詳細資訊
$ ./rebar3 new help ct_suite
ct_suite:
custom template (/home/ferd/.config/rebar3/templates/ct_suite.template)
Description: A basic Common Test suite for an OTP application
Variables:
name="suite" (Name of the suite, prepended to the standard _SUITE suffix)
date="2014-11-10"
datetime="2014-11-10T18:46:33+00:00"
author_name="Anonymous"
author_email="anonymous@example.org"
copyright_year="2014"
apps_dir="apps/" (Directory where applications will be created if needed)
來自變數和描述的文件都已到位。要套用範本,請前往您的任何 OTP 應用程式的頂層目錄
$ ./rebar3 new ct_suite demo
===> Writing test/demo_SUITE.erl
您將會看到程式碼已到位。
插件範本
插件可以附帶自己的範本,然後與其他範本一起列出。結構與 ~/.config/rebar3/templates/
中的結構完全相同,只是它們必須放在插件的 priv/
目錄中。