選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

274 行
9.3 KiB

  1. # Plugins #
  2. Rebar3's system is based on the concept of *[providers](https://github.com/tsloughter/providers)*. A provider has three callbacks:
  3. - `init(State) -> {ok, NewState}`, which helps set up the state required, state dependencies, etc.
  4. - `do(State) -> {ok, NewState} | {error, Error}`, which does the actual work.
  5. - `format_error(Error) -> String`, which allows to print errors when they happen, and to filter out sensitive elements from the state.
  6. A provider should also be an OTP Library application, which can be fetched as any other Erlang dependency, except for Rebar3 rather than your own system or application.
  7. This document contains the following elements:
  8. - [Using a Plugin](#using-a-plugin)
  9. - [Reference](#reference)
  10. - [Provider Interface](#provider-interface)
  11. - [List of Possible Dependencies](#list-of-possible-dependencies)
  12. - [Rebar State Manipulation](#rebar-state-manipulation)
  13. - [Tutorial](#tutorial)
  14. ## Using a Plugin ##
  15. To use the a plugin, add it to the rebar.config:
  16. ```erlang
  17. {plugins, [
  18. {plugin_name, ".*", {git, "git@host:user/name-of-plugin.git", {tag, "v1.0.0"}}}
  19. ]}.
  20. ```
  21. Then you can just call it directly:
  22. ```
  23. → rebar3 plugin_name
  24. ===> Fetching plugin_name
  25. ===> Compiling plugin_name
  26. <PLUGIN OUTPUT>
  27. ```
  28. ## Reference ##
  29. TODO
  30. ### Provider Interface ###
  31. TODO
  32. ### List of Possible Dependencies ###
  33. TODO
  34. ### Rebar State Manipulation ###
  35. TODO
  36. ## Tutorial ##
  37. ### First version ###
  38. In this tutorial, we'll show how to start from scratch, and get a basic plugin written. The plugin will be quite simple: it will look for instances of 'TODO:' lines in comments and report them as warnings. The final code for the plugin can be found on [bitbucket](https://bitbucket.org/ferd/rebar3-todo-plugin).
  39. The first step is to create a new OTP Application that will contain the plugin:
  40. → rebar3 new plugin provider_todo desc="example rebar3 plugin"
  41. ...
  42. → cd provider_todo
  43. → git init
  44. Initialized empty Git repository in /Users/ferd/code/self/provider_todo/.git/
  45. Open up the `src/provider_todo.erl` file and make sure you have the following skeleton in place:
  46. ```erlang
  47. -module(provider_todo).
  48. -behaviour(provider).
  49. -export([init/1, do/1, format_error/1]).
  50. -include_lib("rebar3/include/rebar.hrl").
  51. -define(PROVIDER, todo).
  52. -define(DEPS, [app_discovery]).
  53. %% ===================================================================
  54. %% Public API
  55. %% ===================================================================
  56. -spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
  57. init(State) ->
  58. Provider = providers:create([
  59. {name, ?PROVIDER}, % The 'user friendly' name of the task
  60. {module, ?MODULE}, % The module implementation of the task
  61. {bare, true}, % The task can be run by the user, always true
  62. {deps, ?DEPS}, % The list of dependencies
  63. {example, "rebar provider_todo"}, % How to use the plugin
  64. {opts, []} % list of options understood by the plugin
  65. {short_desc, "example rebar3 plugin"},
  66. {desc, ""}
  67. ]),
  68. {ok, rebar_state:add_provider(State, Provider)}.
  69. -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
  70. do(State) ->
  71. {ok, State}.
  72. -spec format_error(any()) -> iolist().
  73. format_error(Reason) ->
  74. io_lib:format("~p", [Reason]).
  75. ```
  76. This shows all the basic content needed. Note that we leave the `DEPS` macro to the value `app_discovery`, used to mean that the plugin should at least find the project's source code (excluding dependencies).
  77. In this case, we need to change very little in `init/1`. Here's the new provider description:
  78. ```erlang
  79. Provider = providers:create([
  80. {name, ?PROVIDER}, % The 'user friendly' name of the task
  81. {module, ?MODULE}, % The module implementation of the task
  82. {bare, true}, % The task can be run by the user, always true
  83. {deps, ?DEPS}, % The list of dependencies
  84. {example, "rebar todo"}, % How to use the plugin
  85. {opts, []}, % list of options understood by the plugin
  86. {short_desc, "Reports TODOs in source code"},
  87. {desc, "Scans top-level application source and find "
  88. "instances of TODO: in commented out content "
  89. "to report it to the user."}
  90. ]),
  91. ```
  92. Instead, most of the work will need to be done directly in `do/1`. We'll use the `rebar_state` module to fetch all the applications we need. This can be done by calling the `project_apps/1` function, which returns the list of the project's top-level applications.
  93. ```erlang
  94. do(State) ->
  95. lists:foreach(fun check_todo_app/1, rebar_state:project_apps(State)),
  96. {ok, State}.
  97. ```
  98. This, on a high level, means that we'll check each top-level app one at a time (there may often be more than one top-level application when working with releases)
  99. The rest is filler code specific to the plugin, in charge of reading each app path, go read code in there, and find instances of 'TODO:' in comments in the code:
  100. ```erlang
  101. check_todo_app(App) ->
  102. Path = filename:join(rebar_app_info:dir(App),"src"),
  103. Mods = find_source_files(Path),
  104. case lists:foldl(fun check_todo_mod/2, [], Mods) of
  105. [] -> ok;
  106. Instances -> display_todos(rebar_app_info:name(App), Instances)
  107. end.
  108. find_source_files(Path) ->
  109. [filename:join(Path, Mod) || Mod <- filelib:wildcard("*.erl", Path)].
  110. check_todo_mod(ModPath, Matches) ->
  111. {ok, Bin} = file:read_file(ModPath),
  112. case find_todo_lines(Bin) of
  113. [] -> Matches;
  114. Lines -> [{ModPath, Lines} | Matches]
  115. end.
  116. find_todo_lines(File) ->
  117. case re:run(File, "%+.*(TODO:.*)", [{capture, all_but_first, binary}, global, caseless]) of
  118. {match, DeepBins} -> lists:flatten(DeepBins);
  119. nomatch -> []
  120. end.
  121. display_todos(_, []) -> ok;
  122. display_todos(App, FileMatches) ->
  123. io:format("Application ~s~n",[App]),
  124. [begin
  125. io:format("\t~s~n",[Mod]),
  126. [io:format("\t ~s~n",[TODO]) || TODO <- TODOs]
  127. end || {Mod, TODOs} <- FileMatches],
  128. ok.
  129. ```
  130. Just using `io:format/2` to output is going to be fine.
  131. To test the plugin, push it to a source repository somewhere. Pick one of your projects, and add something to the rebar.config:
  132. ```erlang
  133. {plugins, [
  134. {provider_todo, ".*", {git, "git@bitbucket.org:ferd/rebar3-todo-plugin.git", {branch, "master"}}}
  135. ]}.
  136. ```
  137. Then you can just call it directly:
  138. ```
  139. → rebar3 todo
  140. ===> Fetching provider_todo
  141. ===> Compiling provider_todo
  142. Application merklet
  143. /Users/ferd/code/self/merklet/src/merklet.erl
  144. todo: consider endianness for absolute portability
  145. ```
  146. Rebar3 will download and install the plugin, and figure out when to run it. Once compiled, it can be run at any time again.
  147. ### Optionally Search Deps ###
  148. Let's extend things a bit. Maybe from time to time (when cutting a release), we'd like to make sure none of our dependencies contain 'TODO:'s either.
  149. To do this, we'll need to go parse command line arguments a bit, and change our execution model. The `?DEPS` macro will now need to specify that the `todo` provider can only run *after* dependencies have been installed:
  150. ```erlang
  151. -define(DEPS, [install_deps]).
  152. ```
  153. We can add the option to the list we use to configure the provider in `init/1`:
  154. ```erlang
  155. {opts, [ % list of options understood by the plugin
  156. {deps, $d, "deps", undefined, "also run against dependencies"}
  157. ]},
  158. ```
  159. Meaning that deps can be flagged in by using the option `-d` (or `--deps`), and if it's not defined, well, we get the default value `undefined`. The last element of the 4-tuple is documentation for the option.
  160. And then we can implement the switch to figure out what to search:
  161. ```erlang
  162. do(State) ->
  163. Apps = case discovery_type(State) of
  164. project -> rebar_state:project_apps(State);
  165. deps -> rebar_state:project_apps(State) ++ rebar_state:src_deps(State)
  166. end,
  167. lists:foreach(fun check_todo_app/1, Apps),
  168. {ok, State}.
  169. [...]
  170. discovery_type(State) ->
  171. {Args, _} = rebar_state:command_parsed_args(State),
  172. case proplists:get_value(deps, Args) of
  173. undefined -> project;
  174. _ -> deps
  175. end.
  176. ```
  177. The `deps` option is found using `rebar_state:command_parsed_args(State)`, which will return a proplist of terms on the command-line after 'todo', and will take care of validating whether the flags are accepted or not. The rest can remain the same.
  178. Push the new code for the plugin, and try it again on a project with dependencies:
  179. ```
  180. → rebar3 todo --deps
  181. ===> Fetching provider_todo
  182. ===> Compiling provider_todo
  183. ===> Fetching bootstrap
  184. ===> Fetching file_monitor
  185. ===> Fetching recon
  186. [...]
  187. Application dirmon
  188. /Users/ferd/code/self/figsync/apps/dirmon/src/dirmon_tracker.erl
  189. TODO: Peeranha should expose the UUID from a node.
  190. Application meck
  191. /Users/ferd/code/self/figsync/_deps/meck/src/meck_proc.erl
  192. TODO: What to do here?
  193. TODO: What to do here?
  194. ```
  195. Rebar3 will now go pick dependencies before running the plugin on there.
  196. you can also see that the help will be completed for you:
  197. ```
  198. → rebar3 help todo
  199. Scans top-level application source and find instances of TODO: in commented out content to report it to the user.
  200. Usage: rebar todo [-d]
  201. -d, --deps also run against dependencies
  202. ```
  203. That's it, the todo plugin is now complete! It's ready to ship and be included in other repositories.