You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
6.9 KiB

  1. # Templates #
  2. - [Default Variables](#default-variables)
  3. - [Global Variables](#global-variables)
  4. - [Batteries-Included Templates](#batteries-included-templates)
  5. - [Custom Templates](#custom-templates)
  6. ## Default Variables
  7. - `date`: defaults to today's date, under universal time, printed according to RFC 8601 (for example, `"2014-03-11"`)
  8. - `datetime`: defaults to today's date and time, under universal time, printed according to RFC 8601 (for example, `"2014-03-11T16:06:02+00:00"`).
  9. - `author_name`: Defaults to `"Anonymous"`
  10. - `author_email`: Defaults to `"anonymous@example.org"`
  11. - `apps_dir`: Directory where OTP applications should be created in release projects. Defaults to `"apps/"`.
  12. - `copyright_year`: Defaults to the current year, under universal time.
  13. ## Global Variables
  14. Global variables can be set by editing the file at `$HOME/.rebar3/templates/globals`:
  15. {variables, [
  16. {author_name, "My Name Is A String"},
  17. {copyright_year, "2014-2022", "The year or range of years for copyright"},
  18. {my_custom_var, "hello there"}
  19. ]}.
  20. This will let you define variables for all templates.
  21. Variables left undefined will be ignored and revert to the default value.
  22. The override order for these variables will be: Defaults < $HOME/.rebar3/templates/globals < command line invocation.
  23. ## Batteries-Included Templates ##
  24. Rebar3 ships with a few templates installed, which can be listed by calling `rebar3 new`:
  25. → ./rebar3 new
  26. app (built-in): OTP Application
  27. lib (built-in): OTP Library application (no processes)
  28. release (built-in): OTP Release structure for executable programs
  29. plugin (built-in): Rebar3 plugin
  30. Any custom plugins would be followed as `<plugin_name> (custom): <description>`.
  31. Details for each individual plugin can be obtained by calling `rebar3 new help <plugin>`:
  32. → ./rebar3 new help plugin
  33. plugin:
  34. built-in template
  35. Description: Rebar3 plugin
  36. Variables:
  37. appid="myplugin" (Name of the plugin)
  38. desc="A rebar plugin" (Short description of the plugin's purpose)
  39. date="2014-11-10"
  40. datetime="2014-11-10T18:29:41+00:00"
  41. author_name="Anonymous"
  42. author_email="anonymous@example.org"
  43. copyright_year="2014"
  44. apps_dir="apps/" (Directory where applications will be created if needed)
  45. All the variables there have their default values shown, and an optional explanation in parentheses.
  46. The variables can also be [overriden globally](#global-variables).
  47. ## Custom Templates ##
  48. Custom templates can be added in `$HOME/.rebar3/templates/`. Each template is at least two files:
  49. - `my_template.dtl`: There can be many of these files. They are regular Erlang files using the django template syntax for variable replacements.
  50. - `my_template.template`; Called the *template index*, there is one per template callable from `rebar3`. This one will be visible when calling `rebar3 new my_template`. This file regroups the different \*.dtl files into a more cohesive template.
  51. ### File Syntax ###
  52. #### Template Index ####
  53. The following options are available:
  54. {description, "This template does a thing"}.
  55. {variables, [
  56. {var1, "default value"},
  57. {var2, "default", "explain what this does in help files"},
  58. {app_dir, ".", "The directory where the application goes"}
  59. ]}.
  60. {dir, "{{appdir}}/src"}.
  61. {file, "mytemplate_README", "README"}.
  62. {chmod, "README", 8#644}.
  63. {template, "myapp/myapp.app.src.dtl", "{{appdir}}/src/{{name}}.app.src"}.
  64. Specifically:
  65. - `description`: takes a string explaining what the template is for.
  66. - `variables`: takes a list of variables in two forms:
  67. - `{Name, DefaultString, HelpString}`;
  68. - `{Name, DefaultString}`.
  69. - `{dir, TemplatablePathString}`: creates a given directory. Variable names can be used in the path name.
  70. - `{file, FilePath, DestFilePath}`: copies a file literally to its destination.
  71. - `{template, DtlFilePath, TemplatablePathString}`: evaluates a given template. The `DtlFilePath` is relative to the template index.
  72. - `{chmod, FilePath, Int}`: changes the permission of a file, using the integer value specified. Octal values can be entered by doing `8#640`.
  73. ### Example ###
  74. As an example, we'll create a template for Common Test test suites. Create the directory structure `~/.rebar/templates/` and then go in there.
  75. We'll start with an index for our template, called `ct_suite.template`:
  76. ```erlang
  77. {description, "A basic Common Test suite for an OTP application"}.
  78. {variables, [
  79. {suite, "suite", "Name of the suite, prepended to the standard _SUITE suffix"}
  80. ]}.
  81. {dir, "test"}.
  82. {template, "ct_suite.erl.dtl", "test/{{suite}}_SUITE.erl"}.
  83. ```
  84. This tells rebar3 to create the test directory and to evaluate an [ErlyDTL](https://github.com/erlydtl/erlydtl) template. All the paths are relative to the current working directory.
  85. Let's create the template file:
  86. ```erlang
  87. -module({{suite}}_SUITE).
  88. -include_lib("common_test/include/ct.hrl").
  89. -include_lib("eunit/include/eunit.hrl"). % Eunit macros for convenience
  90. -export([all/0
  91. ,groups/0
  92. %,init_per_suite/1, end_per_suite/1
  93. %,init_per_group/2, end_per_group/2
  94. ,init_per_testcase/2, end_per_testcase/2
  95. ]).
  96. -export([fail/1]).
  97. all() -> [fail].
  98. groups() -> [].
  99. init_per_testcase(_Name, Config) -> Config.
  100. end_per_testcase(_Name, _Config) -> ok.
  101. fail(_Config) ->
  102. ?assert(false).
  103. ```
  104. This one does very simple variable substitution for the name (using `{{suite}}`) and that's all it needs.
  105. Let's get to any existing project you have and try it:
  106. → ./rebar3 new
  107. app (built-in): OTP Application
  108. ct_suite (custom): A basic Common Test suite for an OTP application
  109. lib (built-in): OTP Library application (no processes)
  110. release (built-in): OTP Release structure for executable programs
  111. plugin (built-in): Rebar3 plugin
  112. The first line shows that our `ct_suite` temlate has been detected and is usable.
  113. Let's look at the details:
  114. → ./rebar3 new help ct_suite
  115. ct_suite:
  116. custom template (/home/ferd/.rebar3/templates/ct_suite.template)
  117. Description: A basic Common Test suite for an OTP application
  118. Variables:
  119. suite="suite" (Name of the suite, prepended to the standard _SUITE suffix)
  120. date="2014-11-10"
  121. datetime="2014-11-10T18:46:33+00:00"
  122. author_name="Anonymous"
  123. author_email="anonymous@example.org"
  124. copyright_year="2014"
  125. apps_dir="apps/" (Directory where applications will be created if needed)
  126. The documentation from variables and the description are well in place. To apply the template, go to any of your OTP application's top-level directory:
  127. → ./rebar3 new ct_suite suite=demo
  128. ===> Writing test/demo_SUITE.erl
  129. And you will see the code in place.
  130. ~